Opposite of nl2br? Is it str_replace?

后端 未结 8 1637
旧时难觅i
旧时难觅i 2020-12-02 00:07

So the function nl2br is handy. Except in my web app, I want to do the opposite, interpret line breaks as new lines, since they will be echoed into a pre-filled form.

<
8条回答
  •  执笔经年
    2020-12-02 00:56

    If whitespaces are stripped out before outputting the html (for minification), "\n", "\r", PHP_EOL, etc. will get stripped out. ASCII encoding will survive the stripping process.

    function minify($buffer) {
        return preg_replace('/\s\s+/', ' ', preg_replace('~>\s+<~', '><', $buffer));
    }
    ob_start('minify');
    
    ...
    
    $nl = preg_replace('/\/i', "
    ", $br);
    echo "";
    
    ...
    
    ob_get_flush();
    

提交回复
热议问题