删除 php 中单词之间的空格


Remove space in between words in php

如何在 php 中删除两个单词之间的空格。例如,'Hello World'应返回'HelloWorld' 。我尝试使用trim()但没有用。

您可以使用

str_replace

$str = str_replace(' ', '', $str);

你也可以这样做

$string = preg_replace('/'s+/', '', $string);
trim用于

删除字符串开头和结尾的空格。 看这里

试试吧

preg_replace("/'s+/", "", $str);

完整代码:

$string="Hello World";
$string = str_replace(' ', '', $string);

试试这个:

function space_trim($st) {
        return @str_replace(" ","",$st);
    }
    echo space_trim("   a fff     jjj    ");