简体   繁体   中英

PHP preg_match not matching with an expression to pattern

I have this php code

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.

Thanks

You're missing the closing parentheses for your if statement.

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.

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. Proper if should look like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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