vesper Posted November 19, 2009 Share Posted November 19, 2009 Hi, I'm after what I'm hoping is a quick bit of help. I have the following in my code: preg_match("/[^a-zA-Z0-9]+$/s", $variableName) I got this code from an example which I was lead to believe only allows letters and numbers. However, it is also allowing underscores. Why is it doing this? Thanks, V. Quote Link to comment https://forums.phpfreaks.com/topic/182153-solved-preg-match-allowing-underscore/ Share on other sites More sharing options...
Alex Posted November 19, 2009 Share Posted November 19, 2009 Inside of a class ([ ]) ^ means 'not'. So [^a-zA-Z0-9]+ means anything that is not a-zA-Z0-9. Quote Link to comment https://forums.phpfreaks.com/topic/182153-solved-preg-match-allowing-underscore/#findComment-961031 Share on other sites More sharing options...
vesper Posted November 19, 2009 Author Share Posted November 19, 2009 That's right. If it matches anything that isn't a-z, A-Z or 0-9 it gives an error. I've just removed the +$ and that seems to have cured it. Need to test it further though. Quote Link to comment https://forums.phpfreaks.com/topic/182153-solved-preg-match-allowing-underscore/#findComment-961034 Share on other sites More sharing options...
cags Posted November 19, 2009 Share Posted November 19, 2009 "[^a-zA-Z0-9]+$/s" Since $ matches against the end, your code would have only returned an error if there were one or more characters that weren't a-zA-Z0-9 directly before the end of the input. Quote Link to comment https://forums.phpfreaks.com/topic/182153-solved-preg-match-allowing-underscore/#findComment-961036 Share on other sites More sharing options...
vesper Posted November 19, 2009 Author Share Posted November 19, 2009 "[^a-zA-Z0-9]+$/s" Since $ matches against the end, your code would have only returned an error if there were one or more characters that weren't a-zA-Z0-9 directly before the end of the input. I see. So should removing +$ from the expression, eg. preg_match("/[^a-zA-Z0-9]/s", $variableName) now only allow a-z, A-Z and 0-9 as intended? Or should I continue testing it? Quote Link to comment https://forums.phpfreaks.com/topic/182153-solved-preg-match-allowing-underscore/#findComment-961041 Share on other sites More sharing options...
cags Posted November 19, 2009 Share Posted November 19, 2009 That would depend on how you implement your boolean logic. The most common way (IMO) of ensuring something only contains standard letters and numbers would be. if(preg_match("#^[a-z0-9]+$#i", $input)) { echo "Valid"; } You appear to be doing the opposite... if(preg_match("#[^a-zA-Z0-9]#", $input)) { echo "Not Valid"; } ...which should still work. Quote Link to comment https://forums.phpfreaks.com/topic/182153-solved-preg-match-allowing-underscore/#findComment-961045 Share on other sites More sharing options...
vesper Posted November 19, 2009 Author Share Posted November 19, 2009 That would depend on how you implement your boolean logic. The most common way (IMO) of ensuring something only contains standard letters and numbers would be. if(preg_match("#^[a-z0-9]+$#i", $input)) { echo "Valid"; } You appear to be doing the opposite... if(preg_match("#[^a-zA-Z0-9]#", $input)) { echo "Not Valid"; } ...which should still work. Hi cags, I've just tried your example and it gives an error if you only use letter and numbers (which is the opposite to what I want). Should there be a ! before preg_match? Quote Link to comment https://forums.phpfreaks.com/topic/182153-solved-preg-match-allowing-underscore/#findComment-961057 Share on other sites More sharing options...
cags Posted November 19, 2009 Share Posted November 19, 2009 Is your code exactly the same as mine? As I said it depends on how you evaluate the value returned by preg_match. The first example I gave will return true if it's a valid string. Hence the fact I check if it's true, then it's valid. The second example will return true if it finds an unwanted character so a returned value of true means it's not valid. Quote Link to comment https://forums.phpfreaks.com/topic/182153-solved-preg-match-allowing-underscore/#findComment-961059 Share on other sites More sharing options...
vesper Posted November 19, 2009 Author Share Posted November 19, 2009 Is your code exactly the same as mine? As I said it depends on how you evaluate the value returned by preg_match. The first example I gave will return true if it's a valid string. Hence the fact I check if it's true, then it's valid. The second example will return true if it finds an unwanted character so a returned value of true means it's not valid. Sorry, cags, my fault. I was looking for it to return an error, so I should have been using the bottom example, not the top one. (I used the top one first, then tried it with a ! before preg_match.) Quote Link to comment https://forums.phpfreaks.com/topic/182153-solved-preg-match-allowing-underscore/#findComment-961060 Share on other sites More sharing options...
cags Posted November 19, 2009 Share Posted November 19, 2009 Yer, placing an exclamation mark before it will obviously flip the result, I guess it depends on how you word the question as to which one is correct. If you consider the topic solved, don't forget to click the 'Topic Solved' button, it appears in the bottom left hand corner of threads you started. Quote Link to comment https://forums.phpfreaks.com/topic/182153-solved-preg-match-allowing-underscore/#findComment-961070 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.