【发布时间】:2011-04-13 17:40:06
【问题描述】:
$variable = 'one, two, three';
如何将单词之间的逗号替换为<br>?
$variable 应该变成:
one<br>
two<br>
three
【问题讨论】:
$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
【讨论】:
你可以这样做:
$variable = str_replace(', ',"<br>\n",$variable);
【讨论】:
$variable = preg_replace('/\s*,\s*/', "<br>\n", $variable);
这会将您带入正则表达式领域,但这将处理逗号之间随机间距的情况,例如
$variable = 'one,two, three';
或
$variable = 'one , two, three';
【讨论】: