开发者

PHPquery lib. and parsing XML

I started using the phpquery thingy, but I got lost in all that documentation. In case someone does not know what the hell I am talking about: http://code.google.com/p/phpquery/

My question is pretty much basic. I succeeded at loading an XML document and now I want to parse all the tags from it.

Using pq()->find('title') I can output all of the contents inside the title tags. Great!

But I want to throw every <title> tag in a variable. So, lets say that there are 10 <title> tags, I want every one of them in a separate variable, like: $title1, $title2 ... 开发者_如何学编程$title10. How can this be done?

Hope you understand the question. TIA!


You could do it like this:

phpQuery::unloadDocuments();
phpQuery::newDocument($content);
$allTitles = [];
pq('title')->each(function ($item) use (&$allTitles) {
       $allTitles[] = pq($item)->text();
});
var_dump($allTitles);

For example if there are 3 titles in the $content this var_dump will output:

array(3) {
       [0] =>
       string(6) "title1"
       [1] =>
       string(6) "title2"
       [2] =>
       string(6) "title3"
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜