简体   繁体   中英

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?

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 ie 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'))

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM