Maison> interface Web> js tutoriel> le corps du texte

javascript字符串如何进行编码转换

青灯夜游
Libérer: 2023-01-05 16:09:44
original
9353 Les gens l'ont consulté

javascript字符串进行编码转换的方法:1、使用escape()函数,对字符串进行编码以便它们能在所有计算机上可读,语法“escape(字符串)”;2、使用unescape()函数,语法“unescape(字符串)”。

javascript字符串如何进行编码转换

本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。

1、escape 方法

对 String 对象编码以便它们能在所有计算机上可读,

escape(charString)
Copier après la connexion

必选项 charstring 参数是要编码的任意 String 对象或文字。

说明 :

escape 方法返回一个包含了 charstring 内容的字符串值( Unicode 格式)。所有空格、标点、重音符号以 及 其他非 ASCII字符都用 %xx 编码代替,其中 xx 等于表示该字符的十六进制数。例如,空格返回的是"%20 " 。 字符值大于 255 的以 %uxxxx 格式存储。

注意 :escape 方法不能够用来对统一资源标示码 (URI) 进行编码。对其编码应使用encodeURI和encodeURIComponent 方法。

2、unescape 方法

解码用 escape 方法进行了编码的 String 对象。

unescape(charstring)
Copier après la connexion

必选项 charstring 参数是要解码的 String 对象。

说明 :

unescape 方法返回一个包含 charstring 内容的字符串值。所有以 %xx 十六进制形式编码的字符都用 ASCII 字符集中等价的字符代替。 以 %uxxxx 格式(Unicode 字符)编码的字符用十六进制编码 xxxx 的 Unicode 字符代替.

注意 : unescape 方法不能用于解码统一资源标识码 (URI)。解该码可使用 decodeURI 和decodeURIComponent 方法

AJAX使用GET请求时传递中文字符串时也必须把中文字符串编码成unicode,一般会用到JS的自带函数escape().不过找到了更好的函数来确决中文字符转换成unicode编码的函数如下

function uniencode(text) { text = escape(text.toString()).replace(/+/g, "%2B"); var matches = text.match(/(%([0-9A-F]{2}))/gi); if (matches) { for (var matchid = 0; matchid < matches.length; matchid++) { var code = matches[matchid].substring(1,3); if (parseInt(code, 16) >= 128) { text = text.replace(matches[matchid], '%u00' + code); } } } text = text.replace('%25', '%u0025'); return text; }
Copier après la connexion

当然服务器端要对编码过的字符串进行第二次转码.把字符串转换成UTF-8编码.

function convert_int_to_utf8($intval) { $intval = intval($intval); switch ($intval) { // 1 byte, 7 bits case 0: return chr(0); case ($intval & 0x7F): return chr($intval); // 2 bytes, 11 bits case ($intval & 0x7FF): return chr(0xC0 | (($intval >> 6) & 0x1F)) . chr(0x80 | ($intval & 0x3F)); // 3 bytes, 16 bits case ($intval & 0xFFFF): return chr(0xE0 | (($intval >> 12) & 0x0F)) . chr(0x80 | (($intval >> 6) & 0x3F)) . chr (0x80 | ($intval & 0x3F)); // 4 bytes, 21 bits case ($intval & 0x1FFFFF): return chr(0xF0 | ($intval >> 18)) . chr(0x80 | (($intval >> 12) & 0x3F)) . chr(0x80 | (($intval >> 6) & 0x3F)) . chr(0x80 | ($intval & 0x3F)); } }
Copier après la connexion

【推荐学习:javascript高级教程

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!