Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strtoupper PHP function for UTF-8 string

I've been reading user comments for the strtoupper() PHP function and there doesn't seem to a consensus on how to do the conversion for non-Enlgish strings. I mean people offer localized solutions and stuff, but shouldn't there be a uniform way to convert a string to all upper (or all lower) case letters?

So my question is, say, if I have a UTF-8 encoded string (in some unknown locale) how do I convert it to all upper/lower letters in PHP?

like image 582
ahmd0 Avatar asked May 11 '11 19:05

ahmd0


People also ask

How can I uppercase a string in PHP?

The strtoupper() function converts a string to uppercase. Note: This function is binary-safe. Related functions: strtolower() - converts a string to lowercase.

Does PHP support UTF 8?

PHP UTF-8 Encoding – modifications to your code: 0, default_charset value is used as the default. From PHP 5.4. 0, UTF-8 was the default, but prior to PHP 5.4. 0, ISO-8859-1 was used as the default.

Is substring in string PHP?

You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .

How do you lowercase in PHP?

The strtolower() function converts a string to lowercase. Note: This function is binary-safe. Related functions: strtoupper() - converts a string to uppercase.


1 Answers

You want to use mb_strtoupper.

$str = "Τάχιστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός"; $str = mb_strtoupper($str, "UTF-8"); echo $str; // Prints ΤΆΧΙΣΤΗ ΑΛΏΠΗΞ ΒΑΦΉΣ ΨΗΜΈΝΗ ΓΗ, ΔΡΑΣΚΕΛΊΖΕΙ ΥΠΈΡ ΝΩΘΡΟΎ ΚΥΝΌΣ 

PHP.net states:

By contrast to the standard case folding functions such as strtolower() and strtoupper(), case folding is performed on the basis of the Unicode character properties. Thus the behaviour of this function is not affected by locale settings and it can convert any characters that have 'alphabetic' property, such as A-umlaut (Ä).

like image 147
Kelly Avatar answered Oct 02 '22 22:10

Kelly