Home > Web > PHP >

PHP strip_tags And Standalone XHTML Elements

Posted Dec 11, 2006
Last Updated Nov 11, 2011

Over the last few months of developing Webonizer I came across a situation where the PHP strip_tags() function would not allow me to exclude the XHTML Break Element (<br/>) from the strip. In fact, I soon found that the quirk extended to the Horizontal Rule as well. After playing around with it, I found a way to exclude these elements from the strip results.

First it is important to note that this issue only appears to be for self-closing or standalone XHTML elements that do not require a closing tag. If it were not for the fact that the IMG element requires attributes, it would also need to following solution(s).

There are two solutions, but using both is probably best. First is to add a space before the ending forward slash in your actual tags. For example, if your content looked like this:

<p>I went to the store<br/>
To get some milk. </p>

You should change it to be:

<p>I went to the store<br />
To get some milk. </p>

The other solution is to add a slash (/) in your strip_tags() exclusion input. For example, you could enter the following:

$mystring = strip_tags($inputString, '<p><br><br/>');

Since XHTML requires standalone elements to be self-enclosed, you are supposed to add the forward slash—but the strip_tags function seems to fail on standalone tags where the forward slash does not have at least one space somewhere between it and the element itself--which is why you need to add a duplicate of a self-encolsed tag in the exclusion but add the forward slash.

2011-11-11 This big has been fixed in the latest versions of PHP.


No HTML Tags are permitted.

Wall Worm plugins and scripts for 3ds Max