doubledee Posted April 2, 2011 Share Posted April 2, 2011 How do I make sure that SPACES are allow in a regular expression like this... if (preg_match('/^[A-Z0-9#&\',.-]{2,30}$/i', $_POST['address1'])){ Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/232483-how-do-you-include-a-space-in-regex/ Share on other sites More sharing options...
SamT_ Posted April 2, 2011 Share Posted April 2, 2011 In your regex, you can use "\s" in your regex as a whitespace character. Don't forget you can also trim() your values prior to going through regex, as that can greatly reduce some of the necessary leading/trailing whitespace logic in your regular expression. Quote Link to comment https://forums.phpfreaks.com/topic/232483-how-do-you-include-a-space-in-regex/#findComment-1195813 Share on other sites More sharing options...
ignace Posted April 2, 2011 Share Posted April 2, 2011 Just add space: if (preg_match('/^[A-Z0-9#&\',. -]{2,30}$/i', $_POST['address1'])){ Or use \s like SamT_ suggested. Quote Link to comment https://forums.phpfreaks.com/topic/232483-how-do-you-include-a-space-in-regex/#findComment-1195818 Share on other sites More sharing options...
doubledee Posted April 2, 2011 Author Share Posted April 2, 2011 In your regex, you can use "\s" in your regex as a whitespace character. Don't forget you can also trim() your values prior to going through regex, as that can greatly reduce some of the necessary leading/trailing whitespace logic in your regular expression. Okay, maybe this sounds obvious, but I meant spaces in between characters (e.g. "H E L L O"). Or is it understood that words can have spaces? I just assumed my regex above would mean everything has to be contiguous with no spaces in between characters?! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/232483-how-do-you-include-a-space-in-regex/#findComment-1195819 Share on other sites More sharing options...
doubledee Posted April 2, 2011 Author Share Posted April 2, 2011 Just add space: if (preg_match('/^[A-Z0-9#&\',. -]{2,30}$/i', $_POST['address1'])){ Or use \s like SamT_ suggested. Is that a SPACE I see between the period and hyphen at the end?? So using \s is just as good? (I guess it depends on your answer to my question above.) Also, if I do use \s, does it matter where it goes? (I found out tonight that if you don't put the hyphen last that things don't work?! (Why is that???) Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/232483-how-do-you-include-a-space-in-regex/#findComment-1195820 Share on other sites More sharing options...
requinix Posted April 2, 2011 Share Posted April 2, 2011 - Yes, it's a space. - \s means whitespace. That includes spaces, tabs, and newline characters (and a few more). - A hyphen in a character set (the [...]) means a range when put between two characters. It's how the A-Z and 0-9 work. If you want a literal hyphen, escape it (with a backslash) or put it at the beginning or end of the set (where it can't possibly mean a range so it loses the special meaning). Allowing spaces will mean that there can be any number of them anywhere in the string. It will allow "HELLO", "H E L L O", and H E L L O If that's not what you want, be very specific about what you want to allow and what you do not want to allow. Quote Link to comment https://forums.phpfreaks.com/topic/232483-how-do-you-include-a-space-in-regex/#findComment-1195827 Share on other sites More sharing options...
doubledee Posted April 2, 2011 Author Share Posted April 2, 2011 - Yes, it's a space. - \s means whitespace. That includes spaces, tabs, and newline characters (and a few more). - A hyphen in a character set (the [...]) means a range when put between two characters. It's how the A-Z and 0-9 work. If you want a literal hyphen, escape it (with a backslash) or put it at the beginning or end of the set (where it can't possibly mean a range so it loses the special meaning). Allowing spaces will mean that there can be any number of them anywhere in the string. It will allow "HELLO", "H E L L O", and H E L L O If that's not what you want, be very specific about what you want to allow and what you do not want to allow. Upon reflection, this is trickier than it looks... I had my First Name regex like this... preg_match('/^[A-Z\'.-]{2,20}$/i', $_POST['firstname'] If I add a SPACE in between Z and \' then my understand is that someone could enter 2-20 SPACES, or H E L L O which is not what anyone wants?! So how do I write a regex that says, "Accept A-Z, apostrophe, period, hyphen and one and only one space between the previous characters"?? That would allow for: Mary Jane J. P. Y Not and eliminate nonsensical entries. Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/232483-how-do-you-include-a-space-in-regex/#findComment-1195942 Share on other sites More sharing options...
SamT_ Posted April 2, 2011 Share Posted April 2, 2011 Give this one a go: "#^[A-Z\'.-]{2,20}[\s]{1}[A-Z\'.-]{2,20}$#i" Here is a test suite: https://gist.github.com/89547f6befbacd783aa8 Quote Link to comment https://forums.phpfreaks.com/topic/232483-how-do-you-include-a-space-in-regex/#findComment-1195953 Share on other sites More sharing options...
sasa Posted April 3, 2011 Share Posted April 3, 2011 <?php $name = trim($_POST['firstname']); //$name = 'h e l l o'; if(preg_match('/^([A-Z\'.-]\s?){2,}$/i', $name) and strlen($name) <= 20) echo 'ok';else echo 'no'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/232483-how-do-you-include-a-space-in-regex/#findComment-1196136 Share on other sites More sharing options...
requinix Posted April 3, 2011 Share Posted April 3, 2011 and one and only one space between the previous characters So any number of spaces in the whole thing but no two consecutive spaces? I'd keep the simpler regex you have now (where you just added the space) and add a second check with strpos. $valid = (preg_match($pattern, $string) && strpos($string, " ") === false); Just because regular expressions are powerful doesn't mean you have to use one to do everything @SamT_: Mmm, Bad Apple... Quote Link to comment https://forums.phpfreaks.com/topic/232483-how-do-you-include-a-space-in-regex/#findComment-1196188 Share on other sites More sharing options...
doubledee Posted April 3, 2011 Author Share Posted April 3, 2011 and one and only one space between the previous characters So any number of spaces in the whole thing but no two consecutive spaces? I'd keep the simpler regex you have now (where you just added the space) and add a second check with strpos. $valid = (preg_match($pattern, $string) && strpos($string, " ") === false); Just because regular expressions are powerful doesn't mean you have to use one to do everything @SamT_: Mmm, Bad Apple... I've been accused of over-analyzing things!! What I was thinking is that my original Regex would allow someone to enter 20 contiguous spaces, right? Or something like "I am a bad per son". Debbie Quote Link to comment https://forums.phpfreaks.com/topic/232483-how-do-you-include-a-space-in-regex/#findComment-1196270 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.