php替换空格的方法:可以利用preg_replace()函数并结合正则表达式【/\s+/】来替换空格,如【preg_replace('/\s+/',' ',$contents)】。

函数介绍:
preg_replace 函数执行一个正则表达式的搜索和替换。
(推荐教程:php视频教程)
举例:
立即学习“PHP免费学习笔记(深入)”;
替换所有空格为空
$contents=" abc ";
$contents=preg_replace('/\s+/','',$contents);
//结果$contents="abc";相关推荐:php培训











