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 Link to comment https://forums.phpfreaks.com/topic/248385-regex-for-two-digit-number-18-or-over/ 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 Link to comment https://forums.phpfreaks.com/topic/248385-regex-for-two-digit-number-18-or-over/#findComment-1275537 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... ? Link to comment https://forums.phpfreaks.com/topic/248385-regex-for-two-digit-number-18-or-over/#findComment-1275548 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"; } Link to comment https://forums.phpfreaks.com/topic/248385-regex-for-two-digit-number-18-or-over/#findComment-1275743 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» Link to comment https://forums.phpfreaks.com/topic/248385-regex-for-two-digit-number-18-or-over/#findComment-1276211 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 ) { Link to comment https://forums.phpfreaks.com/topic/248385-regex-for-two-digit-number-18-or-over/#findComment-1276220 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) { Link to comment https://forums.phpfreaks.com/topic/248385-regex-for-two-digit-number-18-or-over/#findComment-1276236 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"; Link to comment https://forums.phpfreaks.com/topic/248385-regex-for-two-digit-number-18-or-over/#findComment-1276384 Share on other sites More sharing options...
Pikachu2000 Posted October 6, 2011 Share Posted October 6, 2011 && strlen == 2 rather than $number < 100 Link to comment https://forums.phpfreaks.com/topic/248385-regex-for-two-digit-number-18-or-over/#findComment-1276460 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... Link to comment https://forums.phpfreaks.com/topic/248385-regex-for-two-digit-number-18-or-over/#findComment-1276497 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.