PHP에서 iconv 사용가능 하여야 합니다.

 

function tostring($text) {
//    return iconv('UTF-16LE', 'UTF-8', chr(hexdec(substr($text[1], 2, 2))).chr(hexdec(substr($text[1], 0, 2))));
    
return iconv('UTF-16LE''UHC'chr(hexdec(substr($text[1], 22))).chr(hexdec(substr($text[1], 02
))));
}
function 
urlutfchr($text
){
    return 
rawurldecode(preg_replace_callback('/%u([[:alnum:]]{4})/''tostring'$text
));
}

 

처리할 문서가 charset euc-kr 일경우 함수 tostring 에서 iconv 2번째인자를 "UHC" 로

 utf8 일경우 "UTF-8"로 지정한다.

 

Ex

$escapeString = "%uC548%uB155%uD558%uC138%uC694";  // 자스에서 "안녕하세요"를 escape 한 문자열

$unEscapeString = urlutfchr($escapeString);

echo $unEscapeString;

 

// 출력결과

// 안녕하세요

Posted by 철냄비짱
,

escape() 함수와 unescape() 함수

 

escape() 함수는 알파벳과 숫자 및 * , @, - , _ , + , . , / 를 제외한 문자를 모두 16진수 문자로 바꾸어 줍니다. 이 함수는 쉼표와 세미콜론 같은 문자가 쿠키문자열과의 충돌을 피하기 위해 사용됩니다.

 

이렇게 16진수 문자열로 변환된 문자열은 unescape() 함수로 다시 되돌려줄 수 있습니다

 

  1. <SCRIPT LANGUAGE="JavaScript">
  2. <!--
  3. document.write("escape('우리나라') ===>" + escape('우리나라') + "<br>");
  4. document.write("escape('korea') ===>" + escape('korea') + "<br>");
  5. document.write("escape('#$%^') ===>" + escape('#$%^') + "<br>");
  6. document.write("escape('1234') ===>" + escape('1234') + "<br>");
  7. document.write("escape('*@-_+./') ===>" + escape('*@-_+./') + "<br><br>");
  8. document.write("unescape('%uC6B0%uB9AC%uB098%uB77C') ===>" + unescape('%uC6B0%uB9AC%uB098%uB77C') + "<br>");
  9. document.write("unescape('%23%24%25%5E') ===>" + unescape('%23%24%25%5E') + "<br>");
  10. //-->
  11. </SCRIPT>

 

예 제에서 보듯이 영어 알파벳과 숫자, 그리고 *@-_+./를 제외한 문자는 모두 escape() 함수를 이용하면 16진수 문자열로 변환될 수 있고, 변환된 문자열을 unescape() 함수를 이용해 다시 원상태로 복구 시킬 수 있습니다

 

자료출처 : http://www.jasko.co.kr/ 


Posted by 철냄비짱
,