douyong6589 2014-08-18 09: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 10: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条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记