简体   繁体   English

如何在PHP中删除html标签?

[英]how to remove html tags in php?

i posted some data using tinymce (in a symfony project).while retrieving back how can i remove html tags? 我使用tinymce(在symfony项目中)发布了一些数据。而检索我如何删除html标签? strip_tags not working.. strip_tags不工作..

The easies way is to use strip_tags but it's not very reliable. 简单的方法是使用strip_tags但它不是很可靠。 There is a very, very, VERY good project design specifically for this: HTML Purifier . 有一个非常非常非常好的项目设计专门用于此: 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. strip_tags是简单,快速和顺利的方式,但它可能会错过浏览器实际解析和执行的一些格式错误的HTML。


Please, don't use regular expression to parse html! 请不要使用正则表达式解析html!

Note that strip_tags returns a new string. 请注意,strip_tags返回一个字符串。 It does not modify the original string, ie: 它不会修改原始字符串,即:

$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'

尝试这个:

echo strip_tags($this->getContent(ESC_RAW))

strip_tags(); 用strip_tags(); you need to put what ever your $ is wrapped with html. 你需要把你的$包含在html中。 ........... ...........

You could use strip_tags : 你可以使用strip_tags

strip_tags('your text or variable');

It should work in symfony. 它应该在symfony中工作 Make sure that you have done everything correctly. 确保您已正确完成所有操作。

When using Symfony, be sure to use the getRaw() function otherwise the text cannot be stripped from it's HTML code, for example: 使用Symfony时,请务必使用getRaw()函数,否则无法从HTML代码中删除文本,例如:

$myText = $sf_data->getRaw('myVarContainingText'); $ myText = $ sf_data-> getRaw('myVarContainingText');

Then use strip_tags() as such: 然后使用strip_tags():

$myText = strip_tags( $sf_data->getRaw('myVarContainingText') ); $ myText = strip_tags($ sf_data-> getRaw('myVarContainingText'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM