homepage Welcome to WebmasterWorld Guest from 23.22.173.58
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




msg:3063734
 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




msg:3063747
 11:54 pm on Aug 28, 2006 (gmt 0)

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

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

Psychopsia




msg:3063762
 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




msg:3063771
 12:34 am on Aug 29, 2006 (gmt 0)

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

smells so good




msg:3063773
 12:38 am on Aug 29, 2006 (gmt 0)

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

It's a quick fix.

ramoneguru




msg:3063776
 12:41 am on Aug 29, 2006 (gmt 0)

Cool-ness!
--Nick

UserFriendly




msg:3064758
 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




msg:3064790
 7:26 pm on Aug 29, 2006 (gmt 0)

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

heisters




msg:3065911
 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




msg:3065938
 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




msg:3066214
 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




msg:3066806
 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