livethedead Posted February 17, 2012 Share Posted February 17, 2012 I realise I can search the string by using preg_match_all (%+[a-zA-Z0-9]%, $string) but that's only matching a-z 0-9 not telling me if there's some σx or something else funky. Much obliged. Quote Link to comment https://forums.phpfreaks.com/topic/257152-only-allowing-alpha-numeric/ Share on other sites More sharing options...
Pikachu2000 Posted February 17, 2012 Share Posted February 17, 2012 You haven't actually asked a question. Quote Link to comment https://forums.phpfreaks.com/topic/257152-only-allowing-alpha-numeric/#findComment-1318204 Share on other sites More sharing options...
livethedead Posted February 17, 2012 Author Share Posted February 17, 2012 Sorry that was unclear, I meant how to check if anything other than a-z 0-9 is in the string. I figured it out though incase any other newbie runs across the same question. If there's a better way I'm all for improving. <?php $regex = preg_match_all("%[a-zA-Z0-9]%", $str, $array); if (strlen($str) > $regex) { echo "only alpha or numeric combinations are allowed"; } else { echo "success!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/257152-only-allowing-alpha-numeric/#findComment-1318205 Share on other sites More sharing options...
Pikachu2000 Posted February 17, 2012 Share Posted February 17, 2012 You don't need to use a regex pattern for that at all. if( ctype_alnum(str_replace(' ', '', $str)) ) { // only alphanumeric chars and spaces are in the string. } Quote Link to comment https://forums.phpfreaks.com/topic/257152-only-allowing-alpha-numeric/#findComment-1318217 Share on other sites More sharing options...
livethedead Posted February 17, 2012 Author Share Posted February 17, 2012 Ahh thanks, google you have failed me! /sigh I was all stoked I even made my first function out of that code lol. Thanks much. Quote Link to comment https://forums.phpfreaks.com/topic/257152-only-allowing-alpha-numeric/#findComment-1318220 Share on other sites More sharing options...
Pikachu2000 Posted February 17, 2012 Share Posted February 17, 2012 Generally, if you can accomplish a task with simple string functions, then don't bother with a regex pattern. Quote Link to comment https://forums.phpfreaks.com/topic/257152-only-allowing-alpha-numeric/#findComment-1318222 Share on other sites More sharing options...
AyKay47 Posted February 17, 2012 Share Posted February 17, 2012 Generally, if you can accomplish a task with simple string functions, then don't bother with a regex pattern. yes "livethedead" I agree with pika. While I do love regex, if there is a simple PHP functions solution to the problem, I would always go with that. Especially with large strings such as text files etc. They tend to be much more efficient than using a regular expression in most cases. It is only in special cases where the solution cannot be done with simple PHP functions or it would be way too much of an effort to do so where a regex should be used. Quote Link to comment https://forums.phpfreaks.com/topic/257152-only-allowing-alpha-numeric/#findComment-1318323 Share on other sites More sharing options...
livethedead Posted February 17, 2012 Author Share Posted February 17, 2012 Yeah I was thinking of that while I wrote it but, I couldn't seem to find that function so I ended up using regex, thinking it was my next-best choice. Quote Link to comment https://forums.phpfreaks.com/topic/257152-only-allowing-alpha-numeric/#findComment-1318359 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.