PHP在每个新词的末尾添加字符


PHP add character to the end of every new word

我有一个用空格分隔的单词串(除了最后一个单词),我想知道是否有办法在每个新词的末尾添加一个字符。例如:

$string = "this is a short string of words"

将成为

$string = "this; is; a; short; string; of; words;"

感谢

$str = 'lorem ipsum';  
$str_modified = str_replace(' ','; ',trim($str)).';';

爆炸和实施:

implode("; ", explode(" ", $string)).";"; 

您可以这样使用preg_replace()

$string = "this is a short string of words";
echo preg_replace('~('w+)~', '$1;',$string);
// output: this; is; a; short; string; of; words;