11

I am using phpquery to extract some data from a webpage. I need to identify the menu of the page. My implementation is to find each element that has sibilings > 0 and last-child is an "a". My code is:

foreach($this->doc['*'] as $tagObj){
$tag = pq($tagObj);
if(count($tag->siblings()) > 0){
    if($tag->find(":last-child")->tagName  === "a")
        echo trim(strip_tags($tag->html())) . "<br/>";
    }
}

However, I am not getting any output because of

$tag->find(":last-child")->tagName

which isn't returning anything. What would be the reason for this?

4
  • 1
    Does the pq object have the methods find and sibling? I don't think so.. maybe Commented Jul 27, 2015 at 6:02
  • it does have it - code.google.com/p/phpquery/wiki/Traversing Commented Jul 27, 2015 at 15:52
  • 2
    What does the html structure look like at that point? Could it just be that for some reason there aren't any items with siblings and children? Commented Jul 30, 2015 at 15:34
  • 1
    What do you get if you make a var_dump of $tag->find(":last-child") ? Is it the one you searched for? Make a Testcase whisch should return that. Consider to use :last. btw the menu or nav? caus' nav is nav and menu is a <ul>/<ol> with <li> in it with <a> in it. no need to check if the last one is <a> Commented Aug 5, 2015 at 15:13

4 Answers 4

4
+25

I don't know this library but perhaps something like this

$siblings = $tag->siblings();
if (($siblingCount = count($siblings)) && $siblings[$siblingCount - 1]->tagName === 'a') {
    echo ...
}
Sign up to request clarification or add additional context in comments.

Comments

3

You can do it in reverse check for a:last-child :

For example :

foreach($this->doc['*'] as $tagObj){
$tag = pq($tagObj);
if(count($tag->siblings()) > 0){
    if($tag->find("a:last-child"))
        echo trim(strip_tags($tag->html())) . "<br/>";
    }
}

This will check for the a tag of last-child and you can get its content easily. May this help you.

Comments

3

Maybe you should use :last instead of :last-child

According to the library Google Page:

$li = null;
$doc['ul > li']
        ->addClass('my-new-class')
        ->filter(':last') // <--- :last
                ->addClass('last-li')
// save it anywhere in the chain
                ->toReference($li);

Comments

0

Because phpQueryObject returned by pq implements the Iterator and uses a public array $elements to store all elements, we need to get the element using the get() function, which returns a DOMElement that is has the tagName and nodeName properties:

$q = phpQuery::newDocumentHTML('<div><span class="test-span">Testing test</span></div>');
echo $q->find('.test-span')->get(0)->tagName; // outputs "span"
//echo $q->find('.test-span')->get(0)->nodeName; // outputs "span"

Both properties will output the tag name that has the test-span class which of course is span.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.