PHP 函数 nl2br 中的奇怪行为
我正在处理我的小型网络应用程序的重构。整个晚上。今天,当我开始测试时,我发现的第一个错误是系统 PHP 函数 nl2br()
的问题。
在我的本地主机上,我有 PHP 版本 5.2.9 ,正如我在 PHP 网站版本 4.0.5 nl2br()
兼容 XHTML。
然后我完全不明白为什么我的 nl2br()
返回
而没有将第二个参数设置为 false 而不是
< /代码>。
这是我发现这个错误的方法:
public function eliminateTags($msg) {
$setBrakes = nl2br($msg);
$decodeHTML = htmlspecialchars_decode($setBrakes);
# Check PHP version
if((int)version_compare(PHP_VERSION, '4.0.5') == 1) {
$withoutTags = strip_tags($decodeHTML, '<br />');
} else {
$withoutTags = strip_tags($decodeHTML, '<br>');
}
return $withoutTags;
}
I was dealing with refactoring of my small web app. all night. Today when I started testing, first bug what I found was problem with system PHP function nl2br()
.
On my localhost I have PHP version 5.2.9 and as I see on PHP site from version 4.0.5 nl2br()
is XHTML compliant.
Then I absolutely don't understand why does my nl2br()
return <br>
without second argument set to false instead of <br />
.
Here is my method where I found this bug:
public function eliminateTags($msg) {
$setBrakes = nl2br($msg);
$decodeHTML = htmlspecialchars_decode($setBrakes);
# Check PHP version
if((int)version_compare(PHP_VERSION, '4.0.5') == 1) {
$withoutTags = strip_tags($decodeHTML, '<br />');
} else {
$withoutTags = strip_tags($decodeHTML, '<br>');
}
return $withoutTags;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我是否理解你想用这个功能来完成什么。首先,在每个新行中插入 HTML 换行符,然后删除除换行符之外的所有标记。
先去掉标签然后插入 HTML 换行符不是更明智吗?
编辑:
显然您没有正确使用
strip_tags()
。您需要询问 PHP 要排除哪个标记,即
,而不是
。要求 PHP 排除
就像要求排除一样,这是行不通的。
这又意味着您不必检查 PHP 版本 -
strip_tags()
将在所有版本中正常工作。I'm not sure I understand what you're trying to accomplish with this function. First of all you insert HTML breaks in every new line and then you strip all tags except the breaks.
Wouldn't it be more sensible to strip the tags first and then insert the HTML line breaks?
Edit:
Apparently you are not using
strip_tags()
correctly. You need to ask PHP which tag to exclude, which is<br>
, not<br />
. Asking PHP to exclude<br />
is like asking to exclude, say,<p></p>
which won't work.Which in turn means you must not check for a PHP version -
strip_tags()
will work as is in all versions.