- 论坛徽章:
- 0
|
- function uni($str) {
- $ret = '';
- for($i=0;$i<mb_strlen($str,'utf-8');$i=$i+1) {
- $ret .= "&#" . uniord(mb_substr($str, $i, 1, 'utf-8')) . ";";
- }
- return $ret;
- }
- echo uni("海海家园");
- function unichr($u) {
- return mb_convert_encoding(pack("N",$u), mb_internal_encoding(), 'UCS-4BE');
- }
- function uniord($u) {
- $c = unpack("N", mb_convert_encoding($u, 'UCS-4BE', 'UTF-8'));
- return $c[1];
- }
复制代码
- <script>
- function unicode(s){
- var len=s.length;
- var rs="";
- alert(len);
- for(var i=0;i<len;i++){
- var k=s.substring(i,i+1);
- rs+="&#"+s.charCodeAt(i)+";";
- }
- return rs;
- }
-
- function runicode(s){
- var k=s.split(";");
- var rs="";
- for(i=0;i<k.length;i++){
- var m=k[i].replace(/&#/,"");
- rs+=String.fromCharCode(m);
- }
- return rs;
- }
- //alert(unicode("我是一个神"));//我是一个神
- //alert(runicode("我是一个神"));
- </script>
复制代码
- <script language="javascript" type="text/javascript">
- var oSource = document.getElementById("source");
- var oShow2 = document.getElementById("show2");
- var oTt = document.getElementById("tt");
-
- function action(pChoice){
- switch(pChoice){
- case "CONVERT_FMT1":
- oShow2.value = ascii(oSource.value);
- break;
- case "CONVERT_FMT2":
- oShow2.value = unicode(oSource.value);
- break;
- case "RECONVERT":
- oShow2.value = reconvert(oSource.value);
- break;
- }
- }
-
- function ascii(str){
- return str.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"\$2;")});
- }
-
- function unicode(str){
- return str.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"\\u$2")});
- }
-
- function reconvert(str){
- str = str.replace(/(\\u)(\w{4})/gi,function($0){
- return (String.fromCharCode(parseInt((escape($0).replace(/(%5Cu)(\w{4})/g,"$2")),16)));
- });
-
- str = str.replace(/()(\w{4});/gi,function($0){
- return String.fromCharCode(parseInt(escape($0).replace(/(%26%23x)(\w{4})(%3B)/g,"$2"),16));
- });
- return str;
- }
- </script>
复制代码
这些怎么样?
参考 http://seaprince.cn/t/182/1/ |
|