6
$variable = 'one, two, three';

如何用 替换单词之间的逗号<br>

$variable应该变成:

one<br>
two<br>
three
4

5 回答 5

13

要么使用str_replace

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

或者,如果您想对两者之间的元素做其他事情,explode()并且implode()

$variable_exploded = explode(", ", $variable);
$variable_imploded = implode("<br>", $variable_exploded);
于 2010-09-17T13:27:17.697 回答
8
$variable = str_replace(", ","<br>\n",$variable);

应该做的伎俩。

于 2010-09-17T13:27:27.210 回答
5
$variable = explode(', ',$variable);
$variable = implode("<br/>\n",$variable);

然后你可以echo $variable

于 2010-09-17T13:28:16.167 回答
3

你可以做:

$variable = str_replace(', ',"<br>\n",$variable);
于 2010-09-17T13:29:50.670 回答
3
$variable = preg_replace('/\s*,\s*/', "<br>\n", $variable);

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

$variable = 'one,two, three';

或者

$variable = 'one , two, three';
于 2010-09-17T13:52:42.247 回答