开发者

problems with text parsing using preg_split

开发者 https://www.devze.com 2022-12-08 09:13 出处:网络
I write some easy parser for my page and have some problem with it. HTML text: <p>some text</p><p>another text</p>

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号