views:

68

answers:

2

Hello! I write some easy parser for my page and have some problem with it.

HTML text:

<p>some text</p><p>another text</p>

If I try use something like:

preg_split("#<p>#",$string);

I have a result without <p>, and this is very very bad. (only </p> exist)

Maybe I can split this string to array, but don't remove </p>?

+3  A: 

You can use this construct (?=<p>) which is positive lookahead zero-width assertion. This pattern will not consume the text it matches. It just will find the position before <p> string. Here is example:

preg_split("#(?=<p>)#",$string);
Ivan Nevostruev
thank you very much! this solve all my problem ;)
rbdev
A: 

Just like Ivan said, you should use (?=<p>). Just wanted to add that you can use

var $Paragraphs = array_filter(preg_split("/(?=<p>)/", "<p>some text</p><p>another text</p>"));

Which will be:

[1] => <p>some text</p>
[2] => <p>another text</p>
NawaMan