首页 > php函数 > php urlencode中文编码与urlencode语法

php urlencode中文编码与urlencode语法

str urlencode($string)

此功能是方便的编码字符串时要在URL的查询的一部分用来作为一种方便的方法传递变量到下一页。

我写了这个简单的函数,创建一个GET查询的网址()从一个数组:

<?php
function encode_array($args) {
    if (!is_array($args)) return false;
    $c = 0;
    $out = '';
    foreach ($args as $name => $value) {
        if ($c++ != 0) $out.= '&';
        $out.= urlencode("$name") . '=';
        if (is_array($value)) {
            $out.= urlencode(serialize($value));
        } else {
            $out.= urlencode("$value");
        }
    }
    return $out . " ";
}
//如果有在$ args数组数组,它们将被序列化之前进行了urlencoded。
echo encode_array(array(
    'foo' => 'bar'
)); // foo=bar
echo encode_array(array(
    'foo&bar' => 'some=weird/value'
)); // foo%26bar=some%3Dweird%2Fvalue
echo encode_array(array(
    'foo' => 1,
    'bar' => 'two'
)); // foo=1&bar=two
echo encode_array(array(
    'args' => array(
        'key' => 'value'
    )
)); // args=a%3A1%3A%7Bs%3A3%3A%22key%22%3Bs%3A5%3A%22value%22%3B%7D

/*

我需要一个在PHP函数在JavaScript中做完整的逃生功能相同的工作。我花一些时间不找到它。但findaly我决定写我自己的代码。因此,为了节省时间

*/

function fullescape($in) {
    $out = '';
    for ($i = 0; $i < strlen($in); $i++) {
        $hex = dechex(ord($in[$i]));
        if ($hex == '') $out = $out . urlencode($in[$i]);
        else $out = $out . '%' . ((strlen($hex) == 1) ? ('0' . strtoupper($hex)) : (strtoupper($hex)));
    }
    $out = str_replace('+', '%20', $out);
    $out = str_replace('_', '%5F', $out);
    $out = str_replace('.', '%2E', $out);
    $out = str_replace('-', '%2D', $out);
    return $out;
}
//I needed encoding and decoding for UTF8 urls, I came up with these very simple fuctions. Hope this helps教程!我需要为UTF8的编码和解码网址,我想出了这些非常简单fuctions。希望这会有所帮助
function url_encode($string) {
    return urlencode(utf8_encode($string));
}
function url_decode($string) {
    return utf8_decode(urldecode($string));
}


urlencode:是指针对网页url中的中文字符的一种编码转化方式,最常见的就是Baidu、Google等搜索引擎教程中输入中文查询时候,生成经过 Encode过的网页URL。urlencode的方式一般有两种一种是传统的基于GB2312的Encode(Baidu、Yisou等使用),一种是 基于utf-8的Encode(Google,Yahoo等使用)。本工具分别实现两种方式的Encode与Decode。

中文 -> GB2312的Encode -> %D6%D0%CE%C4

中文 -> utf-8的Encode -> %E4%B8%AD%E6%96%87

如果要使用utf-8的Encode,有两种方法:

一、将文件存为utf-8文件,直接使用urlencode、rawurlencode即可。

二、使用mb_convert_encoding函数。

<?php
$url = 'http://www.phprm.com/中文.rar';
echo urlencode(mb_convert_encoding($url, 'utf-8', 'gb2312'))." ";
echo rawurlencode(mb_convert_encoding($url, 'utf-8', 'gb2312'))." ";
//http%3A%2F%2Fwww.111cn.net%2F%E4%B8%AD%E6%96%87.rar
?>


本文地址:http://www.phprm.com/function/php-urlencode.html

转载随意,但请附上文章地址:-)

标签:none

发表留言