function clearTerm($term) {
if( preg_match('/^[a-z]{2}[0-9]{2,10}$/', $term) ) {
return true;
} else {
return false;
}
}
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
[a-z]{2} any character of: 'a' to 'z' (2 times)
--------------------------------------------------------------------------------
[0-9]{2,10} any character of: '0' to '9' (between 2 and 10 times
(matching the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the string
preg_match regular expression to match 2 characters and 2 to 10 digits
-
15-07-2023 - |
Question
I need to do a regular expression that will return true if the first two characters in a string are 2 alphabets and the rest are digits only, length of 2 - 10.
I'm lost trying. I just cannot figure how to do this.
This is what the previous guy left me with:
function clearTerm($term) {
if( preg_match('/^[a-z0-9()\- ]+$/i', $term) ) {
return true;
} else {
return false;
}
}
Here's what I was trying, but it's hopeless. I cannot figure how to check the first two and then the rest.
function clearTerm($term) {
if( preg_match('/^([a-z0-9]+$/i{2})', $term) ) {
return true;
} else {
return false;
}
}
The regex needs to return true if: The first 2 characters are 2 alphabets in lower case (lg) The remaining are digits, 2 to 10 digits in length.
So, lg01234 -> True, lgx1 -> False
Tried and failed, so asked here.
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow