Php 需要从整个博客页面html中提取特定段落文本和图像的帮助吗

Php 需要从整个博客页面html中提取特定段落文本和图像的帮助吗,php,html,parsing,blogs,Php,Html,Parsing,Blogs,我需要html解析方面的帮助。在这里发布问题之前,我试图找到这个答案,但找不到。我已将博客页面的完整html存储在数据库表中。现在我想从html中提取文本和图像。但是我必须从整个html中只提取特定于段落的文本和图像 请参见下面的示例,其中包含许多代码标记。它有三段。我必须只从与我的要求相关的第2段中提取文本和图像。(我有关键字,我可以搜索该关键字,通过这种方式,我可以确定我需要提取此段落。) 如何从任何博客中提取特定段落文本和图像。我有要在html中搜索的关键字,即关键字=产品ABC。我正在使

我需要html解析方面的帮助。在这里发布问题之前,我试图找到这个答案,但找不到。我已将博客页面的完整html存储在数据库表中。现在我想从html中提取文本和图像。但是我必须从整个html中只提取特定于段落的文本和图像

请参见下面的示例,其中包含许多代码标记。它有三段。我必须只从与我的要求相关的第2段中提取文本和图像。(我有关键字,我可以搜索该关键字,通过这种方式,我可以确定我需要提取此段落。)

如何从任何博客中提取特定段落文本和图像。我有要在html中搜索的关键字,即关键字=产品ABC。我正在使用php

<html>
<!-- Javascript: tag come here --->
<!-- Head: tag come here --->
<!-- Meta: tag come here --->
<!-- Title: tag come here --->
<!-- Links: tag come here --->
<!-- Javascript: tag come here --->

<body>

<!-- Lot of other code come here about links, javascript, headings etc -->
<!-- DIV: tag come here --->

<p> "PARAGRAPH 1, This paragraph contain only some text." </p>
<!-- Script: tag come here --->

<p> PARAGRAPH 2, It has some information about PRODUCT ABC...</p>
<img /> <!-- some images come here related to this paragraph.-->
<img /> <!-- some images come here related to this paragraph.-->
<img /> <!-- some images come here related to this paragraph.-->
<!-- Script: tag come here --->

<p> PARAGRAPH 3, This paragraph contain only some text. </p>
<img /> <!-- some images come here related to this paragraph.-->
<!-- Links: tag come here --->
<!-- Javascript: tag come here --->

</body>
</head>
</html>

“第1段,本段仅包含一些文本。”

第2段,它有一些关于产品ABC的信息

第3段,本段仅包含一些文本


我同意dreamwiever的观点。不过,这是html论坛:P

使用此代码:


$html=文件获取html(“”)$par=$html->find('p[id=hello]');foreach($par->find('img')as$element)echo$element->src
'

如果您正在寻找一个简单的
标记来提取,您可以使用
regex

简单地说:

$html = "<html><head></head><body><div>sometext</div><div><p>myPtag</p></div><div> some other text</div></body></html>";

preg_match('/<p>(.*?)<\/p>/',$html,$getTheP);

//and simply call what you want from extraction 
var_dump($getTheP);

)

您可以使用这个由sourceforge.ref编写的基于PHP的dom解析器:我已经在使用它了,但它无法帮助我处理特定的段落文本和相关图像。它带来了整个页面级别的所有文本和图像。所有文本和图像都会破坏我所需的结果。您知道如何获取特定段落文本和图像吗?为什么不先使用dom解析器提取特定段落并存储在变量中,然后调用示例变量上的find来提取图像或您需要的任何元素<代码>$html=文件\u获取\u html('http://www.google.com/'); $par=$html->find('p[id=hello]');foreach($par->find('img')as$element)echo$element->src
'
preg_match('/<p>(.*?)somestring<\/p>',$html,$matchesWithSomeString);

var_dump ( $matchesWithSomeString )