国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 JS中encodeURIComponent函數(shù)用php解碼的代碼

JS中encodeURIComponent函數(shù)用php解碼的代碼

Jun 13, 2016 pm 12:02 PM
encodeuricomponent php code of decoding

JS中encodeURIComponent函數(shù)給中文編碼后,如何用php解碼??
前提:編碼前的中文可能是gbk,gb2312,utf-8等。

復(fù)制代碼 代碼如下:


urldecode()
iconv()



在JS中使用了encodeURIComponent對中文進(jìn)行編碼在PHP中使用iconv('UTF-8','gb2312',$q);就可以得到你需要的字串了,其中g(shù)b2312根據(jù)你實際應(yīng)用來定如還不明白為什么看下面的文章

URL編碼轉(zhuǎn)換,escape() encodeURI() encodeURIComponent()

本文介紹對url編碼的三種函數(shù) escape() ,encodeURI() ,encodeURIComponent()

escape() 方法:
采用ISO Latin字符集對指定的字符串進(jìn)行編碼。所有的空格符、標(biāo)點符號、特殊字符以及其他非ASCII字符都將被轉(zhuǎn)化成%xx格式的字符編碼(xx等于該字符在字符集表里面的編碼的16進(jìn)制數(shù)字)。比如,空格符對應(yīng)的編碼是%20。unescape方法與此相反。不會被此方法編碼的字符: @ * / +

encodeURI() 方法:
把URI字符串采用UTF-8編碼格式轉(zhuǎn)化成escape格式的字符串。不會被此方法編碼的字符:! @ # $& * ( ) = : / ; ? + '

encodeURIComponent() 方法:
把URI字符串采用UTF-8編碼格式轉(zhuǎn)化成escape格式的字符串。與encodeURI()相比,這個方法將對更多的字符進(jìn)行編碼,比如 / 等字符。所以如果字符串里面包含了URI的幾個部分的話,不能用這個方法來進(jìn)行編碼,否則 / 字符被編碼之后URL將顯示錯誤。不會被此方法編碼的字符:! * ( )


因此,對于中文字符串來說,如果不希望把字符串編碼格式轉(zhuǎn)化成UTF-8格式的(比如原頁面和目標(biāo)頁面的charset是一致的時候),只需要使用escape。如果你的頁面是GB2312或者其他的編碼,而接受參數(shù)的頁面是UTF-8編碼的,就要采用encodeURI或者encodeURIComponent。

注意:escape 方法不能用來對“統(tǒng)一資源標(biāo)識符”(uri) 進(jìn)行編碼。對其編碼應(yīng)使用 encodeuri 和 encodeuricomponent 方法。另外,encodeURI/encodeURIComponent是在javascript1.5之后引進(jìn)的,escape則在javascript1.0版本就有。

總結(jié)一下用法:

1、HTTP/POST方式在缺省情況下,采用的是“x-www-form-urlencoded”進(jìn)行編碼,它和JavaScript里面的encodeURI所起的作用是一樣的;
2、經(jīng)過這樣編碼的內(nèi)容,到達(dá)后臺時,其中的字符都是UTF-8編碼格式的;
3、如果你的Servlet/JSP指定輸出使用的Content-Type是UTF-8,恭喜你,你不需要對這些Parameters進(jìn)行特別的編碼、解碼操作,它一定是正常的;
4、否則你需要對這些parameter進(jìn)行解、編碼操作,例如你的頁面是GBK編碼,那么你需要這么寫:
String sPara = new String(request.getParamter("test").getBytes("iso-8859-1"),"GBK");
采用gb2312編碼的也可以采用GBK編碼;
5、如果你是在eclipse中進(jìn)行工作,請注意你的Servlet和JSP文件的屬性,其中的編碼必須和content-type中設(shè)置的一樣,否則javac會對你的文件進(jìn)行錯誤的編碼,這時候的字節(jié)碼文件本身的字符串就是錯誤的,輸出也不頂用;
6、最后做一個總結(jié):
6.1、servlet/JSP文件本身的編碼必須和content-type輸出的一致,否則需要加入額外的編碼、解碼步驟;
6.2、HTTP/POST方式和encodeURI方式傳入的編碼都是UTF-8的;
6.3、普通的window.open(...),也就是HTTP/GET方式,其傳入的編碼是和運行script的頁面編碼一致的;
6.4、后臺解碼必須通過iso-8859-1進(jìn)行解碼,再使用你的目標(biāo)編碼進(jìn)行編碼;
6.5、 response.setContentType("text/xml;charset=utf-8");
如有漢字回顯至頁面,須加charset=utf-8字樣
request.setCharacterEncoding("UTF-8");//頁面如通過POST過來,含有漢字,須加這一句

此語句放在程序最前面;

6.6 請保證項目屬性編碼為"UTF-8",請求頁面與響應(yīng)頁面編碼一致(可以是后臺)UTF-8.必須保證文件

屬性和ContentType設(shè)置一致
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get the current session ID in PHP? How to get the current session ID in PHP? Jul 13, 2025 am 03:02 AM

The method to get the current session ID in PHP is to use the session_id() function, but you must call session_start() to successfully obtain it. 1. Call session_start() to start the session; 2. Use session_id() to read the session ID and output a string similar to abc123def456ghi789; 3. If the return is empty, check whether session_start() is missing, whether the user accesses for the first time, or whether the session is destroyed; 4. The session ID can be used for logging, security verification and cross-request communication, but security needs to be paid attention to. Make sure that the session is correctly enabled and the ID can be obtained successfully.

PHP get substring from a string PHP get substring from a string Jul 13, 2025 am 02:59 AM

To extract substrings from PHP strings, you can use the substr() function, which is syntax substr(string$string,int$start,?int$length=null), and if the length is not specified, it will be intercepted to the end; when processing multi-byte characters such as Chinese, you should use the mb_substr() function to avoid garbled code; if you need to intercept the string according to a specific separator, you can use exploit() or combine strpos() and substr() to implement it, such as extracting file name extensions or domain names.

How do you perform unit testing for php code? How do you perform unit testing for php code? Jul 13, 2025 am 02:54 AM

UnittestinginPHPinvolvesverifyingindividualcodeunitslikefunctionsormethodstocatchbugsearlyandensurereliablerefactoring.1)SetupPHPUnitviaComposer,createatestdirectory,andconfigureautoloadandphpunit.xml.2)Writetestcasesfollowingthearrange-act-assertpat

How to split a string into an array in PHP How to split a string into an array in PHP Jul 13, 2025 am 02:59 AM

In PHP, the most common method is to split the string into an array using the exploit() function. This function divides the string into multiple parts through the specified delimiter and returns an array. The syntax is exploit(separator, string, limit), where separator is the separator, string is the original string, and limit is an optional parameter to control the maximum number of segments. For example $str="apple,banana,orange";$arr=explode(",",$str); The result is ["apple","bana

JavaScript Data Types: Primitive vs Reference JavaScript Data Types: Primitive vs Reference Jul 13, 2025 am 02:43 AM

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

Using std::chrono in C Using std::chrono in C Jul 15, 2025 am 01:30 AM

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

How to pass a session variable to another page in PHP? How to pass a session variable to another page in PHP? Jul 13, 2025 am 02:39 AM

In PHP, to pass a session variable to another page, the key is to start the session correctly and use the same $_SESSION key name. 1. Before using session variables for each page, it must be called session_start() and placed in the front of the script; 2. Set session variables such as $_SESSION['username']='JohnDoe' on the first page; 3. After calling session_start() on another page, access the variables through the same key name; 4. Make sure that session_start() is called on each page, avoid outputting content in advance, and check that the session storage path on the server is writable; 5. Use ses

How does PHP handle Environment Variables? How does PHP handle Environment Variables? Jul 14, 2025 am 03:01 AM

ToaccessenvironmentvariablesinPHP,usegetenv()orthe$_ENVsuperglobal.1.getenv('VAR_NAME')retrievesaspecificvariable.2.$_ENV['VAR_NAME']accessesvariablesifvariables_orderinphp.iniincludes"E".SetvariablesviaCLIwithVAR=valuephpscript.php,inApach

See all articles