22 Votes

PHP: Remove all HTML Tags from String

Tutorial by Stefan Trost | Last update on 2022-12-25 | Created on 2014-06-04

Sometimes, it is necessary to remove all HTML tags and formatting from a text. In other words, to extract the plain text from a formatted HTML document.

In this tutorial, I would like to show you, how you can do it. We are using the PHP function strip-tags() for this purpose.

Remove all of the Formatting

Let us look at a small example firs

$txt = "<p>This is a <b>text</b>.</p>";

$txt = strip_tags($txt);  // This is a text.

We are storing a string with some HTML formattings into the variable $txt. After we have applied strip_tags() to this text, all of the formatting is vanished.

Strip HTML Comments

Incidentally, strip_tags() is also deleting all HTML comments that are possibly located in the passed string.

$txt = "<p>AB<!-- Comment -->CD</p>";

$txt = strip_tags($txt);  // ABCD

As you can see in this example, only "ABCD" remains in the string.

Keep some of the Formatting

The second parameter of strip_tags() is optional. You can use this parameter to pass a collection of HTML tags that should not be deleted.

$txt = "<p>This is a <b>text</b>.</p>";

$txt = strip_tags($txt,'<p><h1>');  // <p>This is a text.</p>

In this example, we would like to keep paragraphs (<p>) and headings (<h1>) in the string while the rest should be sorted out. Accordingly, only the <b> tag will be removed in the example above.

Security Advice

Finally, I would like to make a few words about the security when using strip_tags(). It is not automatically ensured that you can use this function to make a user input "secure". For example, when allowing the <p> tag and someone is submitting something like <p onmouseover="do_evil_code">, this is not removed by strip_tags(). Generally, strip_tags() is not removing any attributes from allowed HTML tags.

Furthermore, it can cause problems when passing invalid, incorrect or incomplete HTML code to this function. Under circumstances, this can lead to more deleted text than intended.

You can read some more information about this in the answers to the question about how to allow only specific HTML code.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.