Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mb_strtolower and utf8 strings

Tags:

arrays

php

As you know, we need to use mb_strtolower() instead of strtolower() while we're working with utf-8 data:

$str = 'برنامه'; echo strtolower($str); ---------------------- output: ����� 

It's all gone to undefined chars, now I use mb_strtolower()

$str = 'برنامه'; echo mb_strtolower($str); ---------------------- output: ����� 

still the same results, now:

$str = 'برنامه'; echo mb_strtolower($str,  mb_detect_encoding($str)); ---------------------- output: برنامه 

Now it's fixed, so the way to use mb_strtolower is to also having mb_detect_encoding.

Now my problem is that I want to do the same thing with array_map:

$results_array = array_map('mb_strtolower', $results_array); 

How I'm supposed to use mb_detect_encoding with the above line?

like image 355
behz4d Avatar asked Nov 08 '12 12:11

behz4d


People also ask

What is mb_ strtolower?

Description ¶ mb_strtolower(string $string , ? string $encoding = null ): string. Returns string with all alphabetic characters converted to lowercase.

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.

How do you uppercase in PHP?

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

What is Ucfirst PHP?

The ucfirst() function converts the first character of a string to uppercase. Related functions: lcfirst() - converts the first character of a string to lowercase. ucwords() - converts the first character of each word in a string to uppercase. strtoupper() - converts a string to uppercase.


2 Answers

The solution is to tell mb_strtolower what your string encoding is:

echo mb_strtolower($str, 'UTF-8'); 

If you don't want to supply this parameter every time, set it once for all mb_ functions:

mb_internal_encoding('UTF-8'); 

Then you can call any mb_ function and it will handle your string as UTF-8:

echo mb_strtolower($str); // works without second parameter now 

mb_detect_encoding happens to return 'UTF-8' because it detected it, but it is generally unreliable, since it's conceptually impossible to reliably detect arbitrarily encoded strings. Know what your strings are encoded in and pass this information explicitly.

like image 58
deceze Avatar answered Sep 16 '22 20:09

deceze


Simply put, define your own function which then calls mb_strtolower with mb_detect_encoding.

$results_array = array_map(function($var) {       return mb_strtolower($var, mb_detect_encoding($var)); }, $results_array); 
like image 21
Berry Langerak Avatar answered Sep 19 '22 20:09

Berry Langerak