• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 628
  • Last Modified:

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

Open in new window

0
hankknight
Asked:
hankknight
5 Solutions
 
thehagmanCommented:
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
 
hankknightAuthor Commented:
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
 
Roger BaklundCommented:
\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;
  }

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Roger BaklundCommented:
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

Open in new window


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
 
Mohamed AbowardaSoftware EngineerCommented:
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;
}

Open in new window

0
 
Ray PaseurCommented:
Please see http://www.laprbass.com/RAY_temp_hankknight.php

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

Open in new window

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
 
skullnobrainsCommented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now