preg_match对最后两个字符无效


preg_match not working on last two charcters

我得到了他的小代码

 <?php  
    if (preg_match("/Remanufactured|REMANUFACTURED/",$product_info['products_description'] ) 
|| preg_match("/N2|N3|W1|W2|A1|A2|A3|R4/",$product_info['manufacturers_code'])) {
    echo "Refurbished";
   } else  {         
  echo "New Boxed"; 
 }
?>      

如果$product_info['manufacturers_code']='2122586R4'或'MC-MCB43112U0C2/04/1',则上述代码有效。

我希望上面的代码只匹配最后两个字符为"/N2|N3|W1|W2|A1|A2|A3|R4/"的

我试过"/N2$|N3$|W1$|W2$|A1$|A2$|A3$|R4$/",但它不起作用。

关于

irfan

更换

/N2|N3|W1|W2|A1|A2|A3|R4/

/^(.*)(N2|N3|W1|W2|A1|A2|A3|R4)$/

.*将匹配开头的任何字符串和结尾的指定两个字符串

试试这个

preg_match('/(N2|N3|W1|W2|A1|A2|A3|R4)$/',$product_info['manufacturers_code'])

( )-将一系列模式元素分组为单个元素。

|-分离备选可能性。

$-匹配行或字符串的末尾。

所以"(a|b)"的意思是"a or b"。"(a|b)$"表示行或字符串末尾的"a or b"。