dongpao5261 2012-04-21 07:37
浏览 51

PHP正则表达式匹配配对标记但不匹配未配对标记

I am extracting a nested content of a tag. I have written a regular expression that works perfectly on my local machine (WAMP) PHP Version 5.3.8, Multibyte regex (oniguruma) version - 4.7.1

$tagname = 'mytag';
$pattern = "@<$tagname(?:\s.*?!/)?>(.*?)</$tagname\s*>@s"; 

but the same regular expression is not working properly on my live machine with configuration of PHP Version 5.2.11, Multibyte regex (oniguruma) version- 4.4.4 and Multibyte regex (oniguruma) backtrack check = on.

Please help in modifying this regular expression that it start work on live site.

This regular expression match <mytag></mytag> and it is not matching <mytag />.

Update: Thanks for your suggestions and responses.

I updated that regular expression a bit and it worked for me. You can use this regular expression whenever you want get inner content (nested tags and text). No matter how your tag is arranged and no matter how many attributes it has.

  • 写回答

1条回答 默认 最新

  • doulu8341 2012-04-21 07:43
    关注

    you have to Put somwhere, something like:

    (?:</$tagname>|/>)
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度