I have the following code
<div class="content"></div>
<div class="content">
<h3 itemprop="name">Grab this text</h3><div itemprop="description">Grab this text too</div>
</div>
How can I grab both those text parts and place them inside variables?
I’m working in WordPress, building a plugin. There are multiple itemprops on the page that I don’t want, so I can’t select just those.
I’ve tried
$name = $xpath->query( '//div[@class="content"]//h3[@itemprop="name"]' );
But that doesn’t select the second part description and leaves me with an error when I try to echo it.
I think I might have to iterate through them after grabbing the main div, but I’m not sure how to do that, and I looked up a few other stacks that didn’t help.
Thanks
2
Answers
Thanks @Siebe, your answer partly helped, so I didn't accept the answer I wanted to post what the difference I found making it work.
I got the name and description separate.
Thanks for the idea of the .= that's what I was missing.
If the filter of
@itemprop
insidediv[@class="content"]
is enough, use this:If you are only interested in
@itemprop
with the value ‘name’ or ‘description’ use this:The
*
means anyelement
, so it will find bothh3
anddiv