Thursday 15 September 2011

html - use DOMXPath and function query php -



html - use DOMXPath and function query php -

have next html code section:

<ul id="tree"> <li> <a href="">first</a> <ul> <li><a href="">subfirst</a></li> <li><a href="">subsecond</a></li> <li><a href="">subthird</a></li> </ul> </li> <li> <a href="">second</a> <ul> <li><a href="">subfirst</a></li> <li><a href="">subsecond</a></li> <li><a href="">subthird</a></li> </ul> </li> </ul>

need parse html code , next (using domxpath::query() , foreach())

- first -- subfirst -- subsecond -- subthird - sec -- subfirst -- subsecond -- subthird

piece of code:

$xpath = new \domxpath($html); $catgs = $xpath->query('//*[@id="tree"]/li/a'); foreach ($catgs $category) { // first level echo '- ' . mb_strtolower($category->nodevalue) . '<br>'; // sec level // don't know }

thanks in advance!

this domblaze for:

/* domblaze */ $doc->registernodeclass("domelement","domblaze"); class domblaze extends domelement{public function __invoke($expression) {return $this->xpath($expression);} function xpath($expression){$result=(new domxpath($this->ownerdocument))->evaluate($expression,$this);return($result instanceof domnodelist)?new iteratoriterator($result):$result;}} $list = $doc->getelementbyid('tree'); foreach ($list('./li') $item) { echo '- ', $item('string(./a)'), "\n"; foreach ($item('./ul/li') $subitem) { echo '-- ', $subitem('string(./a)'), "\n"; } }

output:

- first -- subfirst -- subsecond -- subthird - sec -- subfirst -- subsecond -- subthird

domblaze fluentdom poor.

php html xpath domdocument

No comments:

Post a Comment