douyong6589 2014-08-18 01:53
浏览 27
已采纳

当正则表达式正常时,为什么preg_match在标签之间找不到字符串?

I have the next issue here. I'm trying to fetch a string between two HTML comment tags with preg_match/preg_match_all but the result I'm getting is empty array with a lot of records.

Here's my code:

$pattern = "/\<!-- Start of NewsTicker --\>(.*)\<!-- End of NewsTicker --\>/";
preg_match_all($pattern, $description , $matches);

The regular expression pattern itself seems to be valid since it works for me when I put just a short sample string (like: "123456") between two tags within $description variable.

However on the real life content it doesn't work and my guess is that this happens because of the content's length.

Here is the example of the real case the preg_match fails for me: http://www.phpliveregex.com/p/6oJ

Could anyone explain please how the issue could be solved? It's always possible to play with simple string functions like strpos, substr and etc. but isn't there other better options?

Thank you very much!

  • 写回答

2条回答 默认 最新

  • dsdapobp26141 2014-08-18 02:06
    关注

    You should add the "matches newline" flag, which is s.

    So your regex should be the following:

    $pattern = "/\<!-- Start of NewsTicker --\>(.*)\<!-- End of NewsTicker--\>/s";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部