4 回答

TA贡献2080条经验 获得超4个赞
你需要:
$ro = preg_replace('/\s+/', ' ',$row['message']);
您使用的\s\s+是指空格(空格,制表符或换行符)后跟一个或多个空格。这实际上意味着用单个空格替换两个或多个空格。
您想要的是用单个空格替换一个或多个空格,因此您可以使用模式 \s\s*或\s+(推荐)

TA贡献1796条经验 获得超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条经验 获得超5个赞
简化为一个功能:
function removeWhiteSpace($text)
{
$text = preg_replace('/[\t\n\r\0\x0B]/', '', $text);
$text = preg_replace('/([\s])\1+/', ' ', $text);
$text = trim($text);
return $text;
}
根据Danuel O'Neal的回答。
- 4 回答
- 0 关注
- 682 浏览
添加回答
举报