Home > Backend Development > PHP Tutorial > PHP Chinese interception string function (very useful)

PHP Chinese interception string function (very useful)

WBOY
Release: 2016-07-25 08:55:42
Original
906 people have browsed it
  1. /**

  2. * Chinese string interception function
  3. * by bbs.it-home.org
  4. */
  5. function cut_str($string,$sublen,$filter=true,$start=0,$code='UTF-8'){
  6. if($filter) $string=Html2Text($string);
  7. if($code=='UTF-8'){
  8. $pa="/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
  9. preg_match_all($pa,$string,$t_string);
  10. if(count($t_string[0])-$start>$sublen) return join('',array_slice($t_string[0],$start,$sublen))."...";
  11. return join('',array_slice($t_string[0],$start,$sublen));
  12. }else{
  13. $start=$start*2;
  14. $sublen=$sublen*2;
  15. $strlen=strlen($string);
  16. $tmpstr='';
  17. for($i=0;$i<$strlen;$i++){
  18. if($i>=$start&&$i<($start+$sublen)){
  19. if(ord(substr($string,$i,1))>129){
  20. $tmpstr.=substr($string,$i,2);
  21. }else{
  22. $tmpstr.=substr($string,$i,1);
  23. }
  24. }
  25. if(ord(substr($string,$i,1))>129) $i++;
  26. }
  27. if(strlen($tmpstr)<$strlen ) $tmpstr.="...";
  28. return $tmpstr;
  29. }
  30. }

  31. //html转换函数

  32. function Html2Text($str){
  33. $str = preg_replace("/||/isU","",$str);
  34. $alltext = "";
  35. $start = 1;
  36. for($i=0;$i if($start==0 && $str[$i]==">"){
  37. $start = 1;
  38. }else if($start==1){
  39. if($str[$i]=="<"){
  40. $start = 0;
  41. $alltext .= " ";
  42. }else if(ord($str[$i])>31){
  43. $alltext .= $str[$i];
  44. }
  45. }
  46. }
  47. $alltext = str_replace(" "," ",$alltext);
  48. $alltext = preg_replace("/&([^;&]*)(;|&)/","",$alltext);
  49. $alltext = preg_replace("/[ ]+/s"," ",$alltext);
  50. return $alltext;
  51. }

复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template