如何添加'+'灵活字符串php空格后的字符


how to add '+' character after space in flexible string php?

我需要在字符串的空格后添加'+'字符。例如:

$str = "hello world helloo worldd" -> "+hello +world +helloo +worldd". 

字符串是灵活的。我怎么能在php做。谢谢你的帮助

您只需要将space 替换为space& + +(可能在字符串的开头添加CC_3)。

$str = 'hello world helloo worldd';
echo '+' . str_replace(' ', ' +', $str); // returns '+hello +world +helloo +worldd'

你可以这样做:-

$v = 'hello world helloo worldd';
$v = '+'. str_replace(' ', ' +', $v);
echo $v;

您可以使用str_replace PHP函数来实现。例如,要替换字符串"hello world hello world "....

$stringValue = 'hello world helloo worldd';
echo '+' . str_replace(' ', ' +', $stringValue);

如果需要,请查看PHP官方文档....http://php.net/str_replace