Using PHP strip_tags() to Remove HTML Tags

The article explains PHP's strip_tags() function, its syntax, basic usage, how to preserve specific or multiple HTML tags, how to remove all tags, and important notes for safely cleaning user input in backend development.

php Courses
php Courses
php Courses
Using PHP strip_tags() to Remove HTML Tags

PHP's strip_tags() function removes HTML tags from a string, returning plain text, which helps prevent malicious code injection.

Syntax:

string strip_tags ( string $str [, string $allowable_tags ] )

, where $str is the input string and $allowable_tags optionally specifies tags to keep.

Basic usage: calling strip_tags($str) on a string like " Hello world! " outputs "Hello, world!".

To preserve specific tags, provide them in $allowable_tags, e.g., strip_tags($str, "<b><i>") keeps and tags.

Multiple tags can be listed, such as strip_tags($str, "<b><i><a>"), which retains bold, italic, and anchor tags while stripping others.

Setting $allowable_tags to an empty string removes all tags.

Notes: the function strips all HTML, including script and style tags; attributes can be preserved by including them in $allowable_tags; a wildcard "<>" keeps all tags; for selective removal, preg_replace() with regex may be used.

In summary, strip_tags() provides a simple way to clean user input by stripping unwanted HTML tags while optionally preserving desired ones.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Securityphp-functionshtml-removalstrip_tags
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.