PHP “backspace” character during output possible?

后端 未结 8 1097
囚心锁ツ
囚心锁ツ 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:52

    Yes, the backspace character is ASCII character code 8 (According to the ASCII table), so you can output it in php using chr(). eg:

    echo 'ab' . chr(8);
    

    will output "a"

    0 讨论(0)
  • 2021-02-13 04:53

    What about just replacing the "\n" by a white space (or just nothing, if you already have one space in your incoming string) ?

    Like this, for instance :

    $str = "Hello\nWorld!";
    var_dump($str);
    
    $str = str_replace("\n", ' ', $str);
    var_dump($str);
    

    The first output gives :

    string 'Hello
    World!' (length=12)
    

    And the second one :

    string 'HelloWorld!' (length=11)
    

    Is that not enough ?
    (Or maybe I don't understand the question well)

    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2021-02-13 04:58

    This is not a direct answer to his exact question, but to what the title seems to allude to: outputting a PHP "backspace" character, which is probably only useful when using the PHP CLI.

    You can find the right ASCII code for this (and other characters) on the ASCII table, and then use either chr() or an escape sequence:

    echo chr(8);
    echo "\010";
    
    0 讨论(0)
  • 2021-02-13 05:05

    If the output target is HTML then extra spaces don't matter - browsers don't render multiple, contiguous spaces (which is why we have &nbsp;)

    If the output target is something else, then you can simply cleanup the output. As you can see from other replies, there are a myriad of ways to do this. It seems like you're working with echo statements so the output-buffering functions will be the route you want to take.

    ob_start();
    
    echo "Hello \n";
    echo "World!";
    
    $output = preg_replace( "/ +/", ' ', str_replace( "\n", ' ', ob_get_clean() ) );
    
    echo $output;
    
    0 讨论(0)
  • 2021-02-13 05:16

    I don't know how to do a backspace, but if you are just trying to do a new line I would use:

    echo "<br>";
    

    PHP code allows for html code as long as it is used in a print or echo statement and is inside double quotes.

    0 讨论(0)
提交回复
热议问题