开发者

Get a single element with PHP and XPath

开发者 https://www.devze.com 2022-12-25 11:55 出处:网络
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?

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

精彩评论

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

关注公众号