Php urlencode Chinese encoding and urlencode syntax

Source: Internet
Author: User
Tags rar strlen urlencode

Str urlencode ($ string)
This function is a convenient part of URL query when encoding strings. It is used as a convenient way to pass variables to the next page.

I wrote this simple function to create a GET query URL () from an array:
*/

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 are urlencoded before serialization.


Echo encode_array (array ('foo' => 'bar'); // foo = bar
Echo encode_array (array ('foo & bar' => 'some = weird/value'); // foo % 26bar = some % 3 Dweird % 2 Fvalue
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 % 22 value % 22% 3B % 7D

/*
I need a job in which PHP functions share the full escape function in JavaScript. I spent some time not finding it. But findaly I decided to write my own code. Therefore, to save time
*/

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 tutorial! I need to encode and decode the URL for UTF8, and I have come up with these simple fuctions. Hope this will be helpful.

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 can be either of the traditional GB2312-based Encode (used by Baidu or Yisou) or UTF-8-based Encode (used by Google or Yahoo ). This tool implements Encode and Decode in two ways respectively.

Chinese-> GB2312 Encode-> % D6 % D0 % CE % C4
Chinese-> UTF-8 Encode-> % E4 % B8 % AD % E6 % 96% 87

To use the UTF-8 Encode, you can use either of the following methods:

1. Save the file as a UTF-8 file and use urlencode and rawurlencode directly.
2. Use the mb_convert_encoding function.

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

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.