首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

把&+数目字的编码与字符串的相互转换

2012-12-21 
把&#+数字的编码与字符串的相互转换function html_decode($str,$encodeUTF-8){preg_match_all(|\&\#\d

把&#+数字的编码与字符串的相互转换

function html_decode($str,$encode="UTF-8"){    preg_match_all("|\&\#\d{0,5}|",$str,$strs);                               for($i=0;$i<count($strs[0]);$i++){       $temp="";       $code=floor(str_replace("&#","",$strs[0][$i]));      if($code>256){          $temp=iconv("UTF-16BE",$encode,chr(floor($code/256)).chr($code%256));      }      elseif($code>128)      {         $temp=iconv("UTF-16BE",$encode,chr(0).chr($code%256));         }      else{          $temp=chr($code);      }     $str=str_replace($strs[0][$i],$temp,$str);   }       return $str;    }

?

?

function html_encode($str,$encode='UTF-8'){    $str=iconv($encode,'UTF-16BE',$str);    for ($i=0;$i<strlen($str);$i++,$i++){               $code=ord($str{$i})*256+ord($str{$i+1});       echo "code:".$code."<br>";        if ($code<128) {        $output.=chr($code);       }else if ($code!=65279) {        $output.='&#'.$code.';';       }    }    return $output;}
?

?

热点排行