acctman Posted April 28, 2009 Share Posted April 28, 2009 can someone help me with a code that will echo an error message if the text contains anything other than a-Z characters, remove any spaces in front or in back format all text (see below) and for Format the text casing, First letter cap, all other lowercase, and First letter cap if its after a character+space (i.e. New york character+space New York) ex: "nEw YOrk" -> "New York" (detected that there was more than 1 space, echo error) ex: LAS VEGAS -> "Las Vegas" ex: " DULUTH" -> "Duluth" Quote Link to comment https://forums.phpfreaks.com/topic/155984-solved-formating-text-case/ Share on other sites More sharing options...
premiso Posted April 28, 2009 Share Posted April 28, 2009 <?php $input = array("NeW 1yOrK NotValid", "NewW YoRK Te valid", "valid new yourk", "valid", "not$ valid", " DULUTH"); foreach ($input as $in) { preg_match('~(^[a-z ]*+)~is', $in, $matches); if (!empty($matches[1]) && $matches[1] == $in) { $in = ucwords(strtolower($in)); $in = trim($in); echo $in . " was valid.<br />"; }else { echo "Invalid Characters in {$in}.<br />"; } } ?> I know the regex can be done in a better manner, but my regex is block is on today. Hopefully nrg_alpha comes along and fixes that part Till then, the above should work. Output from the above: Invalid Characters in NeW 1yOrK NotValid. Neww York Te Valid was valid. Valid New Yourk was valid. Valid was valid. Invalid Characters in not$ valid. Duluth was valid. Quote Link to comment https://forums.phpfreaks.com/topic/155984-solved-formating-text-case/#findComment-821168 Share on other sites More sharing options...
nrg_alpha Posted April 28, 2009 Share Posted April 28, 2009 If I understand this correctly.. if the string doesn't contain a-Z (and I think spaces should be included in this, otherwise entries like 'new york' would fail), then it is invalid.. otherwise, clean things up by trimming initial and trailing spaces, and make only the first letter of every word caps... Borrowing from premiso's snippet: $input = array("NeW 1yOrK NotValid", "NewW YoRK Te valid", "valid new yourk", "valid", "not$ valid", " DULUTH", "nEw YorK"); foreach ($input as $in) { if(preg_match('~^[a-z ]*+$~i', $in)){ $in = ucwords(strtolower(trim($in))); echo 'VALID - ' . $in . "<br />\n"; } else { echo 'INVALID - ' . $in . "<br />\n"; } } Output: INVALID - NeW 1yOrK NotValid VALID - Neww York Te Valid VALID - Valid New Yourk VALID - Valid INVALID - not$ valid VALID - Duluth VALID - New York Is this what is wanted? Whether this is a hit or miss, just a few notes to premiso... In your pattern, you used the s modifer.. but since there is no dot_match_all, that modifier is not used / thus not necessary.. Your use of character class was correct.. and character classes by default take newlines into account (unless specified). I omitted the (!empty($matches[1]) && $matches[1] == $in) part, since the entry must contain a-Z and spaces to remotely qualify, empty string would fail this. Other than that, looks good by my understanding. Quote Link to comment https://forums.phpfreaks.com/topic/155984-solved-formating-text-case/#findComment-821184 Share on other sites More sharing options...
nrg_alpha Posted April 28, 2009 Share Posted April 28, 2009 Hold on.. I think there are some problems with my snippet (regarding tabs by example... ) brb EDIT - Ok, the pattern should probably be: ~^[a-z\s]*+$~i I use \s instead of a literal space, just to make sure stuff like tabs are taken into account (just in case). Quote Link to comment https://forums.phpfreaks.com/topic/155984-solved-formating-text-case/#findComment-821189 Share on other sites More sharing options...
acctman Posted April 28, 2009 Author Share Posted April 28, 2009 thanks i'll test it out now in my application Quote Link to comment https://forums.phpfreaks.com/topic/155984-solved-formating-text-case/#findComment-821318 Share on other sites More sharing options...
acctman Posted May 5, 2009 Author Share Posted May 5, 2009 Hold on.. I think there are some problems with my snippet (regarding tabs by example... ) brb EDIT - Ok, the pattern should probably be: ~^[a-z\s]*+$~i I use \s instead of a literal space, just to make sure stuff like tabs are taken into account (just in case). can you tell me if this will work /~^[\w\s]*+{3,25}$~i/ i was told that \w is for all word characters, and as you meantion \s for space then min 3chr 25max Quote Link to comment https://forums.phpfreaks.com/topic/155984-solved-formating-text-case/#findComment-826984 Share on other sites More sharing options...
Ken2k7 Posted May 5, 2009 Share Posted May 5, 2009 \w = [a-zA-Z0-9_] Do you want digits and underscore to be allowed too? Quote Link to comment https://forums.phpfreaks.com/topic/155984-solved-formating-text-case/#findComment-826988 Share on other sites More sharing options...
nrg_alpha Posted May 5, 2009 Share Posted May 5, 2009 Well in PCRE, \w is at the minimum a-zA-Z0-9_ so if you don't want to allow underscores (among other potential characters that might be included within \w depending on the locale, you will probably not want to use that. In my case, \w also includes stuff like exponents and characters that have accents.. something to consider. Don't be afraid to test things out.. Come up with test strings that are meant to throw the kitchen sink if you will towards the preg pattern. If there's issues, you'll be more likely to find them. EDIT - \s is not just for spaces or tabs.. it is all whitespace characters... this can be usually represented as [ \t\f\v\r\n] (even though PCRE doesn't support vertical tabs '\v') Quote Link to comment https://forums.phpfreaks.com/topic/155984-solved-formating-text-case/#findComment-826992 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.