JeremyCanada26 Posted May 2, 2011 Share Posted May 2, 2011 I'm trying to regex match a search phrase that can be any letter or number and be either one word or more than one word, also between 2 and 100 in length. function checkSearchPhrase() { if(isset($_POST['sp'])) { //matches any single word or more if(preg_match("/^[A-Za-z0-9]{1,100}$/", $_POST['sp']) == 1) { return trim($_POST['sp']); } else { return false; } } else { return false; } } Quote Link to comment https://forums.phpfreaks.com/topic/235387-how-do-i-match-the-spacebar-in-regex/ Share on other sites More sharing options...
Pikachu2000 Posted May 2, 2011 Share Posted May 2, 2011 You can add a space to the pattern with \s, if I'm not mistaken. preg_match("/^[A-Za-z0-9\s]{1,100}$/", $_POST['sp']) Quote Link to comment https://forums.phpfreaks.com/topic/235387-how-do-i-match-the-spacebar-in-regex/#findComment-1209691 Share on other sites More sharing options...
salathe Posted May 3, 2011 Share Posted May 3, 2011 You can match a space character by typing a space character (unless the PCRE_EXTENDED pattern modifier is used), or by using a backslash escape sequence to represent that character by its ASCII value. These are basic regexes that will match a space: / / and /\040/ (octal ASCII value, also /\40/ provided there are fewer than 40 previous capturing subpatterns) and /\x20/ (hexadecimal ASCII value). In contrast, the \s will match more than just the space character; it matches Horizontal Tab, Line Feed, Form Feed, Carriage Return and Space. Quote Link to comment https://forums.phpfreaks.com/topic/235387-how-do-i-match-the-spacebar-in-regex/#findComment-1209794 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.