Encodera

Thanks for reading!

Donate to the site — we work hard for you.

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:

&lt;b&gt;bold&lt;/b&gt;

Functions used in this recipe

Thanks for reading!

Donate to the site — we work hard for you.