4

Should I be using mb_convert_case with MB_CASE_TITLE or ucwords? Or something else? What will the differences be?

RobertPitt
  • 56,863
  • 21
  • 114
  • 161
Ben G
  • 26,091
  • 34
  • 103
  • 170

2 Answers2

3

It depends.

mb_convert_case() is multibyte safe. ucwords() is not.

mb_convert_case() requires an extension that is not always available. ucwords() is always available.

So if your application will only ever use single-byte encodings then ucwords() gives you better portability.

But if your application might need to process multi-byte encodings then ucwords() will fail you.

1
function uc_words($string){
return mb_convert_case($string, MB_CASE_TITLE, "UTF-8");
}

MB means multi byte, so it can convert non-ASCII characters, ucwords can convert only ASCII.

If you use ucwords on "moj šal", you will get "Moj šal", if you use multi byte convert you will get "Moj Šal"... that's it.

Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66
  • how is that better than using `mb_convert_case()`? –  Dec 24 '10 at 20:30
  • mb_convert_case, it is using it, this is just a function so he can choose upper, lower, or title case conversion. As I stated it works with UTF-8 (multibyte) while ucwords only works with ASCII. – Dejan Marjanović Dec 24 '10 at 20:32
  • babonk can choose upper, lower, or title case conversion perfectly easily using `mb_convert_case()` directly. What additional functionality does your `mb_convert()` function offer? I can't see any. It's just overhead. –  Dec 24 '10 at 20:35
  • Absolutely nothing, I edited my post, he can write a function that will work with multibyte encodings. I always use something like this. – Dejan Marjanović Dec 24 '10 at 20:47
  • It converts AAAA to Aaaa. ucwords() leaves AAAA intact. – digout Apr 15 '20 at 16:48