从字符串中去除所有html标签的最佳方式是什么? [英] What is the best way to strip out all html tags from a string?

查看:451
本文介绍了从字符串中去除所有html标签的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHP,给定一个字符串,例如:,这是一个< strong>字符串< / strong> ;我需要一个函数去掉所有的html标签,以便输出结果是:这是一个字符串。有任何想法吗?感谢您的提前。 解决方案

PHP有一个内置函数, //www.php.net/strip_tagsrel =nofollow noreferrer> strip_tags

  $ text ='< b>你好< / b>世界'; 
print strip_tags($ text); //输出Hello World

如果您希望断开HTML,您将需要将它加载到DOM解析器,然后提取文本。


Using PHP, given a string such as: this is a <strong>string</strong>; I need a function to strip out ALL html tags so that the output is: this is a string. Any ideas? Thanks in advance.

解决方案

PHP has a built-in function that does exactly what you want: strip_tags

$text = '<b>Hello</b> World';
print strip_tags($text); // outputs Hello World

If you expect broken HTML, you are going to need to load it into a DOM parser and then extract the text.

这篇关于从字符串中去除所有html标签的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆