如何在PHP中修剪/删除字符串之间的空格? [重复]
问题描述:
This question already has an answer here:
I have a string like "Tom Jerry ". I want to trim the extra space from middle of the string(there is 2 white space between first name and last name, I want only one white space). I.e. desirable output is "Tom Jerry". How do I do that?
</div>
此问题已经存在 这里有一个答案: p>
-
从字符串中删除多余的空格
11 answers
span>
li>
ul>
div>
我有一个像“Tom Jerry”这样的字符串。 我想从字符串的中间修剪额外的空间(名字和姓氏之间有2个空格,我只想要一个空格)。 即 理想的输出是“汤姆杰里”。 我该怎么做? p> div>
答
Try this
$str = "Tom Jerry";
$cleanStr = trim(preg_replace('/\s\s+/', ' ', str_replace("
", " ", $str)));
echo $cleanStr;