我想將字符串轉換爲數字,使用帶有鍵的數組作爲查找表。PHP:包含特殊字符的數組鍵未找到
這是數組:
$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數據庫所採取的不同的字符串。
我應該從第一行中得到數字10,但是我得到12和完整的字符串不變「兒童就讀幼兒園/託兒所/託兒所/日母親」。
這必須與特殊字符è有關,但我一直無法解決它。請任何幫助。
編輯1
做一個十六進制轉儲的建議後,我得到了以下結果
從SQL DB:
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
區別在於「E8」與「C3A8」或「 è「從數據庫與」è「從PHP字符串。
那麼我怎麼能確保PHP字符串仍然是「è」?
如果您的網站以UTF-8編碼運行,您的數據庫應該支持它。檢查列定義是否採用UTF-8編碼的數據,否則可能會看到一些數據丟失。用於網絡應用程序的UTF-8不是一個不錯的選擇。注意MySQL支持UTF-8的方式不同:http://dev.mysql.com/doc/refman/5.5/en/charset-unicode.html – hakre