PHP array_key_exists和UTF 8 [英] PHP array_key_exists and UTF 8
问题描述
这样我的应用程序中的所有内容都是UTF-8。
尽管我对array_key_exists函数有问题。
- 它以对数据库的查询开始
SELECT name,from TABLE
- 然后我填写一个哈希图,结果如下:
$ hashmap [$ row ['name']] = $ row ['value']
但是当DB中的名称包含é等重音符时,以下内容返回false:
$这 - > DB->选择( '姓名');
$ this-> db-> select('value');
$ this-> db-> from('table');
$ q = $ this-> db-> get();
$ res = $ q-> result_array();
foreach($ res as $ value){
$ hashmap [$ value ['name']] = $ value ['value'];
}
$ key ='nameé'; //这里的ord返回233
array_key_exists($ key,$ hashmap)
我不知道如何进一步,你是否遇到这个问题?
我有一个表现要求。
感谢您的帮助。
数组键以UTF-8编码,如果它确实来自UTF-8字符串数据库。显然,您的源代码文件不是用UTF-8编码的,我猜这是在Latin-1编码的。因此UTF-8字节序列与拉丁字节序列之间的比较不成功。以UTF-8保存源代码文件,并且它应该工作(请咨询您的文本编辑器)。
I have a database where encoding is UTF-8 for multilingual purpose. I this that everything in my app is in UTF-8.
Though I have a problem with the array_key_exists function.
- It starts by a query to the database
SELECT name, value from TABLE
- Then I fill a hash map with the result :
$hashmap[ $row['name'] ] = $row['value']
But when the name in the DB contains accents like 'é', the following returns false :
$this->db->select('name');
$this->db->select('value');
$this->db->from('table');
$q = $this->db->get();
$res = $q->result_array();
foreach ($res as $value) {
$hashmap[$value['name']] = $value['value'];
}
$key = 'name é'; // an ord here returns 233
array_key_exists($key, $hashmap)
I dont know how to go further with that, did you encountered the problem ?
I have a performance requirement.
Thanks for your help.
The array key is encoded in UTF-8 if it indeed comes as UTF-8 string from the database. Apparently your source code file is not encoded in UTF-8, I'd guess it's encoded in Latin-1. A comparison between a UTF-8 byte sequence and a Latin-1 byte sequence is therefore unsuccessful. Save you source code files in UTF-8 and it should work (consult your text editor).
这篇关于PHP array_key_exists和UTF 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!