【问题标题】:How do I make this preg_match case insensitive?如何使这个 preg_match 不区分大小写?
【发布时间】:2012-09-06 19:44:57
【问题描述】:

考虑:

preg_match("#(.{100}$keywords.{100})#", strip_tags($description), $matches);

我试图在每边只显示 100 个字符,中间是搜索字符串。

此代码确实有效,但区分大小写。如何使其不区分大小写?

【问题讨论】:

    标签: php regex


    【解决方案1】:

    只需在分隔符 # 之后添加 i 修饰符:

    preg_match("#(.{100}$keywords.{100})#i", strip_tags($description), $matches);
    

    如果设置了 i 修饰符,则模式中的字母匹配大小写字母。

    【讨论】:

      【解决方案2】:

      另一种选择:

      <?php
      $n = preg_match('/(?i)we/', 'Wednesday');
      echo $n;
      

      https://php.net/regexp.reference.internal-options

      【讨论】:

        猜你喜欢
        • 2014-02-05
        • 2011-02-01
        • 2021-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-14
        • 1970-01-01
        相关资源
        最近更新 更多