读书人

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

发布时间: 2012-12-21 12:03:50 作者: rapoo

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

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;}
?

?

读书人网 >编程

热点推荐