如何更改列表的分隔符?

时间:2010-09-17 13:25:06

标签: php explode

$variable = 'one, two, three';

如何用<br>

替换单词之间的逗号

$variable应该成为:

one<br>
two<br>
three

5 个答案:

答案 0 :(得分:12)

使用str_replace

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

或者,如果您想对其中的元素执行其他操作,explode()implode()

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

答案 1 :(得分:8)

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

应该做的伎俩。

答案 2 :(得分:5)

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

然后您可以echo $variable

答案 3 :(得分:3)

你可以这样做:

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

答案 4 :(得分:3)

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

这将带你进入正则表达式,但这将处理逗号之间随机间隔的情况,例如

$variable = 'one,two, three';

$variable = 'one , two, three';