php - Split paragraphs into multidimensional array -


i'm trying put html-informations in 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 put these in array fields "title" , "content". problem put p-tags has span , belongs same title.

the result should be: (strip_tags title , remove span 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); ($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 merge content 1 variable. need above array...

you need implement own counter checking title change.

$output = array(); $last_title=''; // store last title reference $counter=-1; //your counter $paragraphs = explode('<p>', $html); ($i = 1,$c=count($paragraphs) ; $i < $c ; $i++) { //start second there blank row     $paragraphs[$i] = '<p>' . $paragraphs[$i];     if (strpos($paragraphs[$i], '<span') !== false) {         $output[$counter]['content'] .= trim(strip_tags($paragraphs[$i],'<p>'));     }     else $title = $paragraphs[$i];     if ($title != $last_title){         $last_title = $title;         $counter++;         $output[$counter]['title'] = strip_tags($title);         $output[$counter]['content'] = '';     } } print_r ($output); 

note: still suggest php dom.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -