如何更改列表的分隔符?

问题描述:

$variable = 'one, two, three'; 

如何用<br>替换单词之间的逗号?如何更改列表的分隔符?

$variable应该变成:

one<br> 
two<br> 
three 

要么使用str_replace

$variable = str_replace(", ", "<br>", $variable); 

,或者,如果你想要做其他事情与元素之间,explode()implode()

$variable_exploded = explode(", ", $variable); 
$variable_imploded = implode("<br>", $variable_exploded); 

$variable = str_replace(", ","<br>\n",$variable); 

应该这样做。

$variable = explode(', ',$variable); 
$variable = implode("<br/>\n",$variable); 

然后,您可以只echo $variable

+0

这是非常昂贵的.. – Petrogad 2010-09-17 13:44:12

你可以这样做:

$variable = str_replace(', ',"<br>\n",$variable); 

$variable = preg_replace('/\s*,\s*/', "<br>\n", $variable); 

这需要你进入正则表达式的土地,但这样会处理逗号之间的随机间距,例如病例

$variable = 'one,two, three'; 

$variable = 'one , two, three';