PHP “backspace” character during output possible?

后端 未结 8 1102
囚心锁ツ
囚心锁ツ 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 05:17

    If you wanted to be able to do it for anything you could use the output buffer:

    ob_start();
    
    echo "Hello\n World";
    
    $out = ob_get_contents();
    
    ob_end_clear();
    echo str_replace('\n', '', $out);
    

    You could even use httaccess to append scripts containing this to any script called.

    However, couldn't you just deal with it before it is set to stdout? Like

    function print2($str){
        echo str_replace("\n", '', $str);
    }
    
    0 讨论(0)
  • 2021-02-13 05:18

    Instead of thinking about how to do a backspace character, I would suggest rethinking the problem. Why do you want to take back some of your output? Probably because you outputted it too early.

    Instead of doing an echo on the intermediary values, append them to the buffer. Then edit the buffer. Then print it out. If you can't control whether the output is produced or not (for example, you're using external vendor's library that outputs stuff), use PHP output buffering.

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