PHP Reference ยท String Functions
PHP strip_tags() function
Reference for PHP strip_tags(): remove HTML and PHP tags from a string, keep an allowed list of tags, and why it is not a substitute for escaping.
strip_tags() removes HTML tags, HTML comments and PHP tags from a string and returns what is left. Text between the tags is kept, which means the content of a <script> or <style> element stays behind as plain text. You can pass a list of tags that should survive.
It is handy for producing a plain-text preview of rich text, such as a short excerpt for a search result or an email subject. It is not an HTML sanitizer: it works with a simple tokenizer, not a real HTML parser, so it must not be your only defense when you accept markup from users.
<?php echo strip_tags("<p>Hello <b>world</b></p>"); // Hello world
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
strip_tags(string $string, array|string|null $allowed_tags = null): string
| Name | Description |
|---|---|
$string |
The input text. |
$allowed_tags |
Tags to keep. Either a string like "<p><a>" or, since PHP 7.4, an array such as ["p", "a"]. |
Return value: the string without tags.
Usage notes
- Text inside
<script>and<style>stays. Only the tags go. Remove those elements yourself (for example with a regular expression orDOMDocument) if you do not want their content. - Allowed tags keep their attributes.
onclick,styleandhref="javascript:..."pass through untouched, so allow-lists are not safe for untrusted input. Use a dedicated HTML sanitizer library, or escape everything withhtmlspecialchars(). - It does not decode entities.
<b>remains as typed. Combine withhtml_entity_decode()if you need plain text. - Unusual input. A lone
<followed by a space or a digit is kept, but a<immediately followed by a letter starts a tag, and an unclosed tag swallows the rest of the text. Inputs likea<bcan lose data. - Spacing. Removing a tag can glue words together (
"one<br>two"becomesonetwo). Replace tags with spaces first when that matters. - PHP tags (
<?php ... ?>) and comments are also removed. - Not a security boundary. Treat it as a convenience for display text, never as the thing that makes input safe.