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
4

4 回答 4

24

ucfirst()只看第一个字符,所以你应该先转换为小写。

用这个:

$produtto = 'APPLE';
echo ucfirst(strtolower($produtto));
//output: Apple
于 2013-06-20T13:42:34.100 回答
1

在第一种情况下,我假设您首先需要将它们转换为小写strtolower,然后ucfirst在字符串上使用。

于 2013-06-20T13:42:46.140 回答
0

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

MB_CASE_TITLE 与 ucfirst() 不同。ucfirst 只对第一个字符感兴趣。MB_CASE_TITLE 是关于整个字符串并使其成为初始大写字符串。

于 2014-12-12T10:01:24.963 回答
0

阅读手册!APPLE = 大写.. 所以 ucfirst 什么都不做。

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!
于 2013-06-20T13:42:13.150 回答