Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.