phparraysxpath

XPATH - how to use multiple paths


I am using xpath to retrieve the text value of some links (a) in a given element. I then push the results to an array called $tableau. Everything is working fine :) The thing is, I would like now to retrieve also the href of those links. So have both the text value and the href in my array. The other thing is, I have no idea how to do that. Hope someone can help. Thank you in advance for your replies. Cheers. Marc

$url = 'http://www.somesite.com';
$path = '.../a';
$tableau = array();

$dom = new DOMDocument();
@$dom->loadHTML($url);
$xpath = new DomXPath($dom);
$x = $xpath->query($path);

foreach ($x as $value)
     array_push($tableau, $value->nodeValue);

Solution

  • Thanks to the hints given by javram and vireshad I manage to find the solution. See here below:

    $url = 'http://www.somesite.com';
    $path = '.../a';
    $tableau = array();
    
    $dom = new DOMDocument();
    @$dom->loadHTML($url);
    $xpath = new DomXPath($dom);
    $x = $xpath->query($path);
    
    foreach ($x as $value)
         array_push($tableau, array($value->nodeValue, $value->getAttribute('href')));