Replace excess whitespaces and line-breaks with PHP?

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

提交回复
热议问题