hankknight
asked on
PHP/REGEX: Number greater than 12 in specific place
Using PHP and REGEX, how can I test if a string contains a number greater than immediately after "Item_"?
testAbove12('xyz/abc/Item_12.txt'); // true
testAbove12('xyz/abc/Item_5.txt'); // false
testAbove12('hello world Item_234.txt'); // true
testAbove12('ABC Item_234 HELLO world'); // true
testAbove12('xyz/abc/Item.txt'); // false
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
\d+ means one or more digits, it is shorthand for [0-9]+
Try this:
Try this:
function testAbove12($str) {
return preg_match('@/Item_(\d+)@',$str,$m) ? ((int)$m[1]>=12) : false;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
[2-9][0-9]|1[2-9]|11[0-9]
Is there a way to do something like
[0-9+]
?