Phpurlencode Chinese encoding and urlencode syntax

Source: Internet
Author: User
Strurlencode ($ string): This function is convenient to encode a string. it is used as a convenient method to pass variables to the next page. I wrote this simple function to create a GET query URL () from a number... str urlencode ($ string): This function is convenient to encode a string. it is used as a convenient method to pass variables to the next page.

I wrote this simple function and created a GET query URL () from an array. the code is as follows:

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 . " "; }

If there are $ args array arrays, they will be urlencoded before serialization, the code is as follows:

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

I need a job that does the same full escape function in PHP functions in JavaScript. I spent some time not finding it, but findaly I decided to write my own code, so, to save time, the code is as follows:

Function fullescape ($ in) {$ out = ''; for ($ I = 0; $ I
 
  

I need to encode and decode the URL for UTF8. I have come up with these simple functions. I hope this will be helpful. the code is as follows:

function url_encode($string){     return urlencode(utf8_encode($string)); }function url_decode($string){    return utf8_decode(urldecode($string)); }

Urlencode: a pointer to the Chinese characters in the url of a webpage. The most common method is to enter Chinese characters in search engines such as Baidu and Google, generate a webpage URL that has passed the Encode. urlencode generally has two methods: traditional GB2312-based Encode (Baidu, Yisou, etc ), one is based on UTF-8 Encode (used by Google, Yahoo, etc.). This tool implements two methods of Encode and Decode respectively.

Chinese-> GB2312 Encode-> % D6 % D0 % CE % C4

Chinese-> UTF-8 Encode-> % E4 % B8 % AD % E6 % 96% 87

If you want to use the UTF-8 Encode, there are two methods.

1. Save the file as a UTF-8 file and use urlencode and rawurlencode directly.

II. use the mb_convert_encoding function.

   



Tutorial link:

Reprint at will ~ However, please keep the tutorial address★

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.