PHP array_key_exists和UTF 8 [英] PHP array_key_exists and UTF 8

查看:144
本文介绍了PHP array_key_exists和UTF 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,其中编码是UTF-8,用于多语言目的。
这样我的应用程序中的所有内容都是UTF-8。



尽管我对array_key_exists函数有问题。


  1. 它以对数据库的查询开始

SELECT name,from TABLE


  1. 然后我填写一个哈希图,结果如下:

$ 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.

  1. It starts by a query to the database

SELECT name, value from TABLE

  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆