php urlencode中文編碼與urlencode文法

來源:互聯網
上載者:User

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
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.