Brandon Jaeger Posted October 2, 2008 Share Posted October 2, 2008 Hey guys, I have a simple question about a RegEx pattern. ereg('[^A-Za-z0-9]', $post_username) How would I exclude the space character from being checked? Thanks, Brandon Quote Link to comment https://forums.phpfreaks.com/topic/126707-solved-how-to-exclude-a-space-character-from-being-checked/ Share on other sites More sharing options...
Brandon Jaeger Posted October 2, 2008 Author Share Posted October 2, 2008 Nevermind. It was too simple. Quote Link to comment https://forums.phpfreaks.com/topic/126707-solved-how-to-exclude-a-space-character-from-being-checked/#findComment-655349 Share on other sites More sharing options...
nrg_alpha Posted October 2, 2008 Share Posted October 2, 2008 May I recommend switching from ereg to preg. As for PHP 6+, ereg support will be dropped. You can learn about preg here. As for your regex, you can use \s, but be mindful that this will encompass many forms of spaces (tabs, return carriages, newlines, etc..). So if you want a space explicitly, you can use \x20 (or a literal blank space). Looking at your ereg, here is what I would have done as the equivalent in preg (with the space issue). $match = array(); preg_match('#[^a-z\d\x20]#i', $post_username, $match); I know you'll have some questions about this.. so I'll explain it (but I recommend you have a look at the link above to really familiarize yourself with PCRE). between the delimiters (# characters), in a character class, match anything that is not a-z or 0-9 (represented as \d - for any digit) or a space. Notice the i after the closing # delimiter? This means case insensitive (so it will in essence match a-z or A-Z). Every match is stored into the array $match. Hope this helps out. Cheers, NRG Quote Link to comment https://forums.phpfreaks.com/topic/126707-solved-how-to-exclude-a-space-character-from-being-checked/#findComment-655613 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.