How to remove html tags from string using PHP

  • 14 July, 2018
  • Renish Khunt
  • 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.