PHP has a function called strip_tags which strips all HTML and PHP tags from a string. It’s useful when you need filter submitted content from user, show a short description (excerpt) or try to search content from a string. Plus, it allows to specify which tags should not be stripped to make sure you have more options for the removing.
I usually use this function when I want to parse content from an external resource. Pull the content, remove all unnecessary strings then get most important needed strings.
PHP Remove All HTML Tags
Let check an example as below:
[php highlight=”10,12″] <?php$str = "";
$str .= "<h1>This a header. </h1>";
$str .= "<p>This a paragraph. </p>";
$str .= "<table>";
$str .= "<tr><td>This is a HTML table with 1 column & 2 rows. </td></tr>";
$str .= "<tr><td>This is a <strong>bold</strong> text. </td></tr>";
$str .= "</table>";
echo strip_tags($str) . "\r\n";
echo strip_tags($str,"<h1><p>");
?>
[/php]
Output:
[text]This a header. This a paragraph. This is a HTML table with 1 column & 2 rows. This is a bold text.
<h1>This a header. </h1><p>This a paragraph. </p>This is a HTML table with 1 column & 2 rows. This is a bold text.
[/text]The strip_tags on line 10, all HTML tags are removed. And strip_tags on line 12, all HTML tags are removed except tags: <h1> and <p>.