homepage Welcome to WebmasterWorld Guest from 54.145.238.55
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Age regular expression
I have the code, but it only kinda works.
ramoneguru

5+ Year Member



 
Msg#: 3063732 posted 11:26 pm on Aug 28, 2006 (gmt 0)

Ok, using this regualr expression to check to see if an age is between 1-105:

"([1-9][0-9]?)¦([10][1-6])"

Does not work, any suggestions?
--Nick

 

BjarneDM

10+ Year Member



 
Msg#: 3063732 posted 11:54 pm on Aug 28, 2006 (gmt 0)

"([1-9][0-9]?)(10[1-6])"

[10] = either 1 or 0 ; not "10"

Psychopsia

5+ Year Member



 
Msg#: 3063732 posted 12:18 am on Aug 29, 2006 (gmt 0)

You can do this with a simple condition:

$age = 50;
if ($age >= 1 && $age <= 105)
{
// Code
}
else
{
// Your age is invalid
}

ramoneguru

5+ Year Member



 
Msg#: 3063732 posted 12:34 am on Aug 29, 2006 (gmt 0)

That's fine, but it blows up with non integer characters.
--Nick

smells so good

5+ Year Member



 
Msg#: 3063732 posted 12:38 am on Aug 29, 2006 (gmt 0)

$age = <non-integer character> * 1;

It's a quick fix.

ramoneguru

5+ Year Member



 
Msg#: 3063732 posted 12:41 am on Aug 29, 2006 (gmt 0)

Cool-ness!
--Nick

UserFriendly

5+ Year Member



 
Msg#: 3063732 posted 6:54 pm on Aug 29, 2006 (gmt 0)

A simple way to check that the string only contains digits is as follows:

if (ctype_digit($age)) {
echo 'Age string contains only numbers.';
}

Psychopsia

5+ Year Member



 
Msg#: 3063732 posted 7:26 pm on Aug 29, 2006 (gmt 0)

if (!preg_match('#([0-9]+)#i', $str))
{
// Not Number
}

heisters

5+ Year Member



 
Msg#: 3063732 posted 3:11 pm on Aug 30, 2006 (gmt 0)

Not that a regex is a good solution for this problem, but...

#([0-9]+)#i

would be better written as

/\d+/

\d is a synonym for [0-9], grouping (parentheses) is unnecessary, and reduces performance. Using Perl Regexp in PHP is generally un-optimal; better to use lower level string functions or integer casting and a regular conditional.

Also, just for readability, it might be better to use real type casting:

$age = (int) $nonIntChar;

instead of

$age = <non-integer character> * 1

But both those points are splitting hairs ;)

coopster

WebmasterWorld Administrator coopster us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3063732 posted 3:26 pm on Aug 30, 2006 (gmt 0)


Using Perl Regexp in PHP is generally un-optimal; better to use lower level string functions or integer casting and a regular conditional.

I used to think this as well. As a matter of fact, most of the PHP function pages will even state as such. However, in my recent testing with PHP5 I have found PCRE to be faster. Perhaps it is the new engine or otherwise but I just wanted to share my experiences.

whoisgregg

WebmasterWorld Senior Member whoisgregg us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3063732 posted 5:55 pm on Aug 30, 2006 (gmt 0)

Or force an integer from any string with:

$age = intval($_POST['age']);
// OR
$age = (int) $_POST['age'];

Then do the <= && >= tests.

Filipe

10+ Year Member



 
Msg#: 3063732 posted 3:07 am on Aug 31, 2006 (gmt 0)

For future reference: Search Google for "regular expression tool". There are a lot of handy regex builders. A site called reWork has one I particularly like.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved