PHP源:
//php function
function escape($string) {
$n = $bn = $tn = 0;
$output = '';
$special = "-_.+@/*0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
while($n < strlen($string)) {
$ascii = ord($string[$n]);
if($ascii == 9 || $ascii == 10 || (32 <= $ascii && $ascii <= 126)) {
$tn = 1;$n++;
} elseif(194 <= $ascii && $ascii <= 223) {
$tn = 2;$n += 2;
} elseif(224 <= $ascii && $ascii <= 239) {
$tn = 3;$n += 3;
} elseif(240 <= $ascii && $ascii <= 247) {
$tn = 4;$n += 4;
} elseif(248 <= $ascii && $ascii <= 251) {
$tn = 5;$n += 5;
} elseif($ascii == 252 || $ascii == 253) {
$tn = 6;$n += 6;
} else {
$n++;
}
$singleStr = substr($string,$bn,$tn);
$charVal = bin2hex(iconv('utf-8', 'ucs-2', $singleStr));
if(base_convert($charVal, 16, 10) > 0xff) {
if (!preg_match("/win/i", PHP_OS))
$charVal = substr($charVal, 2, 2).substr($charVal, 0, 2);
$output .= '%u' . $charVal;
}
else {
if(false !== strpos($special, $singleStr))
$output .= $singleStr;
else
$output .="%" . dechex(ord($string[$bn]));
}
$bn = $n;
}
return $output;
}
易语言翻译源码:
.版本 2
.支持库 iconv
.子程序 Escape, 文本型
.参数 string, 文本型
.局部变量 n, 整数型
.局部变量 bn, 整数型
.局部变量 tn, 整数型
.局部变量 ASCII, 整数型
.局部变量 special, 文本型
.局部变量 singleStr, 文本型
.局部变量 charVal, 文本型
.局部变量 output, 文本型
' strlen=取文本长度()
' || 或者
' && 并且
' substr=取文本中间()
' dechex=进制_十到十六()
' ord=取代码()
special = “-_.+@/*0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”
.判断循环首 (n < 取文本长度 (string))
ASCII = 取代码 (string, n)
输出调试文本 (“ascii ” + 到文本 (n) + “ ” + 到文本 (ASCII))
.判断开始 (ASCII = 9 或 ASCII = 10 或 ASCII ≥ 32 且 ASCII ≤ 126)
输出调试文本 (1)
tn = 1
n = n + 1
.判断 (ASCII ≥ 194 且 ASCII ≤ 223)
输出调试文本 (2)
tn = 2
n = n + 2
.判断 (ASCII ≥ 224 且 ASCII ≤ 239)
输出调试文本 (3)
tn = 3
n = n + 3
.判断 (ASCII ≥ 240 且 ASCII ≤ 247)
输出调试文本 (4)
tn = 4
n = n + 4
.判断 (ASCII ≥ 248 且 ASCII ≤ 251)
输出调试文本 (5)
tn = 5
n = n + 5
.判断 (ASCII = 252 或 ASCII = 253)
输出调试文本 (6)
tn = 6
n = n + 6
.默认
n = n + 1
.判断结束
singleStr = 取文本中间 (string, bn, tn)
charVal = Bin2Hex (编码转换 (到字节集 (singleStr), #编码_UTF_8, #编码_UCS_2, ))
.如果 (进制_十六到十 (charVal) > 进制_十六到十 (charVal))
output = output + charVal
.否则
.如果 (寻找文本 (special, singleStr, , 假) ≠ -1)
output = output + singleStr
.否则
output = output + 进制_十到十六 (取代码 (string, bn))
.如果结束
.如果结束
bn = tn
.判断循环尾 ()
返回 (output)
.子程序 Bin2Hex, 文本型
.参数 Bin, 字节集
.局部变量 Length, 整数型
.局部变量 Index, 整数型
.局部变量 Result, 字节集
Length = 取字节集长度 (Bin)
Result = 取空白字节集 (Length × 2)
.变量循环首 (1, Length, 1, Index)
.如果 (Bin [Index] < 160)
Result [Index × 2 - 1] = 位或 (右移 (Bin [Index], 4), 48)
.否则
Result [Index × 2 - 1] = 位或 (右移 (Bin [Index], 4) - 9, 96)
.如果结束
.如果 (位与 (Bin [Index], 15) < 10)
Result [Index × 2] = 位或 (位与 (Bin [Index], 15), 48)
.否则
Result [Index × 2] = 位或 (位与 (Bin [Index], 15) - 9, 96)
.如果结束
.变量循环尾 ()
返回 (到文本 (Result))