xcandiottix Posted June 11, 2010 Share Posted June 11, 2010 $me = 'cheese'; if(preg_replace('/[a-zA-Z0-9]/', '', $me)){ echo "Bad Characters"; } else { echo "Passed"; } -Keep getting it passed when the string cheese has letters in it which should fail the test. Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/204461-correct-code-for-alerting-user-that-form-is-improperly-filled-out/ Share on other sites More sharing options...
Karlos94 Posted June 11, 2010 Share Posted June 11, 2010 Maybe you are looking for the preg_match() function instead of the preg_replace(). What your doing is if the preg_replace() function returns true (replacing your letters with nothing), it will echo out bad characters. Quote Link to comment https://forums.phpfreaks.com/topic/204461-correct-code-for-alerting-user-that-form-is-improperly-filled-out/#findComment-1070698 Share on other sites More sharing options...
xcandiottix Posted June 11, 2010 Author Share Posted June 11, 2010 No, preg_match() gets me a "Passed" result, too. Match does make more sense though, I'll try running with it. Quote Link to comment https://forums.phpfreaks.com/topic/204461-correct-code-for-alerting-user-that-form-is-improperly-filled-out/#findComment-1070701 Share on other sites More sharing options...
codebyren Posted June 11, 2010 Share Posted June 11, 2010 Your code didn't work like I expected it to either... But I've never actually used preg_replace so that's not saying much. You could try: $string = 'cheese'; $pattern = '/[a-zA-Z0-9]/'; echo preg_match($pattern, $string) ? 'matched' : 'not matched'; This will show that the string matched. Hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/204461-correct-code-for-alerting-user-that-form-is-improperly-filled-out/#findComment-1070712 Share on other sites More sharing options...
xcandiottix Posted June 12, 2010 Author Share Posted June 12, 2010 behicthebuilder, thank you very much. This worked very well. $pattern = '/[~!#$%^&*()+=|:";\'<>,/*+]/'; $check = preg_match($pattern, $a) ? 1 : 0; if ($check == 1){ $error = true; $status = "Please only use Alphanumeric characters (A-Z, a-z, 0-9). Underscores and dashes are allowed (_,-)."; } if ($check == 0){ //no illegal characerts found } Now, in my blocked characters list, would anyone know how could I add {}[] without the script thinking that i'm closing the encapsulating [ ]'s ? Quote Link to comment https://forums.phpfreaks.com/topic/204461-correct-code-for-alerting-user-that-form-is-improperly-filled-out/#findComment-1070976 Share on other sites More sharing options...
jcbones Posted June 12, 2010 Share Posted June 12, 2010 Escape them with a slash ( \ ). If you are going to that trouble, why not just check to see if the string contains only alphaNumeric + underscore and dashes. $pattern = '~^[a-zA-Z0-9\s_\-]+$~'; //Matches any string with spaces, underscore, dashes, or alphanumeric. Quote Link to comment https://forums.phpfreaks.com/topic/204461-correct-code-for-alerting-user-that-form-is-improperly-filled-out/#findComment-1070984 Share on other sites More sharing options...
xcandiottix Posted June 12, 2010 Author Share Posted June 12, 2010 I've tried to find a good how-to for how to add characters to this but I haven't had luck. Even what you posted: '~^[a-zA-Z0-9\s_\-]+$~' Why 2 ~, why come in brackets and others not, why \s_ ? I'm having problems grasping the logic of this statement :-\ On the other hand! I followed your advice and checking to make sure that the fields only contain your string. Which works fine. I just hate to not understand why it works. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/204461-correct-code-for-alerting-user-that-form-is-improperly-filled-out/#findComment-1070987 Share on other sites More sharing options...
jcbones Posted June 12, 2010 Share Posted June 12, 2010 The (~) is de-limiters, It is like yours except you are using slashes. Only when delimiting the slashes, you must escape the escaping slash. Just adds more slashes, like (\\{). I'm probably not being clear. Explanation of the string. ~ delimiter ^ matches beginning of string [] encloses a set. a-z matches any lowercase alpha character A-Z matches any uppercase alpha character \s matches any space _ matches any underscore \- matches any dash + matches 1 or more of the previous expression $ matches the end of the string ~ ending delimiter. Quote Link to comment https://forums.phpfreaks.com/topic/204461-correct-code-for-alerting-user-that-form-is-improperly-filled-out/#findComment-1070988 Share on other sites More sharing options...
xcandiottix Posted June 12, 2010 Author Share Posted June 12, 2010 Ahh that's great. Very clear, I understand that. That'll be notepadded for sure Thanks very much Quote Link to comment https://forums.phpfreaks.com/topic/204461-correct-code-for-alerting-user-that-form-is-improperly-filled-out/#findComment-1070993 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.