Remove HTML tags from a string in PHP
In this short article, we will explain to you how to remove HTML tags from a string in PHP. Sometimes we need to get only the content part without HTML tags so today we will show you a simple way to remove HTML tags.
Here we will use two different PHP methods such as predefined method and user defined method to remove the HTML tags.
Ways to remove HTML tags from a string in PHP
1. Using built-in function
We have built-in php function strip_tags() to remove HTML tags from a string.
Syntax
1 | strip_tags ( $str [, $allowable_tags ] ) |
Here, $str
parameter is required and $allowable_tags
parameter is optional. Therefore the $allowable_tags
are not removed from the string. You can also pass multiple tags together as a string.
Example 1:
Let’s take a simple example where we will pass only required parameters into the strip_tags()
function.
1 2 3 4 5 6 7 8 9 10 | <?php $htmlString = '<p>Welcome to <a href="https://www.cluemediator.com" target="_blank" rel="noopener noreferrer">Clue Mediator</a><br> - The way to write your code</p>'; $content = strip_tags($htmlString); echo htmlentities($content); // Output: // Welcome to Clue Mediator - The way to write your code ?> |
Example 2:
In the second example, if we want to ignore the <a>
tag from the string and remove the rest of all the HTML tags then pass the <a>
tag in the second argument of the strip_tags()
php function.
1 2 3 4 5 6 7 8 9 10 | <?php $htmlString = '<p>Welcome to <a href="https://www.cluemediator.com" target="_blank" rel="noopener noreferrer">Clue Mediator</a><br> - The way to write your code</p>'; $content = strip_tags($htmlString, '<a>'); echo htmlentities($content); // Output: // Welcome to <a href="https://www.cluemediator.com" target="_blank" rel="noopener noreferrer">Clue Mediator</a> - The way to write your code ?> |
Example 3:
If you want to ignore the multiple HTML tags during the removal from the string then pass the all tags (i.e. <a><br>
) in a single argument as a string.
Check out the below code where we have passed the <a><br>
tags in the second argument of the strip_tags()
function.
1 2 3 4 5 6 7 8 9 10 11 12 | <?php $htmlString = '<p>Welcome to <a href="https://www.cluemediator.com" target="_blank" rel="noopener noreferrer">Clue Mediator</a><br> - The way to write your code</p>'; $content = strip_tags($htmlString, '<a><br>'); // As of PHP 7.4.0 the line above can be written as: // $content = strip_tags($htmlString, ['a', 'br']); echo htmlentities($content); // Output: // Welcome to <a href="https://www.cluemediator.com" target="_blank" rel="noopener noreferrer">Clue Mediator</a><br> - The way to write your code ?> |
2. Using custom function
Here we have created a custom function to remove HTML tags which you want. This function will work a little differently than the built-in php function. It will remove all the HTML tags which we will pass as arguments.
Check out the following code for more understanding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php function removeTags($htmlString, $htmlTags) { $tagString = ""; foreach($htmlTags as $key => $value) { $tagString .= $key == count($htmlTags)-1 ? $value : "{$value}|"; } $pattern= array("/(<\s*\b({$tagString})\b[^>]*>)/i", "/(<\/\s*\b({$tagString})\b\s*>)/i"); $result = preg_replace($pattern, "", $htmlString); return $result; } $htmlString = '<html><body><p>Welcome to <a href="https://www.cluemediator.com" target="_blank" rel="noopener noreferrer">Clue Mediator</a><br> - The way to write your code</p></body></html>'; $content = removeTags($htmlString, array("html", "body", "a", "b", "p")); echo htmlentities($content); // Output: // Welcome to Clue Mediator<br> - The way to write your code ?> |
That’s it for today.
Thank you for reading. Happy Coding..!!