开发者

Regex match HTML wrapped around HTML

I asked here a while ago about matching text inside of two wrapped <code>..</code> tags from a string, and it's been working great until somebody had some other HTML wrapped inside the <code> tags.

This is how I'm doing it so far:

preg_match_all("!<code>([^<]*)</code>!", $string, $return_array);

Could anybody impr开发者_如何学Cove this regular expression to solve my problem?


This is one case where I have to agree with the dreaded regex are evil meme. For straightforward extraction purposes, regular expressions are often suitable. But if you want to process malformed and or nested HTML, it's not an option without significant fuss.

Hence I'd recommend using phpQuery or QueryPath for such occasions. It's also pretty simple:

print qp($html)->find("code")->text();


Have you tried this?

preg_match_all("!<code>(.*?)</code>!", $string, $return_array);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜