Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP仅从HTML字符串中提取文本?_Php - Fatal编程技术网

如何使用PHP仅从HTML字符串中提取文本?

如何使用PHP仅从HTML字符串中提取文本?,php,Php,我只想从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.

我只想从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&hellip;</strong></p>

<p>&nbsp;</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。 我怎样才能做到这一点?关于这项技术有什么想法吗?

您可以试试:

echo(strip_tags($your_string));

此处的更多信息:

另一个选项是使用Html2Text。它将比strip_标记做得更好,尤其是当您想要解析复杂的HTML代码时

从HTML中提取文本是一件棘手的事情,因此最好使用为此目的而构建的库

使用composer安装:

composer require html2text/html2text
基本用法:

$html = new \Html2Text\Html2Text('Hello, &quot;<b>world</b>&quot;');

echo $html->getText();  // Hello, "WORLD"
$html=new\Html2Text\Html2Text(‘你好,世界’);
echo$html->getText();//你好,“世界”

为可能需要此功能的其他人添加另一个选项,库可能是一个选项,请参阅


完全公开我是项目的所有者。

删除所有带有strip_标记的html标记()使用strip_标记(),查看手册,您将获得有关如何使用它以及它的作用的详细信息。我们是否有任何方法可以删除html字符。检查这个线程:大量的css会把事情搞砸。尽管内部使用了
strip\u tags()
,但除了包含文本的许多其他标记外,您不希望使用COL项目,您是否有想过创建一个处理内联css样式的函数?