如何使此preg_match大小写不敏感?

2022-08-30 08:51:16

考虑:

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

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

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


答案 1

只需在分隔符后添加 i 修饰符(在您的情况下):#

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

如果您的分隔符是 ,则在它后面添加一个 :/i

preg_match("/your_regexp_here/i", $s, $matches); // i means case insensitive

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


答案 2

另一种选择:

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

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


推荐