Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove html tags in php?

Tags:

php

symfony1

i posted some data using tinymce (in a symfony project).while retrieving back how can i remove html tags? strip_tags not working..

like image 670
user288640 Avatar asked Mar 12 '10 10:03

user288640


2 Answers

The easies way is to use strip_tags but it's not very reliable. There is a very, very, VERY good project design specifically for this: HTML Purifier.

It battle-hardened, tested and very good. strip_tags is the easy, fast and go way, but it can miss out some malformated html that a browser will actually parse and execute.


Please, don't use regular expression to parse html!

like image 74
Emil Ivanov Avatar answered Oct 05 '22 11:10

Emil Ivanov


Note that strip_tags returns a new string. It does not modify the original string, i.e:

$html = '<p>Test</p>';
strip_tags($html); // Throws away the result, since you don't assign the return 
                   // value of the function to a variable

$stripped = strip_tags($html);
echo $stripped; // echos 'Test'
like image 29
PatrikAkerstrand Avatar answered Oct 05 '22 13:10

PatrikAkerstrand