Is there a nifty function that you all know which would allow us to detect and prevent submissions
that have too many Upper case letters in them, which of course is bad because it stands for someone
SHOUTING online set aside that it does not look good, while allowing for 1st Characters of each
sentence to be Upper case which is correct English.
While this function Keeps in mind that in certain languages such as German, other Words beside
the 1st Word of a sentence will be Upper case.
I know this is a real tough nut to crack.
Or, if you wish to actually 'detect' the presence of ALL UPPERCASE and reject the submission, a regular expression could do it for you.
As noted it is not that simple.
That is the issue is NOT all upper case, but say 60% upper case, or 50% upper case, or more to exactly
only sentences with 1st letter in upper case are to be allowed.
You know, it'd be easier to let your users write how they want to. Some people write in capitals all the time, because they're possibly used to it from work etc. But that's no reason to correct them. Anyway the logic is fairly straight forward:
- Create a function to calculate whether or not the percentage of upper case characters in a string exceeds a given percentage (so 2 arguments - returns true if the amount of capitals exceeds your threshold. The simplest method that comes to mind is a case-sensitive preg_match of capital letters ~[A-Z]~ and get the number of results found, compare it to the length of the original string).
- If this function returns true, set it all to lower case and then do a search (using preg_replace) to capitalise the first letter after punctuation, newlines and the start of the string.
This topic is now closed. New replies are no longer allowed.