9

I'm probably missing something really obvious.

While converting a bunch of string before inserting them in a array I noticed some string where different among each other because of first char being uppercase or not. I decided then to use ucfirst to make first character uppercase but it seems it doesn't work properly, I have had a look around on the web trying to figure out why this is happening but I had no luck.

$produtto = 'APPLE';
echo ucfirst($produtto);
//output: APPLE

If I use instead mb_convert_case

$produtto = 'APPLE';
echo mb_convert_case($produtto, MB_CASE_TITLE, "UTF-8");
//Output: Apple
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Fabio
  • 23,183
  • 12
  • 55
  • 64

4 Answers4

24

ucfirst() only looks at the first character so you should convert to lowercase first.

Use this:

$produtto = 'APPLE';
echo ucfirst(strtolower($produtto));
//output: Apple
MisterBla
  • 2,355
  • 1
  • 18
  • 29
1

In the first case I assume you would first need to turn them lowercase with strtolower, and then use ucfirst on the string.

silkfire
  • 24,585
  • 15
  • 82
  • 105
1

http://php.net/manual/en/function.mb-convert-case.php

MB_CASE_TITLE is not the same as ucfirst(). ucfirst is only interested in the first character. MB_CASE_TITLE is about the whole string and making it an initial capital string.

Richard A Quadling
  • 3,769
  • 30
  • 40
0

read the manual! APPLE = uppercase.. so ucfirst does nothing.

www.php.net/ucfirst

$foo = 'hello world!';
$foo = ucfirst($foo);             // Hello world!

$bar = 'HELLO WORLD!';
$bar = ucfirst($bar);             // HELLO WORLD!
$bar = ucfirst(strtolower($bar)); // Hello world!
Garytje
  • 874
  • 5
  • 11