strip_tags
Removes HTML tags from a string.
Syntax
strip_tags(string $string, array|string|null $allowed_tags = null): stringParameters
| Parameter | Type | Description |
|---|---|---|
| $string | string | The source string. |
| $allowed_tags | array|string|null | Optional. The tags to keep. |
Return value
stringThe string without tags.
Examples
Text only:
<?php
echo strip_tags('<p>Hello, <b>world</b>!</p>');Output:
Hello, world!Allowed tags stay:
<?php
echo strip_tags('<p>Hello, <b>world</b>!</p>', ['b']);Output:
Hello, <b>world</b>!Notes
The function does not protect against XSS: the attributes of allowed tags stay in place. Use htmlspecialchars() to print user text.
See also
- htmlspecialchars — Replaces HTML special characters with safe entities.
- trim — Removes whitespace and other characters from both ends of a string.
Related recipes
In the tutorial
- Security — The basics of PHP site security — XSS and escaping, SQL injection, password hashing, CSRF tokens and validating input.