PHP “backspace” character during output possible?

后端 未结 8 1099
囚心锁ツ
囚心锁ツ 2021-02-13 04:30

I have a feeling the answer is \"it\'s not possible,\" but thought I\'d ask to satisfy my curiosity.

I have some code that\'s echoed where the \\n is unavoidable:

<
8条回答
  •  终归单人心
    2021-02-13 04:55

    Can't you do it like this:

    $str = str_replace("\n", ' ', $str);
    while (strpos($str, '  ') !== false) // while there's two spaces in a row
      $str = str_replace('  ', ' ', $str);
    

    Now $str will have every spaces or \n characters sequences replaced by only one space. (because if you just remove \n you migth have some place where a space is missing, and if you just replace it by a space you'll have some places with multiple spaces in a row).

    EDIT: i don't know if the loop is really necessary but i don't have anything to test here if str_replace will automatically do the trick (and i don't think using regexp for such a simple thing is really a good idea).

提交回复
热议问题