ed-ta
ed-ta

Reputation: 373

PHP: Does strip_tags() strip self closing XHTML tags or not?

PHP manual says

5.3.4 strip_tags() no longer strips self-closing XHTML tags unless the self-closing XHTML tag is also given in allowable_tags.

But when I do this (5.5):

$text = "<base/><br/>World<hr><input/>";

echo strip_tags($text);

The output is World i.e it does strip self closing XHTML tags.

Upvotes: 2

Views: 1182

Answers (1)

PeeHaa
PeeHaa

Reputation: 72672

The docs in this case are simply wrong.

strip_tags() without any allowed tags strips self closing tags just fine. What is changed however is that as of >= 5.3.4 self closing tags are ignored:

$string = "foo<br>bar<br/>baz<br />\r\n";

echo strip_tags($string); // foobarbaz
echo strip_tags($string, '<br>'); // foo<br>bar<br/>baz<br />
echo strip_tags($string, '<br/>'); // foobarbaz
echo strip_tags($string, '<br />'); // foobarbaz
echo strip_tags($string, '<br><br/>'); // foo<br>bar<br/>baz<br/>

I've created a bug for it to fix it in the docs.

The docs are updated to reflect the actual correct behavior.

However the fact that self closing and "normal" tags need to be defined separately seem odd to me at first, so maybe that needs to be fixed as well.

When reading the original bug report it is not entirely clear to me what has been fixed and what the expected output would be from that so I need to investigate a bit more.

My initial assumption that both self-closing as well as non-self-closing tags should be added was wrong.

Upvotes: 5

Related Questions