Solved

PHP/REGEX: Number greater than 12 in specific place

Posted on 2012-08-31
586 Views
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
``````
0
Question by:hankknight

LVL 20

Assisted Solution

Use the regular expression /Item_0*([2-9][0-9]|1[2-9]|11[0-9])/. You may drop the "0*" if you do not expect leading zeroes.
If there is any chance you might need the actual number and you want to be able to modify the code in a simply way for numbers other than 12, I suggest to simply match for /Item_(\+d)/ and check the value of matches\$[1]
0

LVL 16

Author Comment

Why do I need all this?
[2-9][0-9]|1[2-9]|11[0-9]

Is there a way to do something like
[0-9+]
?
0

LVL 39

Expert Comment

\d+ means one or more digits, it is shorthand for [0-9]+

Try this:

``````function testAbove12(\$str) {
return preg_match('@/Item_(\d+)@',\$str,\$m) ? ((int)\$m[1]>=12) : false;
}
``````
0

LVL 39

Assisted Solution

Why do I need all this?
[2-9][0-9]|1[2-9]|11[0-9]

It is an attempt to match a pattern that is a number >= 12.

``````[2-9][0-9]  |     1[2-9]  |    11[0-9]
20 - 99     or    12-19   or   110-119
``````

There is a mistake here, it matches 110-119 but not 100-109 or 120 and higher. It should probably rather be [0-9][0-9][0-9]+ or [0-9]{3,} at the end, allowing any three or more digit numbers.
0

LVL 12

Assisted Solution

You don't have to use RegEx to do this, try:
``````function testAbove12(\$str)
{
\$pos = strpos(strtolower(\$str), 'item_');
if (\$pos !== false)
{
if (is_numeric(substr(\$str, \$pos + 5, 1)))
return true;
}
return false;
}
``````
0

LVL 107

Assisted Solution

``````<?php // RAY_temp_hankknight.php
error_reporting(E_ALL);

// TEST CASES FROM THE POST AT EE
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

// PLUS ONE
testabove12('foo'); // FALSE

// A FUNCTION DEFINITION
function testabove12(\$str, \$sig='ITEM')
{
// NORMALIZE TO UPPER CASE
\$ups = strtoupper(\$str);

// ISOLATE 'ITEM' AND NUMBER
if (strpos(\$ups, \$sig) !== FALSE)
{
\$arr = explode('ITEM', \$ups);
\$nbr = preg_replace('/[^0-9]/', NULL, \$arr[1]);
if (\$nbr >= 12)
{
echo "<br/>TRUE: \$str";
return TRUE;
}
else
{
echo "<br/>FALSE: \$str";
return FALSE;
}
}
else
{
echo "<br/>WRONG: \$str";
return FALSE;
}
}
``````
You might want to rename the function, since it is really looking for 12 and above, not just above 12.  I think if you remove the echo statements from this snippet you'll have a TRUE/FALSE function.  HTH, ~Ray
0

LVL 25

Accepted Solution

i just pasted this as a private joke, but it seems it would do the trick, indeed

foreach(\$l as \$k => \$v)if(!substr(strrchr(\$v,'Item_'),5) > 12)unset(\$l[\$k]);
0

Featured Post

A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.