Php 用小写或大写n正则表达式匹配一组单词

Php 用小写或大写n正则表达式匹配一组单词,php,regex,preg-match,Php,Regex,Preg Match,我需要一个正则表达式来匹配一组大写或小写的单词 例如,我有一个单词数组: 订单、物品、朋友、学生 我想要一个像OrdeRs或OrdeRs或stuDents或FrIends或stuDents这样的词来匹配正则表达式 我非常感谢你的帮助。感谢您需要使用不区分大小写的regex i标志 这是有道理的。@mario当我尝试[orders,items]/I时,它不起作用,这不是正则表达式中的有效语法。如果列表采用的是某种格式,你甚至不需要正则表达式。@mario你是说这个吗\bcat |狗\bi? <

我需要一个正则表达式来匹配一组大写或小写的单词 例如,我有一个单词数组:

订单、物品、朋友、学生

我想要一个像OrdeRs或OrdeRs或stuDents或FrIends或stuDents这样的词来匹配正则表达式


我非常感谢你的帮助。感谢您需要使用不区分大小写的regex i标志


这是有道理的。@mario当我尝试[orders,items]/I时,它不起作用,这不是正则表达式中的有效语法。如果列表采用的是某种格式,你甚至不需要正则表达式。@mario你是说这个吗\bcat |狗\bi?
<?php
$string = "orders,items,friends,students OrdeRs or orders OR stuDents or FrIends or students";

preg_match_all('/(orders|items|friends|students)/i', $string, $result, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($result[1]); $i++) {
    echo $result[1][$i]."\n";
}
/*
orders
items
friends
students
OrdeRs
orders
stuDents
FrIends
students
*/
?>
(orders|items|friends|students)

Options: Case insensitive; 

Match the regex below and capture its match into backreference number 1 «(orders|items|friends|students)»
   Match this alternative (attempting the next alternative only if this one fails) «orders»
      Match the character string “orders” literally (case insensitive) «orders»
   Or match this alternative (attempting the next alternative only if this one fails) «items»
      Match the character string “items” literally (case insensitive) «items»
   Or match this alternative (attempting the next alternative only if this one fails) «friends»
      Match the character string “friends” literally (case insensitive) «friends»
   Or match this alternative (the entire group fails if this one fails to match) «students»
      Match the character string “students” literally (case insensitive) «students»