How to remove specific HTML tags from string in PHP
Last Updated on Dec 4, 2022 - Written By Torikul Islam
In this article, I’m going to remove specific HTML tags from a string using PHP built-in str_replace() or preg_replace() function.
A String set up with a collection of different characters. String could contain HTML tags. Those may seems hidden, but can make impact on output result.
Basic Syntax:
str_replace(‘substring ’, ‘replaced_string’, ‘original_string’);
Or
preg_replace(‘substring ’, ‘replaced_string’, ‘original_string’);
We need to provide three parameters.
1. Substring is the first parameter which should be replaced with replaced_string.
2. Replaced_string will replace the substring
3. Original_string will contain the full string from where you want to remove HTML tags.
In the placement of parameters, you can use PHP variables.
Ways to remove particular HTML tags from string
If you want to remove specific HTML tags, but not the all tags, you can use str_replace() or preg_replace() function in following ways.
Approach 1: Remove a single HTML tag
This process is very simple. You can simply replace the tag with blank string.
<?php
//input string
$string = '<a href="#">Hello world</a>!<br> <b> I want to remove br tag</b> .<br><i>But not this one.</i>';
//remove particular html tags
$msg = str_replace("<br>", "", $string);
//show output with html tags
echo htmlentities($msg);
?>
Output:
<a href="#">Hello world</a>! <b> I want to remove br tag</b> .<i>But not this one.</i>
Approach 2: Remove multiple HTML tags
You need to include array function to remove multiple HTML tags.
<?php
//input string
$string = '<a href="#">Hello world</a>!<br> <b> I want to remove this tag.</b> <br><i>And aslo this one.</i>';
//remove multiple html tags
$msg = str_replace(array("<b>","</b>","<i>","</i>"), "", $string );
//show output with html tags
echo htmlentities($msg);
?>
Output:
<a href="#">Hello world</a>!<br> I want to remove this tag.<br>And aslo this one.
Approach 3: Use RegEX method
I don’t recommend you to use this method, but you can use.
<?php
//input string
$string = '<a href="#">Hello world</a>!<br> <b> I want to remove this tag</b> .<br><i>But not this one.</i>';
//remove particular html tags with regex
$msg = preg_replace("/<b.*?>(.*)?<\/b>/im","$1",$string);
//show output with html tags
echo htmlentities($msg);
?>
Output:
<a href="#">Hello world</a>!<br> I want to remove this tag .<br><i>But not this one.</i>
But not any approaches will work if HTML tag contain any attribute. In such case, you should follow the strategy below.
Approach 4: Remove HTML tag which contain attributes
In this method, all attribute within the HTML tag will be removed with the tag itself.
<?php
//input string
$string = '<a href="#">Hello world</a>!<br> <b> I want to remove this tag</b> .<br><i>But not this one.</i>';
//remove particular html tag with attribute
$msg = preg_replace("/<a\s(.+?)>(.+?)<\/a>/is", "$2", $string);
//show output with html tags
echo htmlentities($msg);
?>
Output:
Hello world!<br> <b> I want to remove this tag</b> .<br><i>But not this one.</i>
Note: htmlentities() is a PHP built-in function, that allows you to show string with HTML codes. Because by default, HTML codes are hidden. It is not mandatory to use this function. I just used it to more clarify the outputs.