PHPquery lib. and parsing XML

903 views Asked by At

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!

1

There are 1 answers

0
cofirazak On

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"
}