Search code examples
phpdictionarydomdocumentdomxpath

DOMDocument and DOMXPath error


I'd like to map all my area shape tags using DOMDocument, but my code just works with DIV's not with map tag. Why? Here is my code. The problem that I get is null strings return.

Code:

$DOM = new DOMDocument; 
$DOM->loadHTML($conteudo['ambiente']->mapeamento);
$xpath = new DOMXPath($DOM);
$tags = $xpath->query('//area');
foreach ($tags as $tag) {
    var_dump(trim($tag->nodeValue));
}

Example code:

<map name="Map" id="Map">
  <area shape="poly" coords="0,916,86,880,527,854,520,819,536,818,547,818,564,819,563,855,627,851,620,813,640,810,653,810,664,813,660,847,804,837,816,820,766,823,756,821,752,815,743,414,755,407,782,406,1282,430,1286,432,1279,775,1290,775,1306,775,1311,777,1305,804,1337,801,1335,775,1341,772,1353,772,1370,774,1370,797,1711,793,1792,802,1789,365,1795,360,1804,0,-1,0,0,916,1,915,1144,804" href="#" />
  <area shape="poly" coords="1143,803,1271,792,1274,808,1151,816,1143,812,1142,803" href="#" />
  <area shape="poly" coords="1800,-1,2052,-1,2054,1033,1907,1003,1909,973,2024,967,2016,315,1866,328,1794,360,1803,2" href="#" />
  <area shape="poly" coords="1899,975,1894,1002,1876,998,1879,974,1901,974" href="#" />
</map>

Solution

  • Your <area> tags have no content, so the output is correct. If you want to get the value of the coords attribute, you should use

    $tags = $xpath->query('//area/@coords');