How to Remove or Keep HTML Tags with PHP strip_tags()
PHP’s strip_tags() returns a string with HTML and PHP tags removed. Pass a second argument to retain selected HTML tags. The attributes of retained tags remain, so choose a different method when you need to escape text or sanitize user-supplied HTML.
Remove all tags or keep selected tags
Pass just the input string to the PHP manual’s strip_tags() function to remove all tags. To retain one tag, pass a string such as '<strong>'. For several tags in a string, put the names together as in '<strong><em>'. PHP 7.4 and later also accept an array such as ['a'].
Save the code blocks as separate PHP files and run them with PHP 8 from a terminal, for instance with php strip.php. The table shows the output of the first block in order.
<?php
$html = '<p>Lorem <strong>Ipsum</strong></p>';
echo strip_tags($html), PHP_EOL;
echo strip_tags($html, '<strong>'), PHP_EOL;
$link = '<p>Lorem <a href="amet" title="Details">Ipsum</a> Dolor.</p>';
echo strip_tags($link, ['a']), PHP_EOL;
| Operation | Returned string |
|---|---|
| Remove all tags | Lorem Ipsum |
| Keep only strong | Lorem <strong>Ipsum</strong> |
| Keep only a | Lorem <a href="amet" title="Details">Ipsum</a> Dolor. |
In the third result, the p tag is gone and the a tag keeps its href and title attributes. The allowlist specifies tag names.
Check whitespace, comments, and entities
strip_tags() adds no space or newline where it removes a paragraph tag. It removes HTML comments, and text inside a removed tag such as style can remain.
<?php
echo strip_tags('<p>First</p><p>Second</p>'), PHP_EOL;
echo strip_tags('First<!-- note -->Second'), PHP_EOL;
echo strip_tags('<style>p { color: green; }</style><p>Text</p>'), PHP_EOL;
echo strip_tags('<p>A & B</p>'), PHP_EOL;
The first and second lines are both FirstSecond. The third is p { color: green; }Text. Use an HTML parser when you need to preserve paragraph boundaries or remove an element and its contents.
The last line still contains &. Removing tags and decoding HTML entities are separate operations.
Display text and literal tags in a browser
To turn entities into ordinary characters for plain text, use html_entity_decode() after stripping the tags. Before inserting the resulting text into the body of an HTML document, escape it with htmlspecialchars(). To show the original tags literally, call htmlspecialchars() on the original string.
<?php
$html = '<p>A & B</p>';
$text = html_entity_decode(strip_tags($html), ENT_QUOTES | ENT_HTML5, 'UTF-8');
echo htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'), PHP_EOL;
echo htmlspecialchars('<strong>Ipsum</strong>', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'), PHP_EOL;
The first line is A & B in the HTML source and appears as A & B in the browser. The second appears as the literal text <strong>Ipsum</strong>.
Choose how to handle user input
For plain user text in an HTML body, escape it when producing the output. If you need to preserve some formatting in user-supplied HTML, use an HTML sanitizer that checks tags, attributes, and link destinations.
Use strip_tags() for a simple excerpt from HTML you already trust. Malformed tags can cause unexpected text loss, so check the output with representative inputs.



