giraffemedia Posted October 4, 2011 Share Posted October 4, 2011 hello, I'm trying to put together a regex that validates a two-digit number if it's over 17 i.e. 18 of more. Can anyone help, i'm not terribly hot on regex stuff. Thanks, James Quote Link to comment Share on other sites More sharing options...
giraffemedia Posted October 4, 2011 Author Share Posted October 4, 2011 No worries i've just figured it out. It's [1-9][8-9] James Quote Link to comment Share on other sites More sharing options...
salathe Posted October 4, 2011 Share Posted October 4, 2011 What about 20, 21, 22, 23, 24, 25, 26, 27, 30, 31, 31, 33, 34... ? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 4, 2011 Share Posted October 4, 2011 This might work (might as in I don't know how your using the number). <?php $values = array( "Joe is 18 years old", "Joe is 13 years old", "Joe is 11 years old", "Joe is 100 years old" ); foreach($values as $value){ preg_match("/\d+/", $value, $matches); if($matches[0] >= 18){ echo "Joe is 18 or older"; }else{ echo "Joe is under 18"; } echo "<br>"; } ?> if you have as specific number, such as $age = 10, then you could just do something like this: $age = 10; if($age >= 18){ echo "18 or older"; }else{ echo "under 18"; } Quote Link to comment Share on other sites More sharing options...
xyph Posted October 5, 2011 Share Posted October 5, 2011 1[89]|[2-9]\d Match either the regular expression below (attempting the next alternative only if this one fails) «1[89]» Match the character “1” literally «1» Match a single character present in the list “89” «[89]» Or match regular expression number 2 below (the entire match attempt fails if this one fails to match) «[2-9]\d» Match a single character in the range between “2” and “9” «[2-9]» Match a single digit 0..9 «\d» Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 5, 2011 Share Posted October 5, 2011 If it's just a number and nothing else, you don't even really need a pattern. This should be faster too. if( ctype_digit($number) && $number > 17 && $number < 100 ) { Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 5, 2011 Share Posted October 5, 2011 If it's just a number and nothing else, you don't even really need a pattern. This should be faster too. if( ctype_digit($number) && $number > 17 && $number < 100 ) { personally I don't like that. I would have done this: if((int)$number > 17) { Quote Link to comment Share on other sites More sharing options...
salathe Posted October 6, 2011 Share Posted October 6, 2011 if( ctype_digit($number) && $number > 17 && $number < 100 ) { if((int)$number > 17) { $number = "00000000000000000000000000000000000000000000042"; Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 6, 2011 Share Posted October 6, 2011 && strlen == 2 rather than $number < 100 Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 6, 2011 Share Posted October 6, 2011 if( ctype_digit($number) && $number > 17 && $number < 100 ) { if((int)$number > 17) { $number = "00000000000000000000000000000000000000000000042"; They both worked... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.