tags:

views:

33

answers:

1

Let's say, that I have the following source output:

<p>This is first paragraph</p>
<p>This is second paragraph</p>
<p>This is third paragraph</p>

What I want to achieve is... I want to split them, first paragraph goes into one variable, others into other. Like:

$first = "<p>This is first paragraph</p>";
$next = "<p>This is second paragraph</p><p>This is third paragraph</p>";

Because those are generated by TinyMCE and user input, I can never know when user will add <br /> or other tags, which will cause TinyMCE to generate a new code-break \r\n. Therefore, all the solutuons which split them by looking for \r\n won't work this time.

Any advice?

+3  A: 

Try this:

$input = str_replace('</P>', '</p>', $input); # In case of upper case tags
list($first, $next) = explode('</p>', $input, 2);
$first .= '</p>';
jmz
+1 Nice and easy. inb4 "Use a DOM parser" etc. You *could* consider to replace the first two lines with `preg_split('#</p>#i', $input, 2)` to avoid mutating `$input`. (Other, unrelated `</P>` tags *may* be affected by `str_replace()`.)
jensgram
Thanks, this will do it for now!
Tom