简体   繁体   中英

Read HTML element values via PHP

I'm trying to read element values from a HTML document via a PHP script.

currently I have the following code:

$doc = new DOMDocument();
@$doc->loadHTMLFile($filename);
$xpath = new DOMXPATH($doc);
$div = $xpath->query('//div[@class="psrch-FullResult"]');
$div = $div->item(0);
echo $doc->saveXML($div);

This code prints the HTML code of item 0 onto the webpage.

I have two problems to solve

firstly: I would like to inspect the printed elements' children properties/ elements.

the children are located in <div id=ContactInfo> inside of which is a <ul id=MiniContactCard> inside of which is a <li id=NameField> who's value I am trying to read.

Are you able to help show me how to navigate through these lower level Elements?

secondly: I would like to add an additional condition to the xpath->query. The class can be either:

  • psrch-FullResult (as shown) OR
  • psrch-FirstFullResult

Thank you for your help, Kind Regards Jordan

试用PHP简单HTML DOM解析器非常易于使用,您将找到示例。

Try to add libxml_use_internal_errors before loadHTMLFile

libxml_use_internal_errors(true);
$doc->loadHTMLFile($filename);

Enjoy your code!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM