Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php mb_strtolower giving invalid character

Tags:

php

unicode

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
like image 479
Jithin Avatar asked Jun 04 '26 04:06

Jithin


1 Answers

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.

like image 137
Pekka Avatar answered Jun 05 '26 17:06

Pekka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!