1

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

2
  • 1
    you failed to close the if function. if(preg_match('/BUT[a-zA-Z0-9]+TN/', $id)) Commented Jun 6, 2014 at 4:15
  • can you post your full code how you getting id or what error you facing Commented Jun 6, 2014 at 4:20

2 Answers 2

2

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"
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

preg_match returns 0, 1 or false. Since you don't want neither 0 nor false, the > 0 is not necessary to catch a successful match.
@ring0 preg_match returns false only in case of error. We not going to feed bad regexp to it, do we? PHP is flexible about data types but being strict about types and explicit about values over time proven to be really good way of reducing number of bugs caused by automatic typecasting. Good programming habits will never go out of style :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.