str urlencode($string)
此功能是方便的編碼字串時要在URL的查詢的一部分用來作為一種方便的方法傳遞變數到下一頁。
我寫了這個簡單的函數,建立一個GET查詢的網址()從一個數組:
*/
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.111cn.net/中文.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
?>