2014-08-18 09:53



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:

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!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dsdapobp26141 dsdapobp26141 7年前

    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";
    点赞 评论 复制链接分享
  • duanhan8757 duanhan8757 7年前

    Have tried escaping your backslashes and other special characters? Also, it's typically better to use single quotes when dealing with pre-formatted text or RegEx.

    like so:

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

    Also helpful:

    点赞 评论 复制链接分享