How to remove html tags from string using PHP

How to remove HTML tags from string programmatically. let’s see the two easy way to remove HTML tags from the string using “strip_tags” and “preg_replace”. So let’s see how to remove HTML code from the string using “strip_tags”.

$string = strip_tags(“Hello <b>world!</b>);

We used the ‘strip_tags’ function for removing the HTML tags from the string. so it’s very easy to remove HTML using “strip_tags”. But the “strip_tags” add space after removing the tags from the string like.

$string = strip_tags(“Hello<b>world!</b>);

The output of the above statement is “Hello world!” instead of “Helloworld!”. so let’s see the another trick for removing only HTML tags without adding the space.

$string = preg_replace(#<[^>]+>#’, ‘ ‘, ‘Hello<b>world!</b>’);

The output of the above function is “Helloworld!” instead of “Hello world!”.

If you face any issue after using this code please write a comment below.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s