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;
}
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?
public function eliminateTags($msg) {
$decodeHTML = htmlspecialchars_decode($msg);
$withoutTags = strip_tags($decodeHTML);
$setBreaks = nl2br($withoutTags);
return $setBreaks;
}
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.
精彩评论