drgm51600 2015-05-23 13:41
浏览 48

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

I am trying to pass special language characters like ąėšų from a JavaScript string into a URL and I would like to retrieve the string using the PHP GET method.

However I end up having different special language characters, somehow ė ends up being Ä—.

I have tried using encodeURIComponent() to encode the string in javascript and then using PHP's rawurldecode() function to decode it, but nothing changes.

Has anyone had this problem before?

  • 写回答

1条回答 默认 最新

  • doupuchen6378 2015-05-23 13:58
    关注

    On the PHP docs page for rawurldecode(), check the note left by Javier. He mentions that some characters he encountered were not decoded properly. Possibly, you are experiencing a similar issue.

    Edit: here is the code from the link just in case:

    <?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:
    // áñ
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘