UTF-8转unicode PHP版本+Javascript版本

PHP代码
  1. function uni($str) {  
  2.     $ret = ;  
  3.     for($i=0;$i$str,‘utf-8′);$i=$i+1) {  
  4.         $ret .= "&#" . uniord(mb_substr($str$i, 1, ‘utf-8′)) . ";";  
  5.     }  
  6.     return $ret;  
  7. }  
  8. echo uni("海海家园");  
  9.   function unichr($u) {  
  10.     return mb_convert_encoding(pack("N",$u), mb_internal_encoding(), ‘UCS-4BE’);  
  11.   }  
  12.     function uniord($u) {  
  13.     $c = unpack("N", mb_convert_encoding($u‘UCS-4BE’‘UTF-8′));  
  14.     return $c[1];  
  15.   }  

JavaScript 版本

JavaScript代码
  1.   

从朋友那得到的新方法:(2007.8.14)

JavaScript代码
  1. "javascript" type="text/javascript">  
  2. var oSource = document.getElementById("source");  
  3. var oShow2 = document.getElementById("show2");  
  4. var oTt = document.getElementById("tt");  
  5.   
  6. function action(pChoice){  
  7.  switch(pChoice){  
  8.   case "CONVERT_FMT1":  
  9.    oShow2.value = ascii(oSource.value);  
  10.    break;  
  11.   case "CONVERT_FMT2":  
  12.    oShow2.value = unicode(oSource.value);  
  13.    break;  
  14.   case "RECONVERT":  
  15.    oShow2.value = reconvert(oSource.value);  
  16.    break;  
  17.  }  
  18. }  
  19.   
  20. function ascii(str){  
  21.  return str.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"\$2;")});  
  22. }  
  23.   
  24. function unicode(str){  
  25.  return str.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"\\u$2")});  
  26. }  
  27.   
  28. function reconvert(str){   
  29.  str = str.replace(/(\\u)(\w{4})/gi,function($0){  
  30.             return (String.fromCharCode(parseInt((escape($0).replace(/(%5Cu)(\w{4})/g,"$2")),16)));  
  31.             });  
  32.               
  33.  str = str.replace(/()(\w{4});/gi,function($0){  
  34.             return String.fromCharCode(parseInt(escape($0).replace(/(%26%23x)(\w{4})(%3B)/g,"$2"),16));  
  35.             });              
  36.  return str;  
  37. }  
  38.