简体   繁体   中英

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 improve this regular exp. to solve my problem? :)

Thanks in advanced!

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();

你有尝试过吗?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM