开发者

Get a single element with PHP and XPath

Lots of tutorials around the net but none of them can explain me this:

How do I select a single element (in a table, for example), having its absolute XPath?

Example: I have this:

/html/body/table/tbody/tr[2]/td[2]/table/tbody/tr/td/table[3]/tbody/tr/td/table/tbody/tr[3]/td/table/tbody/tr[4]/td[5]/span

What's that PHP function to get the text of that element?! Really I开发者_运维知识库 could not find an answer. Found lots of guides and hints to get all the elements of the table, all the buttons of a form, etc, but not what I need.

Thank you.


$xml = simplexml_load_string($html_content_string);
$arr = $xml->xpath("//body/table/tbody/tr[2]/td[2]/table/tbody/tr/td/table[3]/tbody/tr/td/table/tbody/tr[3]/td/table/tbody/tr[4]/td[5]/span");

var_dump($arr); 


Load you HTML document into a DOM object then make a DOMXPath object from it and let it evaluate your query string.

It's all described in detail here: http://php.net/manual/en/book.dom.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜