the_shadow Posted December 27, 2007 Share Posted December 27, 2007 Hi all, I'm new to using regex, but basically I just want to verify that a variable contains either letters or numbers and nothing else (had my own function, but thought there would be any easier way of doing this). I've searched around and came up with these expressions: ereg("^[A-Za-z0-9]*$", $test) preg_match("/[a-zA-Z0-9]{0,}/",$test) They both seem to work alright except the only problem I'm having is that both of them accept a dollar sign ($) after a number and before a letter as well as between letters in the $test string. $test = "a324$sfs32sf"; //$test = "a324s$fs32sf"; // also accepts the $ sign if (ereg("^[A-Za-z0-9]*$", $test)) { echo "ereg"; } if (preg_match("/[a-zA-Z0-9]{0,20}/",$test)) { echo "pregmatch"; } Can anyone offer some suggestions as to why this is happening? Thank you. Quote Link to comment Share on other sites More sharing options...
dsaba Posted December 27, 2007 Share Posted December 27, 2007 $test = "a324$sfs32sf"; echo "hi$bob"; will echo: hi it will also echo a warning on a variable that has not been set with is $bob, if you have your warnings turned on do you see why? using the $ in double quotes it will try to echo the variable change it to: $test = 'a324$sfs32sf' Quote Link to comment Share on other sites More sharing options...
the_shadow Posted December 28, 2007 Author Share Posted December 28, 2007 Thanks! Can't believe I didn't pick that up 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.