Replace excess whitespaces and line-breaks with PHP?

后端 未结 10 1729
你的背包
你的背包 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: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);
    

提交回复
热议问题