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

时间:2013-07-17 23:54:43

标签: php

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

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

会变成

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

由于

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

ExplodeImplode

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

答案 2 :(得分: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;