Search code examples
phphtmlxpathdomxpath

How to use DOMXPath query to get area shapes


Current PHP:

    $dom = new DOMDocument();
    $dom->loadHTML($htmlsrc);

    $xpath = new DOMXPath($dom);

    $tags = $xpath->query('//div/map/area');
    foreach ($tags as $tag) {
        var_dump(trim($tag->nodeValue));
    }

Which obviously is not correct. I have a variable that holds file_get_contents of a file which has a few (about 8 thousand) of these:

<area shape="poly" alt="" coords="500,327,464,349,467,385,478,400,492,394,500,356" href="1880314600" title="" />

I'm trying to collect $coords and $href so I can put these in a database instead of a file but I just cant catch on to how to use xpath. Sorry I do not have a correct answer to "What have you tried?" I have tried a ton of stuff, I simply cannot understand this (or regex for that matter).

EDIT For the comments:

$xpath = new DOMXPath($dom);
    var_dump($xpath);
    $tags = $xpath->query('//div/map/area');
    foreach ($tags as $tag) {
        var_dump($tag->getAttribute('coords'));
    }

The first var_dump() shows: (edited because there are tons of them)

object(DOMXPath)#2985 (1) { ["document"]=> string(22) "(object value omitted)" } string(47) "500,327,464,349,467,385,478,400,492,394,500,356" string(47) "559,310,530,314,532,394,543,389,561,367,561,351" string(63) "613,369,586,343,575,343,575,369,584,394,618,402,638,398,629,371" string(47) "523,431,501,438,498,451,537,468,550,464,550,447" string(39) "525,464,510,460,507,466,518,471,523,469"

However the second var_dump does not return anything. Why is the first xpath dump only showing coords? Further more I am not even puting in a query at that point...


Solution

  • To get the coordinates (or any other attribute for that matter) you can simply do:

    var_dump($tag->getAttribute('coords'));
    

    If you also want to get the href attribute you can simply do:

    var_dump([
        'coords' => $tag->getAttribute('coords'),
        'href'   => $tag->getAttribute('href'),
    ]);