자바스크립트 escape unescape php 구현함수
컨텐츠 정보
- 17,612 조회
- 0 추천
- 목록
본문
<?
function toString($text){
return iconv('UTF-16LE', 'UHC', chr(hexdec(substr($text[1], 2, 2))).chr(hexdec(substr($text[1], 0, 2))));
}
function toUnicode($word) {
$word = iconv('UHC', 'UTF-16LE', $word);
return strtoupper(dechex(ord(substr($word,1,1))).dechex(ord(substr($word,0,1))));
}
function unescape($text){
return urldecode(preg_replace_callback('/%u([[:alnum:]]{4})/', 'toString', $text));
}
function escape($str) {
$len = strlen($str);
for($i=0,$s='';$i<$len;$i++) {
$ck = substr($str,$i,1);
$ascii = ord($ck);
if($ascii > 127) $s .= '%u'.toUnicode(substr($str, $i++, 2));
else $s .= (in_array($ascii, array(42, 43, 45, 46, 47, 64, 95))) ? $ck : '%'.strtoupper(dechex($ascii));
}
return $s;
}
//함수 테스트
$string = "%uC990%uAC70%uC6B4%20%uD558%uB8E8%uB418%uC138%uC694...%5E%5E/";
echo unescape($string);
echo "<br /><br />";
echo escape("즐거운 하루되세요...^^/");
?>