JS中encodeURIComponent函數(shù)用php解碼的代碼
Jun 13, 2016 pm 12:02 PM
JS中encodeURIComponent函數(shù)給中文編碼后,如何用php解碼??
前提:編碼前的中文可能是gbk,gb2312,utf-8等。
復(fù)制代碼 代碼如下:
urldecode()
iconv()
在JS中使用了encodeURIComponent對中文進行編碼在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字符集對指定的字符串進行編碼。所有的空格符、標點符號、特殊字符以及其他非ASCII字符都將被轉(zhuǎn)化成%xx格式的字符編碼(xx等于該字符在字符集表里面的編碼的16進制數(shù)字)。比如,空格符對應(yīng)的編碼是%20。unescape方法與此相反。不會被此方法編碼的字符: @ * / +
encodeURI() 方法:
把URI字符串采用UTF-8編碼格式轉(zhuǎn)化成escape格式的字符串。不會被此方法編碼的字符:! @ # $& * ( ) = : / ; ? + '
encodeURIComponent() 方法:
把URI字符串采用UTF-8編碼格式轉(zhuǎn)化成escape格式的字符串。與encodeURI()相比,這個方法將對更多的字符進行編碼,比如 / 等字符。所以如果字符串里面包含了URI的幾個部分的話,不能用這個方法來進行編碼,否則 / 字符被編碼之后URL將顯示錯誤。不會被此方法編碼的字符:! * ( )
因此,對于中文字符串來說,如果不希望把字符串編碼格式轉(zhuǎn)化成UTF-8格式的(比如原頁面和目標頁面的charset是一致的時候),只需要使用escape。如果你的頁面是GB2312或者其他的編碼,而接受參數(shù)的頁面是UTF-8編碼的,就要采用encodeURI或者encodeURIComponent。
注意:escape 方法不能用來對“統(tǒng)一資源標識符”(uri) 進行編碼。對其編碼應(yīng)使用 encodeuri 和 encodeuricomponent 方法。另外,encodeURI/encodeURIComponent是在javascript1.5之后引進的,escape則在javascript1.0版本就有。
總結(jié)一下用法:
1、HTTP/POST方式在缺省情況下,采用的是“x-www-form-urlencoded”進行編碼,它和JavaScript里面的encodeURI所起的作用是一樣的;
2、經(jīng)過這樣編碼的內(nèi)容,到達后臺時,其中的字符都是UTF-8編碼格式的;
3、如果你的Servlet/JSP指定輸出使用的Content-Type是UTF-8,恭喜你,你不需要對這些Parameters進行特別的編碼、解碼操作,它一定是正常的;
4、否則你需要對這些parameter進行解、編碼操作,例如你的頁面是GBK編碼,那么你需要這么寫:
String sPara = new String(request.getParamter("test").getBytes("iso-8859-1"),"GBK");
采用gb2312編碼的也可以采用GBK編碼;
5、如果你是在eclipse中進行工作,請注意你的Servlet和JSP文件的屬性,其中的編碼必須和content-type中設(shè)置的一樣,否則javac會對你的文件進行錯誤的編碼,這時候的字節(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進行解碼,再使用你的目標編碼進行編碼;
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è)置一致

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

The key steps to install PHP on Windows include: 1. Download the appropriate PHP version and decompress it. It is recommended to use ThreadSafe version with Apache or NonThreadSafe version with Nginx; 2. Configure the php.ini file and rename php.ini-development or php.ini-production to php.ini; 3. Add the PHP path to the system environment variable Path for command line use; 4. Test whether PHP is installed successfully, execute php-v through the command line and run the built-in server to test the parsing capabilities; 5. If you use Apache, you need to configure P in httpd.conf

The basic syntax of PHP includes four key points: 1. The PHP tag must be ended, and the use of complete tags is recommended; 2. Echo and print are commonly used for output content, among which echo supports multiple parameters and is more efficient; 3. The annotation methods include //, # and //, to improve code readability; 4. Each statement must end with a semicolon, and spaces and line breaks do not affect execution but affect readability. Mastering these basic rules can help write clear and stable PHP code.

The key to writing Python's ifelse statements is to understand the logical structure and details. 1. The infrastructure is to execute a piece of code if conditions are established, otherwise the else part is executed, else is optional; 2. Multi-condition judgment is implemented with elif, and it is executed sequentially and stopped once it is met; 3. Nested if is used for further subdivision judgment, it is recommended not to exceed two layers; 4. A ternary expression can be used to replace simple ifelse in a simple scenario. Only by paying attention to indentation, conditional order and logical integrity can we write clear and stable judgment codes.

The steps to install PHP8 on Ubuntu are: 1. Update the software package list; 2. Install PHP8 and basic components; 3. Check the version to confirm that the installation is successful; 4. Install additional modules as needed. Windows users can download and decompress the ZIP package, then modify the configuration file, enable extensions, and add the path to environment variables. macOS users recommend using Homebrew to install, and perform steps such as adding tap, installing PHP8, setting the default version and verifying the version. Although the installation methods are different under different systems, the process is clear, so you can choose the right method according to the purpose.

PHPisaserver-sidescriptinglanguageusedforwebdevelopment,especiallyfordynamicwebsitesandCMSplatformslikeWordPress.Itrunsontheserver,processesdata,interactswithdatabases,andsendsHTMLtobrowsers.Commonusesincludeuserauthentication,e-commerceplatforms,for

How to start writing your first PHP script? First, set up the local development environment, install XAMPP/MAMP/LAMP, and use a text editor to understand the server's running principle. Secondly, create a file called hello.php, enter the basic code and run the test. Third, learn to use PHP and HTML to achieve dynamic content output. Finally, pay attention to common errors such as missing semicolons, citation issues, and file extension errors, and enable error reports for debugging.

TohandlefileoperationsinPHP,useappropriatefunctionsandmodes.1.Toreadafile,usefile_get_contents()forsmallfilesorfgets()inaloopforline-by-lineprocessing.2.Towritetoafile,usefile_put_contents()forsimplewritesorappendingwiththeFILE_APPENDflag,orfwrite()w
