如何使用PHP从HTML字符串中仅提取文本? [英] How to extract only text from HTML string with PHP?

查看:81
本文介绍了如何使用PHP从HTML字符串中仅提取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想从php字符串中提取文本.

I want to extract only text from a php string.

此php字符串包含html代码(例如标签或类似代码).

This php string contains html code like tags or etc.

因此,我只需要此字符串中的简单文本.

So I only need a simple text from this string.

这是实际的字符串:

<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…

实际上,此文本将放置在meta description标记中,因此我不需要meta标记中的任何HTML. 我该如何执行呢?关于这项技术有什么想法和想法吗?

Actually this text will be placed in meta description tag so I don't need any HTML in meta tag. How can I perform this? Any ideas and thoughts about this technique ?

推荐答案

您可以尝试:

echo(strip_tags($your_string));

此处的更多信息: http://php.net/manual/zh/function.strip-tags.php

这篇关于如何使用PHP从HTML字符串中仅提取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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