Azeem Haider Azeem Haider - 1 year ago 431
PHP Question

DOMXPath object value omitted

I read many stackoverflow question and I'm using this code but I don't know why this is not work.

Here is a code.

$url = '';
$source = file_get_contents($url);

$doc = new DOMDocument;

$xpath = new DOMXPath($doc);
$classname = "list-group";
$events = $xpath->query("//*[contains(@class, '$classname')]");


Can you please check it why this is not working actually I want to get data from

Answer Source

The code is correct. It correctly fetches a list of DOM nodes having the specified class attribute value into the $events variable:

$events = $xpath->query("//*[contains(@class, '$classname')]");

which is an instance of DOMNodeList. Next you should iterate the list and fetch the data you need from $events. For example, if you need the outer HTML for the nodes, use something like this:

foreach ($events as $e) {
  printf("<<<<<\n%s\n>>>>>\n", $e->ownerDocument->saveXML($e));

P.S.: I would rename $events to $elements.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download