简体   繁体   English

转义html标签并仅读取php中的文本?

[英]escaping html tags and reading only the text in php?

I was wondering is there a way to escape all html tags from aa bunch of html code, and extract only the text ie 我想知道是否有一种方法可以从一堆html代码中转义所有html标签,并仅提取文本,即

<strong>this is strong</strong>
<br />
<h1> this is a header</h1>

and I want to get the text between the tags only, so basically only this is strong this is a header 而且我只想获取标签之间的文本,所以基本上只有this is strong this is a header

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);

One of the things I love about PHP...thoughtful functions for web developers. 我喜欢PHP的一件事是……针对Web开发人员的周到功能。

use strip_tags function 使用strip_tags函数

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);

will output 将输出

Test paragraph. 测试段落。 Other text 其他文字

要剥离整个HTML块, http://www.phpro.org/examples/Get-Text-Between-Tags.html具有一些不错的功能和示例。

Use strip_tags() like this: 像这样使用strip_tags()

$html = "<strong>this is strong</strong><br />\n".
        "<h1> this is a header</h1>";
$text = strip_tags($html);

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

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