简体   繁体   English

PHP preg_match与表达式的模式不匹配

[英]PHP preg_match not matching with an expression to pattern

I have this php code 我有这个PHP代码

if(preg_match('/BUT[a-zA-Z0-9]+TN/', $id))
{
echo "Match found";
}

For $id as 'BUTEqHLHxJSRr9DJZSMTN' , its not working/matching.But I have tested the regex pattern online with the id and it worked.Please help me find the issue. 对于$id as 'BUTEqHLHxJSRr9DJZSMTN' ,它无法正常工作/匹配。但是我已经使用id在线测试了正则表达式模式,它可以正常工作,请帮助我找到问题。

Thanks 谢谢

You're missing the closing parentheses for your if statement. 您缺少if语句的右括号。

if (preg_match('/BUT[a-zA-Z0-9]+TN/', $id))
                                          ^

EDIT : Your code and regular expression does work, see working demo . 编辑 :您的代码和正则表达式确实有效,请参见有效的演示 Perhaps you have another issue somewhere else inside your code or your variable $id possibly contains something different. 也许您在代码中的其他地方遇到了另一个问题,或者您的变量$id可能包含其他内容。

As you can see, this returns a match. 如您所见,这将返回一个匹配项。

$id = 'BUTEqHLHxJSRr9DJZSMTN';
preg_match('/BUT[a-zA-Z0-9]+TN/', $id, $match);
echo $match[0]; //=> "BUTEqHLHxJSRr9DJZSMTN"

Please note that preg_match returns integer, not boolean. 请注意,preg_match返回整数,而不是布尔值。 Proper if should look like: 正确的应如下所示:

if (preg_match('/BUT[a-zA-Z0-9]+TN/', $id) > 0)
  {
    echo "Match found";
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM