replace blank lines in php

前端 未结 6 1316
执笔经年
执笔经年 2020-12-20 03:26

How do I remove multiple blank lines from a string. I have looked at the examples on stackoverflow and have tried to change my code accordingly but I am not getting the rig

相关标签:
6条回答
  • 2020-12-20 04:00

    also put a trim on it:

       function removeMultipleBlankLines(&$array) {
          return trim(preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $array));
        }
    
    0 讨论(0)
  • 2020-12-20 04:05

    try this:

    $string = str_replace("\r\n",'',$string);
    
    0 讨论(0)
  • 2020-12-20 04:11

    why not $string = trim($string);?

    you can even specify what you want to be trimmed with a second optional parameter, that is trim ( string $str [, string $charlist ] ) see the docs http://php.net/manual/en/function.trim.php

    0 讨论(0)
  • 2020-12-20 04:12

    You can do:

    $array_with_nonblank_lines = array_filter($textarray, 'trim');
    

    to get exactly what you want.

    Here is link to php's docs page about trim function, which does the job.

    0 讨论(0)
  • 2020-12-20 04:13

    Try this one:

    $str =preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\r\n", $str);
    

    The output file will give same output on simple notepad and also on text editors eg Notepad++.

    0 讨论(0)
  • 2020-12-20 04:20

    Using regular expressions works, but some people unfamiliar with regular expressions could get confused as to how you lay out your expression. Using the familiar trim command is simply a better choice in my opinion, but in the end I think trim would be faster because it doesn't need to evaluate the regular expression etc.

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