markos
11-05-2007, 01:23 AM
I've used PHP for a few years (self taught), but rarely touched regular expression. It's something I now feel I should tackle.
I've got the following:-
if ($price[1]) {
if (preg_match('/^[0-9]{1,}/', $price[1])) {
echo $price[1] . " is numeric";
} else {
echo $price[1] . " is NOT numeric";
}
}
And when I try it out I get:-
1 = "1 is numeric"
a = "a is NOT numeric"
So far so good, but...
1a1 = "1a1 is numeric"
Which is obviously wrong.
I'm reading the script as if it says it should match up with 1 or more numbers (i.e. 0 through 9), and if it does to echo the "numeric" response, otherwise the "NOT numeric" response. I can't understand why it fails to correctly identify 1a1 as NOT numeric. Can anyone enlighten me?
I've got the following:-
if ($price[1]) {
if (preg_match('/^[0-9]{1,}/', $price[1])) {
echo $price[1] . " is numeric";
} else {
echo $price[1] . " is NOT numeric";
}
}
And when I try it out I get:-
1 = "1 is numeric"
a = "a is NOT numeric"
So far so good, but...
1a1 = "1a1 is numeric"
Which is obviously wrong.
I'm reading the script as if it says it should match up with 1 or more numbers (i.e. 0 through 9), and if it does to echo the "numeric" response, otherwise the "NOT numeric" response. I can't understand why it fails to correctly identify 1a1 as NOT numeric. Can anyone enlighten me?