JS中encodeURIComponent函數(shù)用php解碼的代碼
Jun 13, 2016 pm 12:02 PM
JS中encodeURIComponent函數(shù)給中文編碼后,如何用php解碼??
前提:編碼前的中文可能是gbk,gb2312,utf-8等。
復(fù)制代碼 代碼如下:
urldecode()
iconv()
在JS中使用了encodeURIComponent對(duì)中文進(jìn)行編碼在PHP中使用iconv('UTF-8','gb2312',$q);就可以得到你需要的字串了,其中g(shù)b2312根據(jù)你實(shí)際應(yīng)用來(lái)定如還不明白為什么看下面的文章
URL編碼轉(zhuǎn)換,escape() encodeURI() encodeURIComponent()
本文介紹對(duì)url編碼的三種函數(shù) escape() ,encodeURI() ,encodeURIComponent()
escape() 方法:
采用ISO Latin字符集對(duì)指定的字符串進(jìn)行編碼。所有的空格符、標(biāo)點(diǎn)符號(hào)、特殊字符以及其他非ASCII字符都將被轉(zhuǎn)化成%xx格式的字符編碼(xx等于該字符在字符集表里面的編碼的16進(jìn)制數(shù)字)。比如,空格符對(duì)應(yīng)的編碼是%20。unescape方法與此相反。不會(huì)被此方法編碼的字符: @ * / +
encodeURI() 方法:
把URI字符串采用UTF-8編碼格式轉(zhuǎn)化成escape格式的字符串。不會(huì)被此方法編碼的字符:! @ # $& * ( ) = : / ; ? + '
encodeURIComponent() 方法:
把URI字符串采用UTF-8編碼格式轉(zhuǎn)化成escape格式的字符串。與encodeURI()相比,這個(gè)方法將對(duì)更多的字符進(jìn)行編碼,比如 / 等字符。所以如果字符串里面包含了URI的幾個(gè)部分的話,不能用這個(gè)方法來(lái)進(jìn)行編碼,否則 / 字符被編碼之后URL將顯示錯(cuò)誤。不會(huì)被此方法編碼的字符:! * ( )
因此,對(duì)于中文字符串來(lái)說(shuō),如果不希望把字符串編碼格式轉(zhuǎn)化成UTF-8格式的(比如原頁(yè)面和目標(biāo)頁(yè)面的charset是一致的時(shí)候),只需要使用escape。如果你的頁(yè)面是GB2312或者其他的編碼,而接受參數(shù)的頁(yè)面是UTF-8編碼的,就要采用encodeURI或者encodeURIComponent。
注意:escape 方法不能用來(lái)對(duì)“統(tǒng)一資源標(biāo)識(shí)符”(uri) 進(jìn)行編碼。對(duì)其編碼應(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)過(guò)這樣編碼的內(nèi)容,到達(dá)后臺(tái)時(shí),其中的字符都是UTF-8編碼格式的;
3、如果你的Servlet/JSP指定輸出使用的Content-Type是UTF-8,恭喜你,你不需要對(duì)這些Parameters進(jìn)行特別的編碼、解碼操作,它一定是正常的;
4、否則你需要對(duì)這些parameter進(jìn)行解、編碼操作,例如你的頁(yè)面是GBK編碼,那么你需要這么寫(xiě):
String sPara = new String(request.getParamter("test").getBytes("iso-8859-1"),"GBK");
采用gb2312編碼的也可以采用GBK編碼;
5、如果你是在eclipse中進(jìn)行工作,請(qǐng)注意你的Servlet和JSP文件的屬性,其中的編碼必須和content-type中設(shè)置的一樣,否則javac會(huì)對(duì)你的文件進(jìn)行錯(cuò)誤的編碼,這時(shí)候的字節(jié)碼文件本身的字符串就是錯(cuò)誤的,輸出也不頂用;
6、最后做一個(gè)總結(jié):
6.1、servlet/JSP文件本身的編碼必須和content-type輸出的一致,否則需要加入額外的編碼、解碼步驟;
6.2、HTTP/POST方式和encodeURI方式傳入的編碼都是UTF-8的;
6.3、普通的window.open(...),也就是HTTP/GET方式,其傳入的編碼是和運(yùn)行script的頁(yè)面編碼一致的;
6.4、后臺(tái)解碼必須通過(guò)iso-8859-1進(jìn)行解碼,再使用你的目標(biāo)編碼進(jìn)行編碼;
6.5、 response.setContentType("text/xml;charset=utf-8");
如有漢字回顯至頁(yè)面,須加charset=utf-8字樣
request.setCharacterEncoding("UTF-8");//頁(yè)面如通過(guò)POST過(guò)來(lái),含有漢字,須加這一句
此語(yǔ)句放在程序最前面;
6.6 請(qǐng)保證項(xiàng)目屬性編碼為"UTF-8",請(qǐng)求頁(yè)面與響應(yīng)頁(yè)面編碼一致(可以是后臺(tái))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

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

TosettherighttimezoneinPHP,usedate_default_timezone_set()functionatthestartofyourscriptwithavalididentifiersuchas'America/New_York'.1.Usedate_default_timezone_set()beforeanydate/timefunctions.2.Alternatively,configurethephp.inifilebysettingdate.timez

TovalidateuserinputinPHP,usebuilt-invalidationfunctionslikefilter_var()andfilter_input(),applyregularexpressionsforcustomformatssuchasusernamesorphonenumbers,checkdatatypesfornumericvalueslikeageorprice,setlengthlimitsandtrimwhitespacetopreventlayout

ThePhpfunctionSerialize () andunserialize () AreusedtoconvertcomplexdaTastructdestoresintostoraSandaBackagain.1.Serialize () c OnvertsdatalikecarraysorobjectsraystringcontainingTypeandstructureinformation.2.unserialize () Reconstruct theoriginalatataprom

You can embed PHP code into HTML files, but make sure that the file has an extension of .php so that the server can parse it correctly. Use standard tags to wrap PHP code, insert dynamic content anywhere in HTML. In addition, you can switch PHP and HTML multiple times in the same file to realize dynamic functions such as conditional rendering. Be sure to pay attention to the server configuration and syntax correctness to avoid problems caused by short labels, quotation mark errors or omitted end labels.

The key to writing clean and easy-to-maintain PHP code lies in clear naming, following standards, reasonable structure, making good use of comments and testability. 1. Use clear variables, functions and class names, such as $userData and calculateTotalPrice(); 2. Follow the PSR-12 standard unified code style; 3. Split the code structure according to responsibilities, and organize it using MVC or Laravel-style catalogs; 4. Avoid noodles-style code and split the logic into small functions with a single responsibility; 5. Add comments at key points and write interface documents to clarify parameters, return values ??and exceptions; 6. Improve testability, adopt dependency injection, reduce global state and static methods. These practices improve code quality, collaboration efficiency and post-maintenance ease.

Yes,youcanrunSQLqueriesusingPHP,andtheprocessinvolveschoosingadatabaseextension,connectingtothedatabase,executingqueriessafely,andclosingconnectionswhendone.Todothis,firstchoosebetweenMySQLiorPDO,withPDObeingmoreflexibleduetosupportingmultipledatabas
