Search code examples
phpstringuppercasetouppermb-convert-encoding

How can I use php function mb_convert_case and convert only certain words to upper?


I want to pass this input

$string = "AL & JRL buSineSS CENTRE is the best";

Expected Result:

AL & JRL Business Centre Is The Best

I have tried the code below but it converts everything.

mb_convert_case($string, MB_CASE_TITLE, "UTF-8");

Solution

  • So I take it you just want potential acronyms to be ignored, correct? Well, there are a few thoughts. First, you could make a script that ignores anything with 3 or less letters. That's not a great solution, in my opinion. What about "it", "the", etc.? The second is using a dictionary of known words to run ucwords() on. Yuck - that'd be incredibly taxing for such a seemingly simple task!

    I'd recommend simply ignoring anything that is all-caps. This way, no matter what the acronym is (or the length), it'll ignore it. Something like this may suffice:

    $phrase = "Hello this is a TeSt pHrAse, to be tested ASAP. Thanks.";
    
    $chunks = explode(" ", $phrase);
    
    $result = "";
    
    foreach($chunks as $chunk){
        if(!ctype_upper($chunk)) {
            $result .= ucwords($chunk) . " ";
        } else {
            $result .= $chunk . " ";
        }
    
    }
    
    $result = rtrim($result);
    

    Result:

    Hello This Is A Test Phrase, To Be Tested ASAP. Thanks.

    This isn't the most elegant solution, this is just something I've kind of thought about since reading your question. However, if you know your acronyms will be capitalized, this will skip them entirely and only title-case your actual words.

    Caveats

    The example provided above will not work with an acronym joined to a word by a dash, underscore, etc. This only works on spacing. You can easily tweak the above to your needs, and make it a little more intelligent. However, I wanted to be very clear that this may not fulfill all needs!

    Also, this example will come up short in your example phrase. Unfortunately, unless you use a dictionary or count string lengths, this is the closest you'll get. This solution is minimal work for a great deal of functionality. Of course, a dictionary with comparisons would work great (either a dictionary of acronyms or words, either way) - but even then it would be very difficult to keep up to date. Names will throw off a dictionary of words safe to change to title-case. Less commonly used acronyms surely won't be in a dictionary of acronyms. There are endless caveats to all solutions unfortunately. Choose what's best for you.

    Hope this helps. If you have any further questions please comment and I'll try the best I can to help.

    Randomness

    One last thing. I used ucwords(). Feel free to use whatever you want. I'm sure you already know the difference, but check this out:

    Best function for Title capitlization?

    Always good to know exactly what tool is best for the job. Again, I'm sure you know your own needs and I'm sure you chose the right tool. Just thought it was an interesting read that could help anyone stumbling upon this.

    Final Thoughts

    You could use a combination of the above examples to custom tailor your own solution. Often it's very satisfactory to combine methods, thus reducing the downsides of each method.

    Hope this helps, best of luck to you!