Friday, February 6, 2009

how to remove html tags from text in php

if you are reading my post, its probably because you want to remove html tags from your posts or forums or whatever.

when i first started to do this, i wanted to remove all the html tags from a strin i had in my posts. this is an example:
<?
$string = "<h1>BIG HTML TAGS</h1>";
?>

ok as you can see, this will output:

BIG HTML TAGS


but i dont want that, i want to stop and prevent html from being posted on my forums. i tried using htmlspecialchars() and htmlentities() but none did what i wanted.

but finally i found it, the function is called strip_tags()

so if i wanted to remove the <h1> tags from my string i would just simply do this:
<?
$string = "<h1>BIG HTML TAGS</h1>";
$string = strip_tags($string);
?>

and this would be the ouput:
BIG HTML TAGS

No comments: