4 回答

TA貢獻2080條經(jīng)驗 獲得超4個贊
你需要:
$ro = preg_replace('/\s+/', ' ',$row['message']);
您使用的\s\s+是指空格(空格,制表符或換行符)后跟一個或多個空格。這實際上意味著用單個空格替換兩個或多個空格。
您想要的是用單個空格替換一個或多個空格,因此您可以使用模式 \s\s*或\s+(推薦)

TA貢獻1796條經(jīng)驗 獲得超4個贊
<?php
$str = "This is a string with
spaces, tabs and newlines present";
$stripped = preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $str);
echo $str;
echo "\n---\n";
echo "$stripped";
?>
這個輸出
This is a string with
spaces, tabs and newlines present
---
This is a string with spaces, tabs and newlines present

TA貢獻1802條經(jīng)驗 獲得超5個贊
簡化為一個功能:
function removeWhiteSpace($text)
{
$text = preg_replace('/[\t\n\r\0\x0B]/', '', $text);
$text = preg_replace('/([\s])\1+/', ' ', $text);
$text = trim($text);
return $text;
}
根據(jù)Danuel O'Neal的回答。
- 4 回答
- 0 關注
- 710 瀏覽
添加回答
舉報