long time listener. First time caller…
Not strictly a PHP question as it involves regular expressions but this one has got me tearing my hair out.
I have 3 regular expressions that I want to create, and only one is working correctly.
Now I am not sure whether this is due to the fact that:
- I don’t understand preg_match and
ereg and their return codes as I
haven’t worked in PHP for about 7
years. - My regular expressions are
just plain wrong. - I am mentally disabled.
Either way here are the expressions and my feeble attempts in making them work.
1) Match any number starting with 2,3,4 or 5 then followed by 5 digits. (This one I think works)
code:
if (!ereg('/[2-5]\d{5}/', $_POST['packageNumber' )
{
echo "The package number is not the correct format.";
}
2) Match any number starting with 2,3,4 or 5 then followed by 5 digits then a period then a 1 or a 2.
if (!ereg("/[2-5]\d{5}\.[1-2]/", $_POST['packageModifier' )
{
echo "The package modifier is not the correct format.";
}
3) Match any combination of alphanumerics, spaces,periods and hypens up to 50 characters.
if (!ereg("/[0-9a-zA-Z\s\-\.]{0,50}/", $_POST['customerNumber' )
{
echo "The customer number is not the correct format.";
}
If anyone can please tell me what I am doing wrong I’ll give them my first born.
I’m assuming that you just missed off the closing ] on the $_POSTS and i’ve added in anchors for the start and end of the lines and used preg_match.
If you don’t anchor it and the pattern is matched anywhere in the string then the entire thing will match. For example.
“dfasfasfasfasf25555555as5f15sdsdasdsfghfsgihfughd54” would be matched if the first one was not anchored.
Number One
Number Two
Number Three