如何使这个 preg_match 大小写不敏感?

考虑一下:

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

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

这个代码实际上是可以工作的,但是它是区分大小写的。我如何使它不区分大小写呢?

103234 次浏览

只需在分隔符后面添加 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