1

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

2 Answers 2

4

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.