user3713664
user3713664

Reputation: 23

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

Upvotes: 1

Views: 147

Answers (2)

hwnd
hwnd

Reputation: 70732

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"

Upvotes: 2

Vladimir Bashkirtsev
Vladimir Bashkirtsev

Reputation: 1354

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";
  }

Upvotes: 0

Related Questions