Replace excess whitespaces and line-breaks with PHP?

后端 未结 10 1726
你的背包
你的背包 2020-12-07 20:25
$string = \"My    text       has so    much   whitespace    




Plenty of    spaces  and            tabs\";

echo preg_replace(\"/\\s\\s+/\", \" \", $string);


        
相关标签:
10条回答
  • 2020-12-07 21:22

    Alternative approach:

    echo preg_replace_callback("/\s+/", function ($match) {
        $result = array();
        $prev = null;
        foreach (str_split($match[0], 1) as $char) {
            if ($prev === null || $char != $prev) {
                $result[] = $char;
            }
    
            $prev = $char;
        }
    
        return implode('', $result);
    }, $string);
    

    Output:

    My text has so much whitespace
    Plenty of spaces and tabs
    

    Edit: Readded this for it being a different approach. It's probably not what's asked for, but it will at least not merge groups of different whitespace (e.g. space, tab, tab, space, nl, nl, space, space would become space, tab, space, nl, space).

    0 讨论(0)
  • 2020-12-07 21:25
    //Newline and tab space to single space
    
    $from_mysql = str_replace(array("\r\n", "\r", "\n", "\t"), ' ', $from_mysql);
    
    
    // Multiple spaces to single space ( using regular expression)
    
    $from_mysql = ereg_replace(" {2,}", ' ',$from_mysql);
    
    // Replaces 2 or more spaces with a single space, {2,} indicates that you are looking for 2 or more than 2 spaces in a string.
    
    0 讨论(0)
  • 2020-12-07 21:25

    Had the same problem when passing echoed data from PHP to Javascript (formatted as JSON). The string was peppered with useless \r\n and \t characters that are neither required nor displayed on the page.

    The solution i ended up using is another way of echoing. That saves a lot of server resources compared to preg_replace (as it is suggested by other people here).


    Here the before and after in comparison:

    Before:

    echo '
    <div>
    
        Example
        Example
    
    </div>
    ';
    

    Output:

    <div>\r\n\r\n\tExample\r\n\tExample\r\n\r\n</div>


    After:

    echo 
    '<div>',
    
        'Example',
        'Example',
    
    '</div>';
    

    Output:

    <div>ExampleExample</div>


    (Yes, you can concatenate echo not only with dots, but also with comma.)

    0 讨论(0)
  • 2020-12-07 21:28

    First, I'd like to point out that new lines can be either \r, \n, or \r\n depending on the operating system.

    My solution:

    echo preg_replace('/[ \t]+/', ' ', preg_replace('/[\r\n]+/', "\n", $string));
    

    Which could be separated into 2 lines if necessary:

    $string = preg_replace('/[\r\n]+/', "\n", $string);
    echo preg_replace('/[ \t]+/', ' ', $string);
    

    Update:

    An even better solutions would be this one:

    echo preg_replace('/[ \t]+/', ' ', preg_replace('/\s*$^\s*/m', "\n", $string));
    

    Or:

    $string = preg_replace('/\s*$^\s*/m', "\n", $string);
    echo preg_replace('/[ \t]+/', ' ', $string);
    

    I've changed the regular expression that makes multiple lines breaks into a single better. It uses the "m" modifier (which makes ^ and $ match the start and end of new lines) and removes any \s (space, tab, new line, line break) characters that are a the end of a string and the beginning of the next. This solve the problem of empty lines that have nothing but spaces. With my previous example, if a line was filled with spaces, it would have skipped an extra line.

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