LLLLLLL Posted December 23, 2013 Share Posted December 23, 2013 Regex always kills me. I want a string to be only letters, numbers, dash, underscore, or space. if ( !preg_match( $pattern, $str ) ){ $err = 'Only letters, numbers, space, underscore and dash are allowed.'; return $err } In this example, what is $pattern ? My best guess was... $pattern = '/[^a-zA-Z0-9_-\s]+/'; ... but it's not working. Quote Link to comment https://forums.phpfreaks.com/topic/284903-allow-only-letters-numbers-dash-underscore-or-space/ Share on other sites More sharing options...
requinix Posted December 23, 2013 Share Posted December 23, 2013 If you want the entire string then you have to make sure it matches every character from the beginning of the string to the end. That means ^ and $ anchors. Also, - in a character set can mean a range (like "A-Z") so you should either escape it or put it as the very first or very last character (where it couldn't possibly mean a range). Also also, \s is any whitespace so if you don't want tabs and newlines and such then you should put a literal space in there instead. Quote Link to comment https://forums.phpfreaks.com/topic/284903-allow-only-letters-numbers-dash-underscore-or-space/#findComment-1462981 Share on other sites More sharing options...
LLLLLLL Posted December 23, 2013 Author Share Posted December 23, 2013 What's the answer, then? $pattern = '/[^a-zA-Z0-9_ \-$]/'; Still doesn't work... Quote Link to comment https://forums.phpfreaks.com/topic/284903-allow-only-letters-numbers-dash-underscore-or-space/#findComment-1462982 Share on other sites More sharing options...
Irate Posted December 23, 2013 Share Posted December 23, 2013 How about using [^\w $\-] The \w in a synonym for [a-zA-Z0-9_], so this saves you a few valuable characters (if you're writing compressed and/or minified code. Quote Link to comment https://forums.phpfreaks.com/topic/284903-allow-only-letters-numbers-dash-underscore-or-space/#findComment-1462984 Share on other sites More sharing options...
LLLLLLL Posted December 23, 2013 Author Share Posted December 23, 2013 That doesn't work for me either. Everything fails, including "aa" Quote Link to comment https://forums.phpfreaks.com/topic/284903-allow-only-letters-numbers-dash-underscore-or-space/#findComment-1462985 Share on other sites More sharing options...
Solution kicken Posted December 23, 2013 Solution Share Posted December 23, 2013 The anchors go outside of the brackets. Putting them inside makes them additional valid characters rather than anchors. Within the brackets you just add the ranges/characters you want, keeping in mind the comments above. After the brackets you need a repeater in order to allow multiple instances of the characters. + means one-or-more and works well. for example: /^[a-z]+$/ Now just extend that to cover all your characters. Quote Link to comment https://forums.phpfreaks.com/topic/284903-allow-only-letters-numbers-dash-underscore-or-space/#findComment-1462987 Share on other sites More sharing options...
LLLLLLL Posted December 24, 2013 Author Share Posted December 24, 2013 Thanks. Regex isn't something I've had to work with, despite developing for 12 years. The syntax can be confusing; you clarified it. Quote Link to comment https://forums.phpfreaks.com/topic/284903-allow-only-letters-numbers-dash-underscore-or-space/#findComment-1463038 Share on other sites More sharing options...
marcusnunes Posted June 11, 2014 Share Posted June 11, 2014 you can use this pattern: /^([a-z0-9\s\_\-]+)$/ Quote Link to comment https://forums.phpfreaks.com/topic/284903-allow-only-letters-numbers-dash-underscore-or-space/#findComment-1482430 Share on other sites More sharing options...
Jacques1 Posted June 11, 2014 Share Posted June 11, 2014 This thread is from last year and has been solved already. What's the point of digging it out? This isn't really the perfect solution either: You've forgotten the uppercase characters. The underscore doesn't have any meaning, so no need to escape it. A dash at the end of a character class doesn't have any meaning either, so again no need to escape it. If you do use escaping, make it a double backslash, because the regex will be inside a PHP string. What's the point of the parentheses? The \s class contains tabs and newlines as well, which the OP did not want. Why not use the \w class as suggested above? Quote Link to comment https://forums.phpfreaks.com/topic/284903-allow-only-letters-numbers-dash-underscore-or-space/#findComment-1482436 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.