정보-기술/소프트웨어

javascript escape() 사용시 php에서 복원시킬때 utf8RawUrlDecode()

Tester 2007. 12. 14. 03:13
자바스크립트 escape 함수를 이용해서 특수 문자를 인코딩해서 넘겨 줄때
php에서 처리할때 utf-8 형식의 특수 문자를 원래대로 복원시켜서 보여줄때 필요한 함수
대충 stripslashes() 함수를 이용해서 처리하려했는데..
● <- 이 모양이 %u25cf 로 인코딩 된 걸 원상복귀 시키려다 찾아보니 있길래 사용..

<?php

function utf8RawUrlDecode ($source) {
  
$decodedStr = "";
  
$pos = 0;
  
$len = strlen ($source);
   while (
$pos < $len) {
      
$charAt = substr ($source, $pos, 1);
       if (
$charAt == '%') {
          
$pos++;
          
$charAt = substr ($source, $pos, 1);
           if (
$charAt == 'u') {
              
// we got a unicode character
              
$pos++;
              
$unicodeHexVal = substr ($source, $pos, 4);
              
$unicode = hexdec ($unicodeHexVal);
              
$entity = "&#". $unicode . ';';
              
$decodedStr .= utf8_encode ($entity);
              
$pos += 4;
           }
           else {
              
// we have an escaped ascii character
              
$hexVal = substr ($source, $pos, 2);
              
$decodedStr .= chr (hexdec ($hexVal));
              
$pos += 2;
           }
       } else {
          
$decodedStr .= $charAt;
          
$pos++;
       }
   }
   return
$decodedStr;
}

?>