When a user submit a form, let's say a text area, with caps lock on, there are a few options I know we can do to 'fix' the case, some of them are:
- PHP - ucfirst(mb_strtolower($str));
- CSS - text-transform: lowercase;
And some variations, combining the above methods with some others. That's fine, but that won't fix the capitalisation correctly for most cases.
The goal is not to have "ALL CAPS TEXT", nor "This Is A text", or even worst "all lower case. even paragraphs, etc".
Let's say I have this text:
"HELLO, MY NAME IS JHON. I'M A PROGRAMMER. I work at BBC UK"
It's probably not possible to transform this string correctly as it should be (let me know if I'm wrong):
"Hello, my name is Jhon. I'm a programmer. I work at BBC UK"
I'm fine if this ain't possible in any way. But there's a way to just have some logic, despite all lowercase, or camel case, etc?
Something like:
"Hello, my name is jhon. I'm a programmer. I work at bbc uk"
With all lower cases, but paragraphs first letter, and the first letter after dots, would be all right.
Is there some jquery plugin, some php class, whatever, I'm not aware of, that can do this?
I'm sorry if this question is somehow a duplicate of a thousand other related questions, but all I could find was code that just won't format properly the cases and lots of solutions that won't fit all cases, like: PHP remove all caps
<?php
function ucwordsreplace($matches) {
return ucwords(strtolower($matches[0]));
}
$original = "some UPPERCASE words GO HERE";
$fixed = preg_replace_callback('/\b[A-Z]+\b/', "ucwordsreplace", $original);
echo $fixed; // some Uppercase words Go Here
As the old saying goes, you can't make a silk purse out of a pig's ear.
Any script that does that would have to know about sentence boundaries, personal pronouns, proper names, place names and all sorts of other things. Even if you do let proper capitalization of acronyms slide, that's a huge amount of possibilities that have to be covered. For all intents and purposes it would be so impractical as to be basically impossible to do that.
For example, if you encounter the word SMITH, do you render it Smith as in the name, or smith as in the job? Or even SMITH if it happens to be an acronym for something?
I would strtolower
the string, then split (explode
) it on periods then trim
every sentence and ucfirst
and at last join (implode
) with spaces.
Having studied languages, and coding, for many years, I can tell you with some assurity that it's not possible to accurately guess. Consider proper nouns, such as the name of a person or restaurant. How will you be able to tell when a user is writing about their AWESOME VISIT AT THE RED BRICK OVEN RESTAURANT that the word RED is describing the color of the building or is part of the actual name?
Your close-enough solution is similar to what you've already found, but just make sure to do a check on the contents of the post for caps, and if all caps SEEM to be detected, then run your re-casing solution.
Here's a solution which describes detecting all caps.