xProteuSx Posted December 21, 2007 Share Posted December 21, 2007 What would be an 'eregi()' pattern for a string that is: - alphanumeric - 4-12 characters I'm a little confused?? My book is only for PHP version 4. Most online resources are useless. Would this be it? eregi([a-z]+[A-Z]+[0-9]+{4,12},$string) Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 21, 2007 Share Posted December 21, 2007 I don't know about ereg but preg would be equivalent would be preg_match("/[a-zA-Z0-9]{4,12}/i",$string) Quote Link to comment Share on other sites More sharing options...
xProteuSx Posted December 21, 2007 Author Share Posted December 21, 2007 So are you saying that something like this would work? if (!preg_match("/[a-zA-Z0-9]{4,12}/i",$string) { echo 'You have entered an invalid character, or your sting is not 4-12 characters long.'; } Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 21, 2007 Share Posted December 21, 2007 Yep that would do Quote Link to comment Share on other sites More sharing options...
Dragen Posted December 21, 2007 Share Posted December 21, 2007 I believe the ereg would be: if (!eregi("^[a-z0-9]{4,12}4", $string) { echo 'You have entered an invalid character, or your sting is not 4-12 characters long.'; } which is pretty similar. I think preg_match is supposed to be quicker anyway. Quote Link to comment Share on other sites More sharing options...
xProteuSx Posted December 26, 2007 Author Share Posted December 26, 2007 When you use the following code: if (!eregi("^[a-z0-9]{4,12}4", $string) What does the second '4' mean? The one here >> }4" << Quote Link to comment Share on other sites More sharing options...
xProteuSx Posted December 26, 2007 Author Share Posted December 26, 2007 Also, I with this string, does it accept upper case characters? if (!eregi("^[a-z0-9]{4,12}4", $string)) or do I have to do this: if (!eregi("^[a-zA-Z0-9]{4,12}4", $string)) ?? Quote Link to comment Share on other sites More sharing options...
Dragen Posted December 26, 2007 Share Posted December 26, 2007 sorry the four was supposed to be a $. Yes it's not case sensitive as it's using 'eregi', not 'ereg' if (!eregi("^[a-z0-9]{4,12}$", $string) { echo 'You have entered an invalid character, or your sting is not 4-12 characters long.'; } if you want only lower case use this: if (!ereg("^[a-z0-9]{4,12}$", $string) { echo 'You have entered an invalid character, or your sting is not 4-12 characters long.'; } or for only caps switch the a-z, with A-Z Quote Link to comment Share on other sites More sharing options...
xProteuSx Posted December 26, 2007 Author Share Posted December 26, 2007 For those who are following this thread ... If you can't get this to work: if (!ereg("^[a-zA-Z0-9]{4,12}$", $string) { echo 'You have entered an invalid character, or your sting is not 4-12 characters long.'; } This should fix it: if (!ereg("^([a-zA-Z0-9]){4,12}$", $string) { echo 'You have entered an invalid character, or your sting is not 4-12 characters long.'; } That's just something that I found. I had the first snippet work on one page, but not on another. I did not take the time to figure out why, because this second snippet was the fix and I'm short on time. Quote Link to comment Share on other sites More sharing options...
Dragen Posted December 26, 2007 Share Posted December 26, 2007 ah yeah.. another thing I missed out.. the brackets are neccessary. That's what I get for writing code at ridiculous times at night Quote Link to comment Share on other sites More sharing options...
dsaba Posted December 27, 2007 Share Posted December 27, 2007 Most online resources are useless Not true. And still, isn't phpfreaks on online resource that you're using? Are we useless? Quote Link to comment Share on other sites More sharing options...
Dragen Posted December 27, 2007 Share Posted December 27, 2007 And still, isn't phpfreaks on online resource that you're using? Are we useless? no. we're just clumsy.. well, I am at any rate Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.