Jump to content

Regex for two digit number 18 or over


giraffemedia

Recommended Posts

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


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»

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) {

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.