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.
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.
Here’s a simple example demonstrating how to use strip_tags() to remove HTML tags:
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-variable">$string</span></span> = </span><span><span class="hljs-string">"<p>Hello <b>World</b>!</p>"</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">?></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!”
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"><?php</span></span><span>
</span><span><span class="hljs-variable">$string</span></span> = </span><span><span class="hljs-string">"<p>This is <b>bold</b> and <i>italic</i> text.</p>"</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">"<b><i>"</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">?></span></span><span>
</span></span>
The output will be:
<span><span>This is <</span><span><span class="hljs-selector-tag">b</span></span><span>>bold</</span><span><span class="hljs-selector-tag">b</span></span><span>> and <</span><span><span class="hljs-selector-tag">i</span></span><span>>italic</</span><span><span class="hljs-selector-tag">i</span></span><span>> text.
</span></span>
In this example, the and tags are preserved, while other tags (like ) are removed.
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.
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.
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.
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 or tags, although strip_tags() removes most tags, you may still need additional methods, such as regular expressions, to ensure scripts or styles are fully removed.
strip_tags() is a highly practical PHP function that helps remove HTML tags from strings, leaving only plain text. It is an essential tool for data filtering, sanitizing user input, or displaying plain content. With its simple usage and optional allowed_tags parameter, you can flexibly control which tags to keep, ensuring your webpage displays exactly as intended.
Related Tags:
HTML