doubledee Posted August 21, 2011 Share Posted August 21, 2011 Thanks to the advice of others on PHPFreaks, I have decided that allowing any character for certain fields on my "Add an Article" form. However, I still want to define a length between 2 and 100. How do I re-write my RegEx to accomplish this? Before: // Check HTML Title. if (empty($trimmed['htmlTitle'])){ $errors['htmlTitle'] = 'Please enter an HTML Title.'; }else{ if (preg_match('#^[A-Z \'.-]{2,100}$#i', $trimmed['htmlTitle'])){ $htmlTitle = $trimmed['htmlTitle']; }else{ $errors['htmlTitle'] = 'HTML Title must be 2-100 characters (A-Z \' . -)'; } } Here is my best guess... After: // Check HTML Title. if (empty($trimmed['htmlTitle'])){ $errors['htmlTitle'] = 'Please enter an HTML Title.'; }else{ if (preg_match('#^[.*]{2,100}$#i', $trimmed['htmlTitle'])){ $htmlTitle = $trimmed['htmlTitle']; }else{ $errors['htmlTitle'] = 'HTML Title must be 2-100 characters (A-Z \' . -)'; } } Debbie Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/ Share on other sites More sharing options...
JKG Posted August 21, 2011 Share Posted August 21, 2011 do you have to use RegEx? Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260250 Share on other sites More sharing options...
jcbones Posted August 21, 2011 Share Posted August 21, 2011 How about: if (strlen($trimmed['htmlTitle']) > 2 && strlen($trimmed['htmlTitle']) < 100){ $htmlTitle = $trimmed['htmlTitle']; }else{ $errors['htmlTitle'] = 'HTML Title must be 2-100 characters (A-Z \' . -)'; } strlen() Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260252 Share on other sites More sharing options...
darkfreaks Posted August 21, 2011 Share Posted August 21, 2011 /d{2,100} this will match any character 0-9 that is between 2-100 Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260253 Share on other sites More sharing options...
doubledee Posted August 21, 2011 Author Share Posted August 21, 2011 /d{2,100} this will match any character 0-9 that is between 2-100 Why would I use that? I need to allow any character that can be typed in on an English keyboard. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260257 Share on other sites More sharing options...
doubledee Posted August 21, 2011 Author Share Posted August 21, 2011 do you have to use RegEx? No, but I want to. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260258 Share on other sites More sharing options...
Pikachu2000 Posted August 21, 2011 Share Posted August 21, 2011 Rule #1: If you don't need a pattern, don't use a pattern. Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260260 Share on other sites More sharing options...
doubledee Posted August 21, 2011 Author Share Posted August 21, 2011 Rule #1: If you don't need a pattern, don't use a pattern. But I am looking for the pattern "Any string that is between 2 and 100 characters". (Regular Expressions are so much cleaner than writing all of the code some mentioned earlier?!) Debbie Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260263 Share on other sites More sharing options...
darkfreaks Posted August 21, 2011 Share Posted August 21, 2011 http://stackoverflow.com/questions/1444666/regular-expression-to-match-all-characters-on-a-u-s-keyboard Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260265 Share on other sites More sharing options...
Pikachu2000 Posted August 21, 2011 Share Posted August 21, 2011 That is simply checking a string length, which is what strlen() is for. if( strlen($variable) > 1 && strlen($variable) < 101 ) { Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260266 Share on other sites More sharing options...
doubledee Posted August 21, 2011 Author Share Posted August 21, 2011 That is simply checking a string length, which is what strlen() is for. if( strlen($variable) > 1 && strlen($variable) < 101 ) { I can see how that would work, but let's say I want a more robust solution that says... "Allow an combination of characters (on a U.S.) keyboard including whitespaces, but the string must not start with a whitespace or be entirely whitespaces and it should be between 2 and 100 characters." That would allow someone to enter anything for the field "HTML Description" however it would prevent someone from entering all whitespaces. That is why I usually lean towards Regular Expressions - they are better equipped to handle what I just described. Debbie P.S. I looked at the link to StackOverflow, but it doesn't prevent someone entering in all whitespaces. Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260270 Share on other sites More sharing options...
darkfreaks Posted August 21, 2011 Share Posted August 21, 2011 then add /s matches any and all whitespace characte (return carriages,line break and tab) Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260274 Share on other sites More sharing options...
Pikachu2000 Posted August 21, 2011 Share Posted August 21, 2011 You said any characters, with a length between 2 and 100 characters. You don't need a pattern for that even with the whitespace requirement. trim() will remove leading and trailing whitespace, thus if all whitespace is entered, the string will be empty and fail the strlen() tests. Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260276 Share on other sites More sharing options...
jcbones Posted August 21, 2011 Share Posted August 21, 2011 So use: $variable = trim($variable); $length = strlen($variable); if($length > 1 && $length < 101) { //do something } You are trying to use a function, for no other reason than you WANT to use it. Even though it would be using un-needed overhead. Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260277 Share on other sites More sharing options...
doubledee Posted August 21, 2011 Author Share Posted August 21, 2011 then add /s matches any and all whitespace character You didn't read what I said... "Allow an combination of characters (on a U.S.) keyboard including whitespaces, but the string must not start with a whitespace or be entirely whitespaces and it should be between 2 and 100 characters." Using /s would allow someone to enter 100 spaces - hardly a good "HTML Title"?! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260278 Share on other sites More sharing options...
doubledee Posted August 21, 2011 Author Share Posted August 21, 2011 You said any characters, with a length between 2 and 100 characters. You don't need a pattern for that even with the whitespace requirement. trim() will remove leading and trailing whitespace, thus if all whitespace is entered, the string will be empty and fail the strlen() tests. 100 spaces would be a zero-length string w.r.t. strlen()?? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260279 Share on other sites More sharing options...
darkfreaks Posted August 21, 2011 Share Posted August 21, 2011 ah wrong case /S matches any character that is not a white space Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260280 Share on other sites More sharing options...
doubledee Posted August 21, 2011 Author Share Posted August 21, 2011 So use: $variable = trim($variable); $length = strlen($variable); if($length > 1 && $length < 101) { //do something } You are trying to use a function, for no other reason than you WANT to use it. Even though it would be using un-needed overhead. 1.) Why is using a Regex "un-needed overhead"? 2.) No, I'm trying to learn how to do something multiple ways. 3.) I prefer Regex because I can do in one line what code usually takes several likes and often isn't as thorough. So, now I know how to do what I want using PHP functions and code, but I'd still be curious to see how to do this using Regex, because it seems like a very common need?! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260283 Share on other sites More sharing options...
doubledee Posted August 21, 2011 Author Share Posted August 21, 2011 ah wrong case /S matches any character that is not a white space Okay, but then you'd be forced to have a contiguous string of characters from length 2 to 100. With just \S you would be able to have a sentence, e.g. "Postage Meters can save your business money!!" Debbie Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260284 Share on other sites More sharing options...
darkfreaks Posted August 21, 2011 Share Posted August 21, 2011 ^[ \t]+|[ \t]+$ matches leading and trailing whitespace (TABS or SPACES ) that is if you don't want to strip whitespace and have something like thisisacontinuouslinewithoutspacesortabs then you would have "This is a line without a space before or after" Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260293 Share on other sites More sharing options...
doubledee Posted August 21, 2011 Author Share Posted August 21, 2011 jcbones, How about this... // Check HTML Title. if (empty($trimmed['htmlTitle'])){ $errors['htmlTitle'] = 'Please enter an HTML Title.'; }else{ if (strlen($trimmed['htmlTitle']) > 1 && strlen($trimmed['htmlTitle']) < 101){ $htmlTitle = $trimmed['htmlTitle']; }else{ $errors['htmlTitle'] = 'HTML Title must be 2-100 characters'; } } If anyone wants to help me figure out how to do the same thing using a Regular Expression, that would be a bonus. Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260296 Share on other sites More sharing options...
doubledee Posted August 21, 2011 Author Share Posted August 21, 2011 ^[ \t]+|[ \t]+$ matches leading and trailing whitespace (TABS or SPACES ) that is if you don't want to strip whitespace and have something like thisisacontinuouslinewithoutspacesortabs then you would have "This is a line without a space before or after" ^[ \t]+|[ \t]+$ says... one or more spaces or tabs OR one or more spaces or tabs That doesn't make any sense?! I can probably modify what I said earlier to... "Allow any character on a U.S. keyboard and whitespaces, however the string cannot be all whitespaces and must be a length between 2 and 100 characters total." Debbie Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260299 Share on other sites More sharing options...
darkfreaks Posted August 21, 2011 Share Posted August 21, 2011 i don't understand what whitespace you want to disallow or how your whole idea would work and what i said DOES make since it would strip whitespaces and tabs before or after a string. Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260304 Share on other sites More sharing options...
doubledee Posted August 21, 2011 Author Share Posted August 21, 2011 i don't understand what whitespace you want to disallow What you have... ^[ \t]+|[ \t]+$ says... one or more spaces or tabs OR one or more spaces or tabs It doesn't say anything about allowing for Non-Whitespace Characters. or how your whole idea would work and what i said DOES make since it would strip whitespaces and tabs before or after a string. If I have a form field called "Article Title", then while I will allow spaces, I don't want someone to enter 100 spaces as their entire entry. The suggestions above (e.g. "\s" or "\s\S") would do that. Valid Article Titles: "Postage Meters can save your business money!" "Why communicating online is sooo difficult sometimes?!" "Moving towards answers at a snail's pace!!!" Invalid Article Titles: " " " " Debbie Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260306 Share on other sites More sharing options...
jcbones Posted August 21, 2011 Share Posted August 21, 2011 So use: $variable = trim($variable); $length = strlen($variable); if($length > 1 && $length < 101) { //do something } You are trying to use a function, for no other reason than you WANT to use it. Even though it would be using un-needed overhead. 1.) Why is using a Regex "un-needed overhead"? 2.) No, I'm trying to learn how to do something multiple ways. 3.) I prefer Regex because I can do in one line what code usually takes several likes and often isn't as thorough. So, now I know how to do what I want using PHP functions and code, but I'd still be curious to see how to do this using Regex, because it seems like a very common need?! Debbie I ran a regex on a string containing 93 characters, and then ran the same string through strlen AND trim. Here is the results. This is a string that contains more than 2 characters but less than 100 characters in length. 10,000 runs of preg_match takes 0.0234 seconds 10,000 runs of strlen() and trim() takes 0.0096 seconds. Quote Link to comment https://forums.phpfreaks.com/topic/245374-include-any-character-between-2-and-100/#findComment-1260324 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.