在mysql字段中,我有如下近似值
Hello there
world
当我使用以下代码格式化以上内容时:
echo htmlspecialchars($thestring)
它输出这个,
Hello there<br/><br/>world
我怎样才能让它换行?
我想继续使用htmlspecialchar来帮助处理其他html字符。
您需要调用nl2br
将换行符转换为html:
nl2br(htmlspecialchars($text))
http://php.net/manual/en/function.nl2br.php
<![CDATA[<br/>]]>
这对我来说很有用
如果在htmlspecialchars()
之后的字符串中获得<br>
标记,则意味着您的字符串中正好有<br>
标记(而不是'n
换行),因为htmlspecialchars()
不会将'n
转换为<br>
。
如果是这样,您必须进行从<br>
到'n
的双重转换,然后返回:
步骤1.将<br>
转换为'n
步骤2.运行htmlspecialchars()
步骤3.将'n
转换为<br>
示例:
<?php
echo
str_replace( // Step 3
"'n",
"<br />",
htmlspecialchars( // Step 2
str_replace( // Step 1
array("<br />", "<br/>", "<br>"), // make sure that you have your version of <br> tag here
"'n",
$text
),
ENT_QUOTES,
'UTF-8'
)
);
?>