PHP functions corresponding to the encodeuricomponent of JS

Source: Internet
Author: User
Tags urlencode

When using AJAX to interact with the background, we often encode and decode Chinese, and for JS there are two functions: encodeuricomponent for encoding, decodeuricomponent for decoding. For PHP in the background, there are two corresponding encoding and decoding functions: UrlEncode for encoding, UrlDecode for decoding. Now look at the following two pieces of code, first to a PHP code:

$myStr 1 = ' I am Chinese ';
$myStr 2 = UrlEncode ($myStr 1);
echo $myStr 1. ' <br/> ';
Echo $myStr 2. ' <br/> ';
Echo UrlDecode ($myStr 2). ' <br/> ';

The PHP code above will output:

I am a Chinese
%e6%88%91%e6%98%af%e4%b8%ad%e5%9b%bd%e4%ba%ba
I am a Chinese

Then look at the JS code:

var myStr1 = ' I am Chinese ';
var myStr2 = encodeURIComponent (MYSTR1);
document.write (MYSTR1);
document.write (' <br/> ');
document.write (MYSTR2);
document.write (' <br/> ');
document.write (decodeURIComponent (MYSTR2));

The above JS code will output:

I am a Chinese
%e6%88%91%e6%98%af%e4%b8%ad%e5%9b%bd%e4%ba%ba
I am a Chinese

And the output of the PHP code above is the same.

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.