Replace excess whitespaces and line-breaks with PHP?

后端 未结 10 1725
你的背包
你的背包 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:02

    try with:

    $string = "My    text       has so    much   whitespace    
    
    
    
    
    Plenty of    spaces  and            tabs";
    //Remove duplicate newlines
    $string = preg_replace("/[\n]*/", "\n", $string); 
    //Preserves newlines while replacing the other whitspaces with single space
    echo preg_replace("/[ \t]*/", " ", $string); 
    
    0 讨论(0)
  • 2020-12-07 21:10

    Not sure if this will be useful nor am I absolutely positive it works like it should but it seems to be working for me.

    A function that clears multiple spaces and anything else you want or don't want and produces either a single line string or a multi-line string (dependent on passed arguments/options). Can also remove or keep characters for other languages and convert newline tabs to spaces.

    /** ¯\_(ツ)_/¯ Hope it's useful to someone. **/
    // If $multiLine is null this removes spaces too. <options>'[:emoji:]' with $l = true allows only known emoji.
    // <options>'[:print:]' with $l = true allows all utf8 printable chars (including emoji).
    // **** TODO: If a unicode emoji or language char is used in $options while $l = false; we get an odd � symbol replacement for any non-matching char. $options char seems to get through, regardless of $l = false ? (bug (?)interesting)
    function alphaNumericMagic($value, $options = '', $l = false, $multiLine = false, $tabSpaces = "    ") {
        $utf8Emojis = '';
        $patterns = [];
        $replacements = [];
        if ($l && preg_match("~(\[\:emoji\:\])~", $options)) {
            $utf8Emojis = [
                '\x{1F600}-\x{1F64F}', /* Emoticons */
                '\x{1F9D0}-\x{1F9E6}',
                '\x{1F300}-\x{1F5FF}', /* Misc Characters */ // \x{1F9D0}-\x{1F9E6}
                '\x{1F680}-\x{1F6FF}', /* Transport and Map */
                '\x{1F1E0}-\x{1F1FF}' /* Flags (iOS) */
            ];
            $utf8Emojis = implode('', $utf8Emojis);
        }
        $options = str_replace("[:emoji:]", $utf8Emojis, $options);
        if (!preg_match("~(\[\:graph\:\]|\[\:print\:\]|\[\:punct\:\]|\\\-)~", $options)) {
            $value = str_replace("-", ' ', $value);
        }
        if ($l) {
            $l = 'u';
            $options = $options . '\p{L}\p{N}\p{Pd}';
        } else { $l = ''; }
        if (preg_match("~(\[\:print\:\])~", $options)) {
            $patterns[] = "/[ ]+/m";
            $replacements[] = " ";
        }
        if ($multiLine) {
            $patterns[] = "/(?<!^)(?:[^\r\na-z0-9][\t]+)/m";
            $patterns[] = "/[ ]+(?![a-z0-9$options])|[^a-z0-9$options\s]/im$l";
            $patterns[] = "/\t/m";
            $patterns[] = "/(?<!^)$tabSpaces/m";
            $replacements[] = " ";
            $replacements[] = "";
            $replacements[] = $tabSpaces;
            $replacements[] = " ";
        } else if ($multiLine === null) {
            $patterns[] = "/[\r\n\t]+/m";
            $patterns[] = "/[^a-z0-9$options]/im$l";
            $replacements = "";
        } else {
            $patterns[] = "/[\r\n\t]+/m";
            $patterns[] = "/[ ]+(?![a-z0-9$options\t])|[^a-z0-9$options ]/im$l";
            $replacements[] = " ";
            $replacements[] = "";
        }
        echo "\n";
        print_r($patterns);
        echo "\n";
        echo $l;
        echo "\n";
        return preg_replace($patterns, $replacements, $value);
    }
    

    Example usage:

    echo header('Content-Type: text/html; charset=utf-8', true);
    $string = "fjl!sj\nfl _  sfjs-lkjf\r\n\tskj 婦女與環境健康 fsl \tklkj\thl jhj ⚧                                                                    
    0 讨论(0)
  • 2020-12-07 21:12

    this would COMPLETELY MINIFY the entire string (such as a large blog article) yet preserving all HTML tags in place.

    $email_body = str_replace(PHP_EOL, ' ', $email_body);
        //PHP_EOL = PHP_End_Of_Line - would remove new lines too
    $email_body = preg_replace('/[\r\n]+/', "\n", $email_body);
    $email_body = preg_replace('/[ \t]+/', ' ', $email_body);
    
    0 讨论(0)
  • 2020-12-07 21:14

    Edited the right answer. From PHP 5.2.4 or so, the following code will do:

    echo preg_replace('/\v(?:[\v\h]+)/', '', $string);
    
    0 讨论(0)
  • 2020-12-07 21:14

    why you are doing like this?
    html displays only one space even you use more than one space...

    For example:

    <i>test               content 1       2 3 4            5</i>
    

    The output willl be:
    test content 1 2 3 4 5

    if you need more than single space in html, you have to use &nbsp;

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

    Replace Multiple Newline, Tab, Space

    $text = preg_replace("/[\r\n]+/", "\n", $text);
    $text = preg_replace("/\s+/", ' ', $text);
    

    Tested :)

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