gpt4 book ai didi

PHP - 拆分包含换行符的字符串并打印

转载 作者:行者123 更新时间:2023-12-03 23:01:34 25 4
gpt4 key购买 nike

为什么下面的代码不起作用...?

$test = "hello \n world \n !";
foreach(explode("\n",$test) as $line){
echo $line;
}

打印

hello world !

代替

hello 
world
!

谢谢

最佳答案

你还必须回应 <br />

The HTML <br /> element produces a line break in text (carriage-return). It is useful for writing a poem or an address, where the division of lines is significant.

$test = "hello \n world \n !";
foreach(explode("\n",$test) as $line){
echo $line;
echo "<br />";
}

文档: <br />


另一种选择是使用 nl2br在字符串中的所有换行符之前插入 HTML 换行符

$test = "hello \n world \n !";
echo nl2br($test);

文档:nl2br()

关于PHP - 拆分包含换行符的字符串并打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50704753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com