user3767977
user3767977

Reputation: 65

Using PHP DOMDocument - extract all data between each h2

If I have a structure like this:

<h2>Title</h2>
<p>Some content</p>
<a href="/test">A bit more content</a>
<a href="/test2">Another link</a>

<h2>Another title</h2>
<p>More content</p>

<h2>One more title</h2>
<p>Last content element</p>
<img src="/hello.jpg" />

What's the best way for me to get all content between the h2s?

So my output would be an array containing all content (so for instance the p tags and the a tags, and images should they exist) between each set of tags. So I'd have the content between the first h2 and the second h2, and the content between the second h2 and the third h2. and so on.

Hopefully that makes sense.

Any good suggestions?

Thanks in advance.

Upvotes: 0

Views: 306

Answers (1)

kjames
kjames

Reputation: 656

Have you tried $text = $dom->getElementsByTagName('h2'); ?

Upvotes: 2

Related Questions