I'm trying to put some HTML-informations in an array.
<p>Title 1</p>
<p><span>Content 1</span></p>
<p><span>Content 2</span></p>
<p>Title 2</p>
<p><span>Content 1</span></p>
<p><span>Content 2</span></p>
<p><span>Content 3</span></p>
I want to put these in an array with the fields "title" and "content". My problem is to put those p-tags together which has a span and belongs to the same title.
The result should be: (strip_tags for title and remove span for content)
[0]['title'] => 'Title 1',
[0]['content'] => ' <p>Content 1</p><p>Content 2</p>',
[1]['title'] => 'Title 2',
[1]['content'] => ' <p>Content 1</p><p>Content 2</p><p>Content 3</p>',
My attempt:
$paragraphs = explode('<p>', $html);
for ($i = 0 ; $i < count($paragraphs) ; $i++) {
$paragraphs[$i] = '<p>' . $paragraphs[$i];
if (strpos($paragraphs[$i], '<span') !== false) $content .= $paragraphs[$i];
else $title = $paragraphs[$i];
}
But this will merge ALL content to one variable. I need the above array...