如何使此preg_match不区分大小写


How do I make this preg_match case insensitive?

考虑:

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

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

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

只需在分隔符后面添加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修饰符,则模式中的字母将同时匹配大写字母和小写字母。

另一个选项:

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

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