2

I'm able to get anchor's property href using PHP Simple HTML DOM Parser by using following code

foreach($page->find('a') as $anchor){
    echo trim(strip_tags($anchor->href));  
}

This is working absolutely fine. But Here is the issue, Now I've to get the data-cursor property of div

<div class = 'someClass' data-cursor = '4515314844'>
    some contents here..
</div>

but if I try the same approach, as mentioned above, this throws an error

foreach($page->find('div') as $div){
    echo trim(strip_tags($div->data-cursor));  
}

Error: Use of undefined constant cursor - assumed 'cursor'

Thanks.

2 Answers 2

2
Error: Use of undefined constant cursor - assumed 'cursor'

That should tell you that PHP isn't parsing your code correctly because of the hyphen. It's reading 'data' then the hyphen is throwing it off. Try surrounding it like this:

$div->{'data-cursor'}
Sign up to request clarification or add additional context in comments.

Comments

1

To access the hypenated property properly, you need to do this:

$div->{'data-cursor'}

Would look like this:

echo trim(strip_tags($div->{'data-cursor'}));

3 Comments

this is cool, I want to vote it up.. but I've not enough rating to vote it up :(
perhaps it would go up if some of you give this question an up vote
Thanks Man.. I'm still unable to vote it up :( BTW many thanks

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.