Current Location: Home> Latest Articles> What is the strip_tags Function? Learn How to Easily Remove HTML Tags with strip_tags

What is the strip_tags Function? Learn How to Easily Remove HTML Tags with strip_tags

gitbox 2025-09-04

In PHP, the strip_tags() function is a commonly used tool that helps you remove all HTML and PHP tags from a string, leaving only plain text. This is especially important when handling data from user input or external sources, ensuring data security and readability while preventing malicious script injection or excessive HTML tags from affecting page display.

1. Basic Syntax of strip_tags()

The basic syntax of strip_tags() is as follows:

<span><span><span class="hljs-title function_ invoke__">strip_tags</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$str</span></span>, </span><span><span class="hljs-keyword">string</span></span>|</span><span><span class="hljs-literal">null</span></span> </span><span><span class="hljs-variable">$allowed_tags</span></span> = </span><span><span class="hljs-literal">null</span></span>): </span><span><span class="hljs-keyword">string</span></span><span>
</span></span>
  • $str: The input string from which you want to remove HTML tags.

  • $allowed_tags: This is an optional parameter that lets you preserve certain HTML tags. If you want to keep specific tags (such as or ), you can specify them here. If not specified, the default is null, meaning all tags will be removed.

2. Example: Basic Usage

Here’s a simple example demonstrating how to use strip_tags() to remove HTML tags:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$string</span></span> = </span><span><span class="hljs-string">"&lt;p&gt;Hello &lt;b&gt;World&lt;/b&gt;!&lt;/p&gt;"</span></span>;
</span><span><span class="hljs-variable">$clean_string</span></span> = </span><span><span class="hljs-title function_ invoke__">strip_tags</span></span>(</span><span><span class="hljs-variable">$string</span></span>);
</span><span><span class="hljs-keyword">echo</span></span> </span><span><span class="hljs-variable">$clean_string</span></span>;
</span><span><span class="hljs-meta">?&gt;</span></span><span>
</span></span>

The output will be:

<span><span><span class="hljs-attribute">Hello</span></span><span> World!
</span></span>

In this example, strip_tags() removed the

and tags, leaving only the plain text “Hello World!”

3. Example: Preserving Specific Tags

If you want to keep some tags while stripping others, you can use the $allowed_tags parameter. For example, keeping the and tags:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$string</span></span> = </span><span><span class="hljs-string">"&lt;p&gt;This is &lt;b&gt;bold&lt;/b&gt; and &lt;i&gt;italic&lt;/i&gt; text.&lt;/p&gt;"</span></span>;
</span><span><span class="hljs-variable">$clean_string</span></span> = </span><span><span class="hljs-title function_ invoke__">strip_tags</span></span>(</span><span><span class="hljs-variable">$string</span></span>, </span><span><span class="hljs-string">"&lt;b&gt;&lt;i&gt;"</span></span>);
</span><span><span class="hljs-keyword">echo</span></span> </span><span><span class="hljs-variable">$clean_string</span></span>;
</span><span><span class="hljs-meta">?&gt;</span></span><span>
</span></span>

The output will be:

<span><span>This is &lt;</span><span><span class="hljs-selector-tag">b</span></span><span>&gt;bold&lt;/</span><span><span class="hljs-selector-tag">b</span></span><span>&gt; and &lt;</span><span><span class="hljs-selector-tag">i</span></span><span>&gt;italic&lt;/</span><span><span class="hljs-selector-tag">i</span></span><span>&gt; text.
</span></span>

In this example, the and tags are preserved, while other tags (like

) are removed.

4. Common Use Cases

  1. User Input Filtering: When users submit text through a form, it may include unnecessary HTML tags. Using strip_tags() helps clean up unwanted tags and prevent potential XSS attacks.

  2. Data Cleaning: When retrieving HTML content from a database or external API, you may need to strip out tags before further processing or displaying the content.

  3. Text Display: Sometimes, when showing user comments or other content, only plain text is needed without HTML formatting. In such cases, strip_tags() is very useful.

5. Things to Note

  • The strip_tags() function cannot handle certain HTML elements or nested tags effectively. For example, if the HTML contains nested tags, strip_tags() will only remove the outer tags without affecting the inner ones.

  • When dealing with HTML containing