3

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
Community
  • 1
  • 1
Antonio Max
  • 8,627
  • 6
  • 43
  • 42
  • 1
    Well.. how should the script know, that `Jhon` is supposed to start with an uppercase, but `programmer` not? – Peon Dec 16 '13 at 07:27
  • 2
    To capitalize first letter of first word in a sentence you can follow this thread - http://stackoverflow.com/questions/5383471/php-capitalize-first-letter-of-first-word-in-a-sentence – Konsole Dec 16 '13 at 07:30

3 Answers3

2

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?

GordonM
  • 31,179
  • 15
  • 87
  • 129
  • Exactly my thoughts, but it's 2013 and google translator does a very nice job, plus, with voice recognition softwares like SIRI, etc, I thought that * maybe * kids these days have some sort of library, a google API that can do this. Maybe one day someone's gonna answer this question with that. – Antonio Max Dec 16 '13 at 14:54
  • Google has far more resources to throw at problems than you do. :) – GordonM Dec 16 '13 at 15:07
2

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.

Community
  • 1
  • 1
Jim Yarbro
  • 2,063
  • 15
  • 21
  • Google may have something like this in a near future, because they already can fix grammar, have regional logic, and they also have the translator & voice API, so the system would know that Apple is a fruit name but also a corp, so it can analyse context, like they already do in Gmail messages. Also, so far, your answer on detecting app caps, may be the solution for now, as I can warn the user about it. Thank you. – Antonio Max Dec 16 '13 at 15:03
1

I would strtolower the string, then split (explode) it on periods then trim every sentence and ucfirst and at last join (implode) with spaces.

xpy
  • 5,481
  • 3
  • 29
  • 48