dongnei3634 2010-12-20 10:04
浏览 58
已采纳

正则表达式匹配HTML包裹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!

  • 写回答

2条回答 默认 最新

  • dsfds656545 2010-12-20 10:13
    关注

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MYSQL 多表拼接link
  • ¥15 关于某款2.13寸墨水屏的问题
  • ¥15 obsidian的中文层级自动编号
  • ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵
  • ¥15 券商软件上市公司信息获取问题
  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了