Skip to content Skip to sidebar Skip to footer

What Is The Best Way To Strip Out All Html Tags From A String?

Using PHP, given a string such as: this is a string; I need a function to strip out ALL html tags so that the output is: this is a string. Any ideas?

Solution 1:

PHP has a built-in function that does exactly what you want: strip_tags

$text = '<b>Hello</b> World';
print strip_tags($text); // outputs Hello World

If you expect broken HTML, you are going to need to load it into a DOM parser and then extract the text.

Solution 2:

What about using strip_tags, which should do just the job ?

For instance (quoting the doc) :

<?php$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);
echo"\n";

will give you :

Test paragraph. Other text

Edit : but note that strip_tags doesn't validate what you give it. Which means that this code :

$text = "this is <10 a test";
var_dump(strip_tags($text));

Will get you :

string'this is ' (length=8)

(Everything after the thing that looks like a starting tag gets removed).

Solution 3:

strip_tags is the function you're after. You'd use it something like this

$text = '<strong>Strong</strong>';
$text = strip_tags($text);
// Now $text = 'Strong'

Solution 4:

I find this to be a little more effective than strip_tags() alone, since strip_tags() will not zap javascript or css:

$search = array(
    "'<head[^>]*?>.*?</head>'si",
    "'<script[^>]*?>.*?</script>'si",
    "'<style[^>]*?>.*?</style>'si",
);
$replace = array("","",""); 
$text = strip_tags(preg_replace($search, $replace, $html));

Post a Comment for "What Is The Best Way To Strip Out All Html Tags From A String?"