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)
May开发者_开发百科be I can split this string to array, but don't remove </p>
?
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);
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>
精彩评论