a1ias Posted September 26, 2010 Share Posted September 26, 2010 Been playing with this since early this fine Sunday morning and have decided to turn to the pro's for a little pointer. The code I am having an issue with is as follows:- <?php $checkthis = 'a41$;wilsond'; if (preg_match('[^a-zA-Z0-9]', $checkthis)) { $checked = 'Bad String'; } else { $checked = 'Good String'; } echo $checked; ?> What I am expecting that snippet to do is check the variable $checkthis for alphanumeric characters only, and since it contains both a $ and a ; I expect $checked to be returned as 'Bad String', which appears not to be the case...doh! I have no doubt that I have made a simple beginners error, but am failing to understand why it does not work. Any pointers are greatly appreciated. a1ias Quote Link to comment https://forums.phpfreaks.com/topic/214434-beginner-preg_match-simple-query-i-hope/ Share on other sites More sharing options...
Pikachu2000 Posted September 26, 2010 Share Posted September 26, 2010 You omitted delimiters . . . if (preg_match('~[^a-zA-Z0-9]~', $checkthis)) Quote Link to comment https://forums.phpfreaks.com/topic/214434-beginner-preg_match-simple-query-i-hope/#findComment-1115867 Share on other sites More sharing options...
a1ias Posted September 26, 2010 Author Share Posted September 26, 2010 Many many thanks for the help. I've also discovered that if (preg_match('/[^a-zA-Z0-9]/', $checkthis)) works. Is there a big difference between either of them and, as any documentation I have on regex is sketchy to say the least, what do those delimiters actually represent, if I may ask? Quote Link to comment https://forums.phpfreaks.com/topic/214434-beginner-preg_match-simple-query-i-hope/#findComment-1115895 Share on other sites More sharing options...
Pikachu2000 Posted September 26, 2010 Share Posted September 26, 2010 The delimiters can be be any non-alphanumeric character. Some people use slashes, some use other characters. You just need to use the same leading and trailing delimiter. It simply denotes the start and end of the pattern, so the one I posted and the one you posted after are effectively identical. Quote Link to comment https://forums.phpfreaks.com/topic/214434-beginner-preg_match-simple-query-i-hope/#findComment-1115917 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.