<?php
$string = 'user34567';
if(preg_match('/user(^[0-9]{1,8}+$)/', $string)){
echo 1;
}
?>
I want to check if the string have the word user follows by number that can be 8 symbols max.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You’re very close actually:
The anchor for “must match at start of string” should be all the way in front, followed by the “user” literal; then you specify the character set
[0-9]and multiplier{1,8}. Finally, you end off with the “must match at end of string” anchor.A few comments on your original expression:
^matches the start of a string, so writing it anywhere else inside this expression but the beginning will not yield the expected results+is a multiplier;{1,8}is one too, but only one multiplier can be used after an expressionBtw, instead of
[0-9]you could also use\d. It’s an automatic character group that shortens the regular expression, though this particular one doesn’t save all too many characters 😉