Kanan Farzali
Kanan Farzali

Reputation: 1053

UTF8 encoding with special foreign characters

PHP piece of code like this:

trim(strtolower(utf8_encode($res->translation))) == trim(strtolower(utf8_encode($ph)))

returns false if, for example:$res->translation = "Bottines À Talons Hauts" and $ph = "bottines à talons hauts". The problem should be in a weird À letter, as other words match perfectly.

It needs to be mentioned that $res->translation comes from MySQL and $ph is a phrase from textarea in HTML page.

Any ideas?

Upvotes: 0

Views: 406

Answers (2)

Romain
Romain

Reputation: 51

http://www.php.net/manual/en/function.strtolower.php

Returns string with all alphabetic characters converted to lowercase.

Note that 'alphabetic' is determined by the current locale. This means that in i.e. the default "C" locale, characters such as umlaut-A (Ä) will not be converted.

This should be solved using mb_strtolower and forcing UTF-8

trim(mb_strtolower(utf8_encode($res->translation),'UTF-8')) == trim(mb_strtolower(utf8_encode($ph),'UTF-8'))

Upvotes: 1

Ghazanfar Mir
Ghazanfar Mir

Reputation: 3543

May be your database connection needs to be established using utf8 encoding.

Try following when establishing your connection:

$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);

Note: Better to use mysqli or PDO, though

Upvotes: 0

Related Questions