Reputation: 379
I retrieve the contents of a website with DomDoc and query for certain elements with XPath. For example I query for <p>
Elements - by doing something like this inside a Loop $paragraphs = $dom->query('//p')->item(i);
and then continue to manipulate the <p>
inside the loop. Now, is there a way to find out what kind of other HTML elements are maybe inside the <p>
and how many other elements are inside the paragraph element?
I have seen this example : PHP Documentation
But this only seems to work if you know something about the child elements? how can I get the count and element name if the "contents" of the <p></p>
is unknown to me.
Thank You!
Upvotes: 0
Views: 127
Reputation: 167516
Well, on any DOM element you have you can use getElementsByTagName('*')
to find all descendant elements or you can use XPath relative to the element to find all child element with the path *
or count them with the XPath expression count(*)
or you can do the same for all descendant elements using .//*
respectively count(.//*)
.
So given $element = $dom->query('//p')->item(i);
you can use $element->getElementsByTagName('*')->length
as the DOM way to find all descendants, or $dom->query('.//*', $element)
as the XPath way. For the child elements use $dom->query('*', $element)
.
Upvotes: 2