Friday, October 24, 2025
HomeLanguagesHow to remove HTML tags from data in PHP ?

How to remove HTML tags from data in PHP ?

In this article, we will see how to remove HTML tags from data in PHP. PHP provides an inbuilt function to remove the HTML tags from the data. The strip_tags() function is an inbuilt function in PHP that removes the strings form HTML, XML and PHP tags. It accepts two parameters. This function returns a string with all NULL bytes, HTML, and PHP tags stripped from a given $str.

Syntax:

strip_tags(string, allowed_tags)

Parameters Values:

  • string: It is a required parameter that specifies the string to check.
  • allowed_tags: It is an optional parameter that specifies the allowable tags which will not be removed from the returned result.

Return Value: It returns a string where HTML tags are removed except for the allowed tags.

Example 1: In this example, we passed a string containing HTML tags to the strip_tags() function and checked the returned string whether all HTML tags are removed or not.  All the HTML tags in the string are stripped from the string by the strip_tags() function.

PHP




<?php
    echo strip_tags(
          "<b>neveropen</b> one of the popular 
            <i>online learning site</i>");
?>


Output:

 

Example 2: In this code, we specified the allowed_tags parameter along with the string to strip_tags() method, so that we can allow a few tags in the string and strip the unallowed tags from the input string. In the allowed_tags section, we specified <h1> tag. So it did not strip the <h1> tag in the string and stripped the rest of the other tags i.e. <i> italic tag.

PHP




<?php
    echo strip_tags(
  "<h1>neveropen</h1> one of the top 
          <i>Online learning platform</i>","
   <h1>");
?>


Output:

 

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS