2

我正在尝试使用带有键的数组作为查找表将字符串转换为数字。

这是数组:

$q2_10_lt = array("Full-time worker" => 1
                , "Part-time worker" => 2
                , "Unemployed, would like to work" => 3
                , "Unable to work (chronically ill/mentally handicapped/physically handicapped)" => 4
                , "Pensioner/retired" => 5
                , "Housewife/husband" => 6
                , "Student at university of college (post-matric)" => 7
                , "High school learner" => 8
                , "Primary school learner" => 9
                , "Child attending pre-school/nursery school/crèche/day-mother" => 10
                , "Child staying at home" => 11
                , "Other" => 12);

有问题的关键是“上学前班/托儿所/托儿所/日间母亲的孩子”。使用以下代码时找不到此键:

$person_tempArr[] = $q2_10_lt[$row["q2_10"]] != null ? $q2_10_lt[$row["q2_10"]] : "12";
$person_tempArr[] = $q2_10_lt[$row["q2_10"]] == null ? $row["q2_10"] : "";

$row["q2_10"]值只是从 MySQL DB 中获取的不同字符串。

我应该从第一行得到数字 10,但我得到的是 12,并且完整的字符串没有改变“孩子就读学前班/托儿所/托儿所/日间母亲”。

这一定与特殊字符è有关,但我一直无法解决。请提供任何帮助。

编辑 1

按照建议进行十六进制转储后,我得到以下结果

从 SQL 数据库:

43 68 69 6c 64 20 61 74 74 65 6e 64 69 6e 67 20 70 72 65 2d 73 63 68 6f 6f 6c 2f 6e 75 72 73 65 72 79 20 73 63 68 6f 6f 6c 2f 63 72 e8 63 68 65 2f 64 61 79 2d 6d 6f 74 68 65 72

从 php 中的字符串:

43 68 69 6c 64 20 61 74 74 65 6e 64 69 6e 67 20 70 72 65 2d 73 63 68 6f 6f 6c 2f 6e 75 72 73 65 72 79 20 73 63 68 6f 6f 6c 2f 63 72 c3 a8 63 68 65 2f 64 61 79 2d 6d 6f 74 68 65 72

The difference was "E8" vs "C3A8" or "è" from the DB vs "è" from the php string.

So how can I go about ensuring the php string remains "è"?

4

2 回答 2

3

The string you get from your database layer is not the same string you use as the key. To fix it, use the same string each time.

Same means here, that the string is the same byte-by-byte. Do a hexdump of the string you get from the database.

Then enter the binary string as key (or at least for the special letter). This will make your code more robust because it will work regardless in which encoding you're saving the PHP file.

Edit: As you compare against the database, the key needs to co-related to the binary sequence in the database:

        , "Child attending pre-school/nursery school/cr\xE8che/day-mother" => 10
                                                       ^^^^

Use the hexadecimal notation to express the bytes you are not able to "type" with your UTF-8 encoded PHP file. The database uses some ISO-8859-1 or similar encoding.

As you can see it is just \x and then the hexcode E8. It works in double-quoted strings in PHP.

于 2012-10-24T13:59:49.277 回答
0

I ended up changing my table column encoding to utf8_general_ci and table collation to utf8_general_ci. I also added the following lines of code to my php file:

ini_set('default_charset', 'utf-8');

//set encodig to utf-8
mysql_query("SET NAMES 'utf8'"); 
mysql_query("SET CHARACTER SET 'utf8'");

It is working now, but I could be doing something not recommended?

于 2012-10-25T07:16:23.390 回答