Perad Posted May 4, 2009 Share Posted May 4, 2009 Hello, Could someone show me what line of code I would need to add to a script to check a string to see if it contains anything other than 0-9, a-z, A-Z Thanks Quote Link to comment https://forums.phpfreaks.com/topic/156757-no-illegal-characters/ Share on other sites More sharing options...
jackpf Posted May 4, 2009 Share Posted May 4, 2009 '/[^a-zA-Z0-9]/' I think would do it. Or if(preg_match('\W', $string)) would match any non-word characters. Quote Link to comment https://forums.phpfreaks.com/topic/156757-no-illegal-characters/#findComment-825471 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Or if(preg_match('\W', $string)) would match any non-word characters. Wouldn't that just match one non-word character? Same with your other Regex. It only match one character. Quote Link to comment https://forums.phpfreaks.com/topic/156757-no-illegal-characters/#findComment-825657 Share on other sites More sharing options...
jackpf Posted May 4, 2009 Share Posted May 4, 2009 Yeah, but if the string contains one letter that isn't alphanumeric then it's not alphanumeric duhh And it should match all non-alphanumeric chars anyway. Oh, and I made a mistake on the second one, it should be '/\W/' (with delimiters). My bad. Quote Link to comment https://forums.phpfreaks.com/topic/156757-no-illegal-characters/#findComment-825667 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Don't you mean: /\W+/ ? Or possibly: /\W*/ Quote Link to comment https://forums.phpfreaks.com/topic/156757-no-illegal-characters/#findComment-825675 Share on other sites More sharing options...
jackpf Posted May 4, 2009 Share Posted May 4, 2009 Well it doesn't matter. It could contain 1 full stop or ten thousand hiphens, it would still contain anything other than 0-9, a-z, A-Z But yeah, either would work Quote Link to comment https://forums.phpfreaks.com/topic/156757-no-illegal-characters/#findComment-825679 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Oh right. My sincere apology. Early in the morning. Hehe... Quote Link to comment https://forums.phpfreaks.com/topic/156757-no-illegal-characters/#findComment-825689 Share on other sites More sharing options...
jackpf Posted May 4, 2009 Share Posted May 4, 2009 Lol. Half three here Quote Link to comment https://forums.phpfreaks.com/topic/156757-no-illegal-characters/#findComment-825693 Share on other sites More sharing options...
nrg_alpha Posted May 4, 2009 Share Posted May 4, 2009 The problem with the involvement of \w is that this also includes the underscore, as well as characters like é, à etc (depending on the locale from my understanding). So the use \W, won't eliminate those characters (which falls outside of a-zA-Z0-9). jackpf, you're initial character would do it. One could also use: #[^a-z0-9]#i or one could make use of !ctype_alnum as well... Quote Link to comment https://forums.phpfreaks.com/topic/156757-no-illegal-characters/#findComment-825781 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Wouldn't this be good? preg_match('/\W/', $str, $match); if (empty($match)) // all valid Quote Link to comment https://forums.phpfreaks.com/topic/156757-no-illegal-characters/#findComment-825789 Share on other sites More sharing options...
nrg_alpha Posted May 4, 2009 Share Posted May 4, 2009 My bad on !ctype_alnum.. (as this includes characters like é...[perhaps again, locale dependent?]). Wouldn't this be good? preg_match('/\W/', $str, $match); if (empty($match)) // all valid You tell me $str = '123aBc_dé'; echo $str . "<br />\n"; echo (preg_match('/\W/', $str))? 'Does not only contain a-zA-Z0-9' : 'Does only contain a-zA-Z0-9'; Quote Link to comment https://forums.phpfreaks.com/topic/156757-no-illegal-characters/#findComment-825800 Share on other sites More sharing options...
nrg_alpha Posted May 4, 2009 Share Posted May 4, 2009 And of course, both ctype_alnum and \w also include exponents (1E²7³). So from where I see it, the only difference I can tell between ctype_alnum and \w is that cytpe_alnum lacks support for underscore.. so in either case, I would personally stick to #[^a-z0-9]#i Damn you, ctype_alnum! *shakes fist* EDIT - I guess one would have to set the proper locale for the desired effect for cype_alnum. Quote Link to comment https://forums.phpfreaks.com/topic/156757-no-illegal-characters/#findComment-825819 Share on other sites More sharing options...
jackpf Posted May 4, 2009 Share Posted May 4, 2009 preg_match('/\W/', $str, $match); if (empty($match)) // all valid I don't think that would work since $match would be an array. Idk if empty() checks arrays. If it does, forgive me. If it doesn't, I think preg_match('/\W/', $str, $match); if (count($match) == 0) // all valid would be better. Or not. Idk Quote Link to comment https://forums.phpfreaks.com/topic/156757-no-illegal-characters/#findComment-825846 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 empty checks a mixed var. So pretty much anything. Quote Link to comment https://forums.phpfreaks.com/topic/156757-no-illegal-characters/#findComment-825848 Share on other sites More sharing options...
jackpf Posted May 4, 2009 Share Posted May 4, 2009 Oh right. Well ignore me then. Quote Link to comment https://forums.phpfreaks.com/topic/156757-no-illegal-characters/#findComment-825859 Share on other sites More sharing options...
premiso Posted May 4, 2009 Share Posted May 4, 2009 If you want to do a check, why not use the return of the preg_match Return Values preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match_all() on the contrary will continue until it reaches the end of subject . preg_match() returns FALSE if an error occurred. So... preg_match('/\W/', $str, $match); if (count($match) == 0) // all valid Becomes: if (preg_match('/\W/', $str, $match) > 0) // all valid There is a reason they gave it a return value Quote Link to comment https://forums.phpfreaks.com/topic/156757-no-illegal-characters/#findComment-825993 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 Hehe... yeah I did that really early in the morning. You caught me. Quote Link to comment https://forums.phpfreaks.com/topic/156757-no-illegal-characters/#findComment-826511 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.