How to exExtract list values within xml elements using PHP -
in xml file have following...
<fieldcontent><![cdata[<ul><li><a href='http://www.mysite1.com/'>my site 1</a></li> <li><a href='http://wwwmysite2.com'>my site 2</a></li> <li><a href='http://wwwmysite3.com'>my site 3</a></li> </ul>]]></fieldcontent> using xpath or simplexml in php how can extract ahref label part (i.e. 'my site 1') within each
note, number of
many thanks
well, html fragment inside xml text node (a cdata node).
so first read html string:
$dom = new domdocument(); $dom->loadxml($xml); $xpath = new domxpath($dom); $html = $xpath->evaluate('string(//fieldcontent)'); var_dump($html); output:
string(176) "<ul><li><a href='http://www.mysite1.com/'>my site 1</a></li> <li><a href='http://wwwmysite2.com'>my site 2</a></li> <li><a href='http://wwwmysite3.com'>my site 3</a></li> </ul>" now have html can load dom , li/a elements:
$dom = new domdocument(); $dom->loadhtml($html); $xpath = new domxpath($dom); foreach ($xpath->evaluate('//li/a') $link) { var_dump( $link->getattribute('href'), $link->nodevalue ); } output:
string(23) "http://www.mysite1.com/" string(9) "my site 1" string(21) "http://wwwmysite2.com" string(9) "my site 2" string(21) "http://wwwmysite3.com" string(9) "my site 3" live example: https://eval.in/122690
Comments
Post a Comment