在PHP中从字符串中提取DOM元素 [英] Extract DOM-elements from string, in PHP

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

问题描述


可能存在重复:

使用php爬取html页面


>解析HTML的最佳方法


我有一个字符串变量我的php脚本,包含html页面。我如何从这个字符串中提取DOM元素?例如,在这个字符串中'< div class =someclass> text< / div>' >,我希望变量'文字'。我该如何做到这一点?

解决方案

您需要使用 DOMDocument 类,更具体地说,它的 loadHTML 方法,将您的HTML字符串加载到DOM对象中。



例如:

  $ string =<<< HTML< b $ b< p>测试< / p> 
< div class =someclass>文字< / div>
< p>另一个< / p>
HTML;

$ dom = new DOMDocument();
$ dom-> loadHTML($ string);



之后,您将可以操作DOM,使用例如 DOMXPath

例如,就你而言,你可以使用基于这部分代码的东西:

  $ xpath = new DOMXpath($ dom); 
$ result = $ xpath-> query('// div [@ class =someclass]');
if($ result-> length> 0){
var_dump($ result-> item(0) - > nodeValue);
}

在这里,您可以得到以下输出:

  string'text'(length = 4)





也可以使用
DOMDocument 。 php.net/simplexml_load_stringrel =noreferrer> simplexml_load_string SimpleXMLElement :: xpath - 但对于复杂的操作,我通常更喜欢使用 DOMDocument


Possible Duplicates:
crawling a html page using php?
Best methods to parse HTML

I have one string-variable in my php-script, that contains html-page. How i can extract DOM-elements from this string?

For example, in this string '<div class="someclass">text</div>', i wish get variable 'text'. How i can do this?

解决方案

You need to use the DOMDocument class, and, more specifically, its loadHTML method, to load your HTML string to a DOM object.

For example :

$string = <<<HTML
<p>test</p>
<div class="someclass">text</div>
<p>another</p>
HTML;

$dom = new DOMDocument();
$dom->loadHTML($string);


After that, you'll be able to manipulate the DOM, using for instance the DOMXPath class to do XPath queries on it.

For example, in your case, you could use something based on this portion of code :

$xpath = new DOMXpath($dom);
$result = $xpath->query('//div[@class="someclass"]');
if ($result->length > 0) {
    var_dump($result->item(0)->nodeValue);
}

Which, here, would get you the following output :

string 'text' (length=4)


As an alternative, instead of DOMDocument, you could also use simplexml_load_string and SimpleXMLElement::xpath -- but for complex manipulations, I generally prefer using DOMDocument.

这篇关于在PHP中从字符串中提取DOM元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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