我想只从php字符串中提取文本。
这个php字符串包含html代码,如标签等。
所以我只需要这个字符串中的简单文本。
这是实际的字符串:
<div class="devblog-index-content battlelog-wordpress">
<p><strong>The celebration of the Recon class in our second </strong><a href="http://blogs.battlefield.com/2014/10/bf4-class-week-recon/" target="_blank">BF4 Class Week</a><strong> continues with a sneaky stroll down memory lane. Learn more about how the Recon has changed in appearance, name and weaponry over the years…</strong></p>
<p> </p>
<p style="text-align:center"><a href="http://eaassets-a.akamaihd.net/battlelog/prod/954660ddbe53df808c23a0ba948e7971/en_US/blog/wp-content/uploads/2014/10/bf4-history-of-recon-1.jpg?v=1412871863.37"><img alt="bf4-history-of-recon-1" class="aligncenter" src="http://eaassets-a.akamaihd.net/battlelog/prod/954660ddbe53df808c23a0ba948e7971/en_US/blog/wp-content/uploads/2014/10/bf4-history-of-recon-1.jpg?v=1412871863.37" style="width:619px" /></a></p>
我想从字符串中显示:
The celebration of the Recon class in our second BF4 Class Week continues with a sneaky stroll down memory lane. Learn more about how the Recon has changed in appearance, name and weaponry over the years…
实际上,此文本将放置在元描述标记中,因此我不需要元标记中的任何HTML。 我怎么能这样做?关于这种技术的任何想法和想法?
答案 0 :(得分:37)
答案 1 :(得分:2)
为可能需要此功能的其他人添加其他选项,Stringizer库可能是一个选项,请参阅Strip Tags。
完全披露我是该项目的所有者。
答案 2 :(得分:1)
另一种选择是使用Html2Text。它会比strip_tags做得好得多,特别是如果你想解析复杂的HTML代码。
从HTML中提取文本很棘手,所以最好的办法就是使用为此目的而构建的库。
https://github.com/mtibben/html2text
使用composer安装:
composer require html2text/html2text
基本用法:
$html = new \Html2Text\Html2Text('Hello, "<b>world</b>"');
echo $html->getText(); // Hello, "WORLD"