Replace multiple newlines, tabs, and spaces

前端 未结 10 1786
情深已故
情深已故 2020-11-27 05:56

I want to replace multiple newline characters with one newline character, and multiple spaces with a single space.

I tried preg_replace(\"/\\n\\n+/\", \"\\n\",

相关标签:
10条回答
  • 2020-11-27 06:32

    I tried all of above, but it didn't work for me. Then I created some long way to resolve that issue...

    Before :

    echo nl2br($text);
    

    After :

    $tempData = nl2br($text);
    $tempData = explode("<br />",$tempData);
    
    foreach ($tempData as $val) {
       if(trim($val) != '')
       {
          echo $val."<br />";
       }
    }
    

    And it's worked for me.. I wrote here because, if somebody came here to find answer like me.

    0 讨论(0)
  • 2020-11-27 06:32

    If you just want to replace multiple tabs with a single tab, use the following code.

    preg_replace("/\s{2,}/", "\t", $string);
    
    0 讨论(0)
  • 2020-11-27 06:33

    Try this:

    preg_replace("/[\r\n]*/", "\r\n", $text); 
    
    0 讨论(0)
  • 2020-11-27 06:33

    I have dealt with strip_tags function in PHP and had some problems like: after having a linebreak then appear a new line with some spaces and then a new linebreak appear continuously ...etc. without any rule :(.

    This is my solution for dealing with strip_tags

    Replace multiple spaces to one, multiple linebreaks to single linebreak

    function cleanHtml($html)
    {
        // Clean code into script tags
        $html = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $html);
    
        // Clean code into style tags
        $html = preg_replace('/<\s*style.+?<\s*\/\s*style.*?>/si', '', $html );
    
        // Strip HTML
        $string = trim(strip_tags($html));
    
        // Replace multiple spaces on each line (keep linebreaks) with single space
        $string = preg_replace("/[[:blank:]]+/", " ", $string); // (*)
    
        // Replace multiple spaces of all positions (deal with linebreaks) with single linebreak
        $string = preg_replace('/\s{2,}/', "\n", $string); // (**)
        return $string;
    }
    

    Keywords are (*) and (**).

    0 讨论(0)
  • 2020-11-27 06:35

    In theory, you regular expression does work, but the problem is that not all operating system and browsers send only \n at the end of string. Many will also send a \r.

    Try:

    I've simplified this one:

    preg_replace("/(\r?\n){2,}/", "\n\n", $text);
    

    And to address the problem of some sending \r only:

    preg_replace("/[\r\n]{2,}/", "\n\n", $text);
    

    Based on your update:

    // Replace multiple (one ore more) line breaks with a single one.
    $text = preg_replace("/[\r\n]+/", "\n", $text);
    
    $text = wordwrap($text,120, '<br/>', true);
    $text = nl2br($text);
    
    0 讨论(0)
  • 2020-11-27 06:41

    Use \R (which represents any line ending sequence):

    $str = preg_replace('#\R+#', '</p><p>', $str);
    

    It was found here: Replacing two new lines with paragraph tags

    PHP documentation about Escape sequences:

    \R (line break: matches \n, \r and \r\n)

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