Exploding with html tag as separator

I have a piece of html like this:

<p>some text</p>
<p>more text</p>

I want to take out the text only and put it into an array. The easiest way seemed to be using explode like this:

$text_chunks = explode(‘<p>’, $html_string); // forgetting the trailing </p> // at this stage

This doesn’t work even when I escape the tags. Any ideas?

strip_tags(); ?
then you could perhaps do something like explode("
\r", $text_chunks); if it’s spaced like that anyway.

There would be a better way to do it using regex though. :wink:

Are you sure it doesn’t work?

Try print_r($text_chunks);

You could do this:


    echo '<pre>';

    $str = '<p>some text</p>
            <p>more text</p>';

    preg_match_all ( '#<p>(.+?)</p>#', $str, $parts );

    print_r ( $parts[1] );

    echo '</pre>';


Sean :slight_smile:

beat you all. :stuck_out_tongue:
(use seans method ;))

Thanks. I managed it like this as well:

$text_chunks = preg_split(‘{<p>}’, $html_string);
$chunk = ereg_replace(‘\<\/p\>’, ‘’, $text_chunks[1]); //or whichever chunk I

But i still can’t understand why explode doesn’t work?

You should use preg_replace instead.

Or better yet.

$chunk = strip_tags($text_chunks[1]);