facebook icon twitter icon angle down linkedin icon youtube icon
Home > Blog > remove-html-tags-php

How to remove HTML tags in PHP

In this article, I’m going to show how to remove HTML tags from string in PHP. Basically, you can perform this task easily using PHP built-in function strip_tags(). This function returns a string with all NULL bytes, HTML, and PHP tags stripped from a given string.

Basic syntax

strip_tags('string')

Or, if you want to allow specific tags

strip_tags('string','allowed_tags')

You can use two parameter here. But second parameter is optional.

Parameters Values:

String: It is a required parameter which specifies the string to check.

Allowed_tags: It is an optional parameter which specifies the allowable tags. The tags placed here will not be removed from the returned result.

To perform this simple operation you need to ensure PHP version 4+.

Ways to remove remove HTML tags using PHP

Just follow the examples below.

Example 1: Remove all HTML tags from the string

In this example, i passed a string through the strip_tags() function containing HTML tags. It checked the returned string whether all HTML tags are removed or not. All the HTML tags in the string are removed from the string by the strip_tags() function.

<?php
//input string
$string = '<a href="#">Hello world</a>! <b> I want to remove all tags</b> <i> from this string.</i>';
//remove all html tags
                $msg = strip_tags($string);
//show output with html tags
echo htmlentities($msg);
?>

Output:

Hello world! I want to remove all tags from this string.

Example 2: Remove all HTML tags except few

In this method, i specified the allowed_tags parameter along with the string parameter. So, we can allow a few tags to be existed in the string.

<?php
//input string
$string = '<a href="#">Hello world</a>! <b> I want to remove all tags</b> <i> from this string.</i>';
//remove all html tags except few                
$msg = strip_tags($string, '<i><b>');
//show output with html tags
echo htmlentities($msg);
?>

Output:

Hello world! <b> I want to remove specific tag</b> <i> from this string.</i>

Example 3: Remove specific HTML tags from string

You can use different methods to do this.

$string = '<a href="#">Hello world</a>!<br> <b> This is my example bold texts.</b> <br><i>This is my example itslic texts.</i>';
//remove particular html tags                
$msg = str_replace("<br>", "", $msg);

Or

//remove multiple html tags                
$msg = str_replace(array("<b>","</b>","<i>","</i>"), "", $string );

Or

//remove particular html tags with regex            
$msg = preg_replace("/<b.*?>(.*)?<\/b>/im","$1",$string);

Or

//remove particular html tag with attribute            
$msg = preg_replace("/<a\s(.+?)>(.+?)<\/a>/is", "$2", $string);

To see result clearly, you can use htmlentities() built-in function.

Tarikul islam

About Torikul islam

Torikul islam is a professional web developer and affiliate marketer. Join Torikul to learn how to start a website and operate it well. He started his Web Developement career from Bangladesh Association of Software and Information (BASIS) in 2015. Later he continiued his journey to expanding knowledge and sharing it with others.

Write a Comment

No comment yet