$variable = 'one, two, three';
如何用<br>
?
$variable
应该成为:
one<br>
two<br>
three
答案 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';