Php 这个正则表达式中不区分大小写的修饰符在哪里

Php 这个正则表达式中不区分大小写的修饰符在哪里,php,regex,preg-match-all,Php,Regex,Preg Match All,在下面的正则表达式中,i修饰符在哪里使搜索词$searchfor不区分大小写?看来不管我把它放在哪里都不行 $file = 'xml.xml'; $searchfor = '(<first>|<last>|<state>)'; header('Content-Type: text/plain'); $contents = file_get_contents($file); $regExp = '/^'.$searchfor.'(.*)$/m'; if

在下面的正则表达式中,
i
修饰符在哪里使搜索词
$searchfor
不区分大小写?看来不管我把它放在哪里都不行

$file = 'xml.xml';

$searchfor = '(<first>|<last>|<state>)';

header('Content-Type: text/plain');

$contents = file_get_contents($file);

$regExp = '/^'.$searchfor.'(.*)$/m';

if(preg_match_all($regExp, $contents, $matches)){
   echo "Found matches:\n";
   echo implode("\n", $matches[0]);
} else {
   echo "No matches found";
}
$file='xml.xml';
$searchfor='(| |)';
标题(“内容类型:文本/普通”);
$contents=file\u get\u contents($file);
$regExp='/^'$searchfor.(.*)$/m';
if(preg_match_all($regExp,$contents,$matches)){
echo“找到匹配项:\n”;
echo内爆(“\n”,$matches[0]);
}否则{
回显“未找到匹配项”;
}
xml.xml的内容

<first>Micky</first>
<last>Mouse</last>
<state>CA</state>

<first>Donald</first>
<LAST>Duck</LAST>
<state>FL</state>

<FIRST>Gyro</FIRST>
<last>Gearloose</last>
<state>MA</state>
米奇 老鼠 加利福尼亚州 唐纳德 鸭子 佛罗里达州 陀螺 齿轮松动 文科硕士
您可以在还指定了多行的位置添加不区分大小写的标志
/m

这一行:

$regExp = '/^'.$searchfor.'(.*)$/m';
然后可能是:

$regExp = '/^'.$searchfor.'(.*)$/mi';

注意行
$regExp='/^'$searchfor.(.*)$/m'在您的代码中

它“组装”正则表达式(
/…/
)的内容,并紧跟其后 有选择的余地。 您只将
m
放在那里,所以只需添加
i

但这并不是全部。我怀疑在“最初”部分之后你是否真的 需要捕获该行的所有剩余部分

尝试稍微更改的正则表达式:

/^<(first|last|state)>([^<]+)<\/\1>$/mi

/^([^有几种方法可以做到这一点。在当前正则表达式中,您可以在结束分隔符后使用
i
修饰符

$regExp = '/^'.$searchfor.'(.*)$/mi';

或者您可以使用:

$regExp = '/(?i)^'.$searchfor.'(.*)$/m';

这是相同的,但在正则表达式没有分隔符(
grep
等)时更易于使用

您可以采取的另一种方法是使用将两个字符串转换为小写


这些答案中有没有解决您的问题?…需要这一个作为密切链接。
if(preg_match_all(strtolower($regExp), strtolower($contents), $matches)){