Search code examples
phpregexpreg-split

problems with text parsing using preg_split


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>?


Solution

  • 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>