PHP Strip HTML Tutorial-How to remove html entities in the PHP output?


        This is an important tutorial about stripping HTML tags in PHP with sample code. Sometimes you encountered a situation when you are outputting a text into a document say RSS feeds or PDF. And the content (text) that is being outputted contains html entities for example: & nbsp ;

       Common practice of removing html tags in PHP is using the function: strip_tags, however this function will not remove html entities; as a result html entities in being outputted to html resulting in gibberish text.

       PHP function to handle the removal of html tags will usually be in the form:


$htmlfreecontent = strip_tags($content);

       To remove html entities in PHP is to use the function: html_entity_decode

       So this function can be integrate to the above resulting to:

$htmlfreecontent = strip_tags(html_entity_decode($content));

       As a result, any html entities will be converted to their applicable characters.



Related posts: