tags:

views:

22

answers:

1

The following code is creating problem.

var_dump($name);
$name = mb_strtolower($name);
var_dump($name);

Output is

string(32) "brazil and technology, São Paulo"
string(32) "brazil and technology, s�o paulo"

Can someone please explain why I am getting an invalid character for ã? What am I doing wrong here?

mb_detect_encoding($name) says its UTF-8
+3  A: 

mb_strtolower() has a second parameter to specify the encoding. If omitted, it uses mb_internal_encoding()'s return value. Try adding that parameter explicitly. If you're on UTF-8:

 $name = mb_strtolower($name, "UTF-8");

If that doesn't help, make 100% sure the incoming data is UTF-8 in all the steps along the way, and the output is UTF-8 as well. It could well be that you are working with IS-8859-1 data that gets garbled by the strtolower operation.

Pekka
@Pekka. Thanks That was the issue. btw its mb_internal_encoding actually.Can you please tell me what sets mb_internal_encoding value?
Jithin
@Jithin good question! The manual gives no info: http://en.php.net/mb_internal_encoding I assume it *might* always default to ISO-8859-1, but I don't know for sure.
Pekka
It was indeed ISO-8859-1 for me also. Now I am passing UTF-8 as second argument.
Jithin