如何更改列表的分隔符?

发布于 2024-09-19 16:13:10 字数 194 浏览 8 评论 0原文

$variable = 'one, two, three';

如何用
替换单词之间的逗号?

$variable 应该变成:

one<br>
two<br>
three
$variable = 'one, two, three';

How can I replace the commas between words with <br>?

$variable should become:

one<br>
two<br>
three

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

十年不长 2024-09-26 16:13:10

使用 str_replace

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

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

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

Either use str_replace:

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

or, if you want to do other things with the elements in between, explode() and implode():

$variable_exploded = explode(", ", $variable);
$variable_imploded = implode("<br>", $variable_exploded);
琴流音 2024-09-26 16:13:10
$variable = str_replace(", ","<br>\n",$variable);

应该做到这一点。

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

Should do the trick.

指尖凝香 2024-09-26 16:13:10
$variable = explode(', ',$variable);
$variable = implode("<br/>\n",$variable);

然后你可以echo $variable

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

You can then just echo $variable

小忆控 2024-09-26 16:13:10

你可以这样做:

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

You can do:

$variable = str_replace(', ',"<br>\n",$variable);
要走干脆点 2024-09-26 16:13:10
$variable = preg_replace('/\s*,\s*/', "<br>\n", $variable);

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

$variable = 'one,two, three';

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

This takes you into regex land but this will handle cases of random spacing between commas, e.g.

$variable = 'one,two, three';

or

$variable = 'one , two, three';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文