在JavaScript和PHP之间传递语言字符

时间:2015-05-23 13:41:15

标签: javascript php html utf-8 character-encoding

我正在尝试将ąėšų等特殊语言字符从JavaScript字符串传递到URL中,我想使用PHP GET方法检索字符串。

但是我最终得到了不同的特殊语言字符,不知何故ė最终成为Ä—

我尝试使用encodeURIComponent()在javascript中对字符串进行编码,然后使用PHP的rawurldecode()函数对其进行解码,但没有任何变化。

以前有没有人遇到这个问题?

1 个答案:

答案 0 :(得分:0)

rawurldecode()的PHP文档页面上,查看note left by Javier。他提到他遇到的某些角色没有被正确解码。可能,您遇到了类似的问题。

编辑:以下是链接中的代码,以防万一:

<?php
function urlRawDecode($raw_url_encoded)
{
    # Hex conversion table
    $hex_table = array(
        0 => 0x00,
        1 => 0x01,
        2 => 0x02,
        3 => 0x03,
        4 => 0x04,
        5 => 0x05,
        6 => 0x06,
        7 => 0x07,
        8 => 0x08,
        9 => 0x09,
        "A"=> 0x0a,
        "B"=> 0x0b,
        "C"=> 0x0c,
        "D"=> 0x0d,
        "E"=> 0x0e,
        "F"=> 0x0f
    );

    # Fixin' latin character problem
        if(preg_match_all("/\%C3\%([A-Z0-9]{2})/i", $raw_url_encoded,$res))
        {
            $res = array_unique($res = $res[1]);
            $arr_unicoded = array();
            foreach($res as $key => $value){
                $arr_unicoded[] = chr(
                        (0xc0 | ($hex_table[substr($value,0,1)]<<4)) 
                       | (0x03 & $hex_table[substr($value,1,1)])
                );
                $res[$key] = "%C3%" . $value;
            }

            $raw_url_encoded = str_replace(
                                    $res,
                                    $arr_unicoded,
                                    $raw_url_encoded
                        );
        }

        # Return decoded  raw url encoded data 
        return rawurldecode($raw_url_encoded);
}

print urlRawDecode("%C3%A1%C3%B1");

// output:
// áñ

?>