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

PHP在每个新词的末尾添加字符,php,Php,我有一个由空格分隔的单词串(除了最后一个单词),我想知道是否有办法在每个新词的末尾添加一个字符。例如: $string = "this is a short string of words" 将成为 $string = "this; is; a; short; string; of; words;" 谢谢和: 以及: 您可以像这样使用preg\u replace(): $string = "this is a short string of words"; echo preg_replace

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

$string = "this is a short string of words"
将成为

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

和:

以及:


您可以像这样使用
preg\u replace()

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

// output: this; is; a; short; string; of; words;

您可以像这样使用
preg\u replace()

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

// output: this; is; a; short; string; of; words;

用修剪的方式把它包起来,以去掉尾随的空格。它确实需要一个
但不是
修剪
将其包装在修剪中以去除尾随空间。它确实需要
但不是
修剪
只是为了记录,爆炸和内爆会快得多:)@KerePuki No我是最后一个:)。。我还必须承认
str_replace()
更快。我会让JLP接受。他跑得最快,回答得最好。与爆炸和内爆的时间大致相同,但他确实是先回答的。他比我早三分钟回答;)为了记录在案,爆炸和内爆会快得多:)@KerePuki不,我是最后一个:)。。我还必须承认
str_replace()
更快。我会让JLP接受。他跑得最快,回答得最好。与爆炸和内爆的时间大致相同,但他确实是先回答的。他比我早三分钟回答;)
$string = "this is a short string of words";
echo preg_replace('~(\w+)~', '$1;',$string);

// output: this; is; a; short; string; of; words;