我有一串用空格分隔的单词(最后一个单词除外),我想知道是否有办法在每个新单词的末尾添加一个字符。例如:
$string = "this is a short string of words"
会成为
$string = "this; is; a; short; string; of; words;"
谢谢
我有一串用空格分隔的单词(最后一个单词除外),我想知道是否有办法在每个新单词的末尾添加一个字符。例如:
$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)).';';
你可以preg_replace()
这样使用:
$string = "this is a short string of words";
echo preg_replace('~(\w+)~', '$1;',$string);
// output: this; is; a; short; string; of; words;