$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';