How to replace HTML tags using PHP

To replace HTML tags using, you can use PHP built-in functions.

In this article, I’m going to replace specific HTML tags from a string using str_replace() or preg_replace() function.

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 replace particular HTML tags from string

If you want to replace a specific HTML tag with another one, you can use str_replace() or preg_replace() function in following ways.

Approach 1: Simply replace a HTML tag

You can simply replace a HTML tag with another one if the particular tag don’t contain any attribute. In this process you have to replace starting and ending tags seperately.

<?php
//input string
$string = '<a href="#">Hello world</a>!<br> <b> I want to replace this tag.</b> ';
//replace particular html tag
                $msg = str_replace("<b>", "<i>", $msg);
$msg = str_replace("</b>", "</i>", $msg);
//show output with html tags
echo htmlentities($msg);
?>

Output:

<a href="#">Hello world</a>!<br> <i> I want to replace this tag.</i>

But it seems unfrofessional, and you won’t be able to replace multiple tags simultaneously. For this, you need to apply array() function.

Approach 2: Replace multiple HTML tags using array

In this method, you can replace multiple HTML tags at once. You need to organize the starting and ending tags sequentially.

<?php
//input string
$string = '<a href="#">Hello world</a>!<br> <b> I want to replace this tag</b>.<br> <p> And also this one.</p>';
//replace particular html tags                
$msg = str_replace(array("<b>","</b>","<p>","</p>"), array("<i>","</i>","<i>","</i>"), $string );
//show output with html tags
echo htmlentities($msg);
?>

Output:

<a href="#">Hello world</a>!<br> <i> I want to replace this tag</i>.<br> <i> And also this one.</i>

But this method also don’t allow you to replace a tag with attribute. In this case, you need to apply regex method.

Approach 3: Replace HTML tag using RegEX

This method will allow you to eleminate attributes within the HTML tag. Because this system select a part from string, then copy the text and replace that whole matched string with new tag.

<?php
//input string
$string = '<a href="#">Hello world</a>!<br> <b> I don’t want to replace this tag.</b>';
//replace particular html tag                
$msg = preg_replace("/<a\s(.+?)>(.+?)<\/a>/is", "<b>$2</b>", $string);
//show output with html tags
echo htmlentities($msg);
?>

Output:

<b>Hello world</b>!<br> <b> I want to replace this tag.</b>

Note: htmlentities() is a PHP built-in function, that allows you to show string with HTML codes which are hidden on browser output. It is not mandatory to use this function. I just used it to more clarify the result outputs.