1

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

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

会成为

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

谢谢

4

3 回答 3

3
$str = 'lorem ipsum';  
$str_modified = str_replace(' ','; ',trim($str)).';';
于 2013-07-17T23:57:03.337 回答
3

爆炸内爆

implode("; ", explode(" ", $string)).";"; 
于 2013-07-17T23:58:15.390 回答
1

你可以preg_replace()这样使用:

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

// output: this; is; a; short; string; of; words;
于 2013-07-17T23:59:43.903 回答