Ucfirst not working properly?

I will probably miss something really obvious.

When converting a bunch of strings before inserting them into an array, I noticed some string where they are different from each other due to the fact that the first char was uppercase or not. Then I decided to use ucfirst to make the first character in uppercase, but it seems like it is not working properly. I looked on the Internet, trying to understand why this is happening, but I was not lucky.

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

If I use mb_convert_case instead

 $produtto = 'APPLE'; echo mb_convert_case($produtto, MB_CASE_TITLE, "UTF-8"); //Output: Apple 
+4
source share
4 answers

ucfirst() looks only at the first character, so you need to convert it to lowercase first.

Use this:

 $produtto = 'APPLE'; echo ucfirst(strtolower($produtto)); //output: Apple 
+12
source

read the instructions! APPLE = uppercase .. therefore 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! 
0
source

In the first case, I assume that you first need to rotate them to lowercase using strtolower , and then use ucfirst in the string.

0
source

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 a whole line and makes it the starting line of capital.

0
source

Source: https://habr.com/ru/post/1487302/


All Articles