How to remove HTML tags from a string
Short answer
strip_tags() leaves only the text:
<?php
echo strip_tags('<p>Hello, <b>world</b>!</p>');Output:
Hello, world!Keeping some tags
The allowed tags go in the second argument:
<?php
echo strip_tags('<p>Hello, <b>world</b>!</p>', ['b']);Output:
Hello, <b>world</b>!Removing or escaping
If the text has to be shown as is, angle brackets included, escape the tags instead of removing them:
<?php
echo htmlspecialchars('<b>bold</b>');Output:
<b>bold</b>Functions used in this recipe
- strip_tags — Removes HTML tags from a string.
- htmlspecialchars — Replaces HTML special characters with safe entities.
- html_entity_decode — Turns HTML entities back into characters.