当前位置:首页 > PHP教程 > php应用 > 列表

PHP切割汉字的常用方法实例总结

发布:smiling 来源: PHP粉丝网  添加日期:2021-11-20 11:47:59 浏览: 评论:0 

这篇文章主要介绍了PHP切割汉字的常用方法,结合实例形式总结分析了php针对汉字的编码转换、遍历、截取等相关操作技巧,需要的朋友可以参考下。

本文实例讲述了PHP切割汉字的常用方法,分享给大家供大家参考,具体如下:

  1. <?php 
  2. /* 
  3. @UTF-8编码的字符可能由1~3个字节组成。 
  4. */ 
  5. /*--------------------------方法一截取中文字符串方法------------------------------*/ 
  6. function msubstr($str, $start, $len) 
  7. { 
  8.   $tmpstr = ""; 
  9.   $strlen = $start + $len; 
  10.   for ($i = 0; $i < $strlen; $i++) { 
  11.     if (ord(substr($str, $i, 1)) > 0xa0)  //ord()函数返回字符串的第一个字符的ASCII值 
  12.     { 
  13.       $tmpstr .= substr($str, $i, 2); 
  14.       $i++; 
  15.     } else { 
  16.       $tmpstr .= substr($str, $i, 1); 
  17.     } 
  18.   } 
  19.   return $tmpstr; 
  20. } 
  21. /*----------------------------第二种方法-----------------------------------*/ 
  22. //截取的是UTF-8字符串 
  23. function utf_substr($str, $len) 
  24. { 
  25.   $new_str = []; 
  26.   for ($i = 0; $i < $len; $i++) { 
  27.     $tem_str = substr($str, 0, 1); 
  28.     if (ord($tem_str > 127)) { 
  29.       $i++; 
  30.       if ($i < $len) { 
  31.         $new_str[] = substr($str, 0, 3); 
  32.         $str = substr($str, 3); 
  33.       } 
  34.     } else { 
  35.       $new_str[] = substr($str, 0, 1); 
  36.       $str = substr($str, 1); 
  37.     } 
  38.   } 
  39.   return join($new_str);//join()函数把数组元素组合为一个字符串 
  40. } 
  41. /*-------------------------------------第三种方法(UTF-8)--------------------------------*/ 
  42. function cutstr($string, $length) 
  43. { 
  44.   preg_match_all("/[\x01-\x7f]|[\xc2-\xdf]|[\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $info); 
  45.   $wordscut = ""; 
  46.   $j = 0; 
  47.   for ($i = 0; $i < count($info[0]); $i++) { 
  48.     $wordscut .= $info[0][$i]; 
  49.     $j = ord($info[0][$i]) > 127 ? $j + 2 : $j + 1; 
  50.     if ($j > $length - 3) { 
  51.       return $wordscut . "..."; 
  52.     } 
  53.   } 
  54.   return join('', $info[0]); 
  55. } 
  56. $string = "312哈哈,这个组合很难切割哦"; 
  57. echo cutstr($string, 10); 
  58. /*---------------------------------下面是曾经用过的截取第三个的字符串的------------------------------*/ 
  59. // $name1 = mysql_result($my_rst,0,"name"); 
  60. // $name = preg_match("/([1-9][0-9]+)/",$name1,$r); 
  61. // $name = $r[0]; 
  62. // if($name == ""){ 
  63. // $name=preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,2}'. 
  64. // '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,1}).*#s', 
  65. // '$1',$name1); 
  66. // } 
  67. /*--------------------------------------------第四种方法(UTF-8)---------------------------------------------*/ 
  68. function cut_str($sourcestr, $cutlength) 
  69. { 
  70.   $returnstr = ''; 
  71.   $i = 0; 
  72.   $n = 0; 
  73.   $str_length = strlen($sourcestr);//字符串的字节数 
  74.   while ($n < $cutlength && $i <= $str_length) { 
  75.     $temp_str = substr($sourcestr, $i, 1); 
  76.     $ascnum = ord($temp_str);//得到字符串中第$i位字符的ASCII码 
  77.     if ($ascnum >= 224) { 
  78.       $returnstr = $returnstr . substr($sourcestr, $i, 3);//根据UTF-8编码规范,将3个连续的字符计为单个字符 
  79.       $i = $i + 3;//实际Byte记为3 
  80.       $n++;//字串长度为1 
  81.     } elseif ($ascnum >= 192)//如果ASCII位高于192 
  82.     { 
  83.       $returnstr = $returnstr . substr($sourcestr, $i, 2);//根据UTF-8编码规范,将2个连续的字符记为单个字符 
  84.       $i = $i + 2;//实际Byte记为2 
  85.       $n++;//字串长度为1 
  86.     } elseif ($ascnum >= 65 && $ascnum <= 90)//如果是大写字母 
  87.     { 
  88.       $returnstr = $returnstr . substr($sourcestr, $i, 1); 
  89.       $i = $i + 1;//byte记为1 
  90.       $n++;//但考虑到整体美观,大写字母计成一个高位字符 
  91.     } else { 
  92.       $returnstr = $returnstr . substr($sourcestr, $i, 1); 
  93.       $i = $i + 1;//实际的Byte记为1 
  94.       $n = $n + 0.5;//小写字母和半角标点等与半个高位字符宽... 
  95.     } 
  96.   } 
  97.   if ($str_length > $cutlength) { 
  98.     $returnstr = $returnstr . "...";//超过长度时在尾处加上省略号 
  99.   } 
  100.   return $returnstr; 
  101. } 
  102. /*--------------------第五种方法(UTF-8)---------------------------------------------*/ 
  103. function FSubstr($title, $start, $len = "", $magic = true) 
  104. { 
  105.   if ($len == "") $len = strlen($title); 
  106.   if ($start != 0) { 
  107.     $startv = ord(substr($title, $start, 1)); 
  108.     if ($startv >= 128) { 
  109.       if ($startv < 192) { 
  110.         for ($i = $start - 1; $i > 0; $i--) { 
  111.           $tempv = ord(substr($title, $i, 1)); 
  112.           if ($tempv >= 192) break; 
  113.         } 
  114.         $start = $i; 
  115.       } 
  116.     } 
  117.   } 
  118.   if (strlen($title) <= $len) return substr($title, $start, $len); 
  119.   $alen = 0; 
  120.   $blen = 0; 
  121.   $realnum = 0; 
  122.   $length = 0; 
  123.   for ($i = $start; $i < strlen($title); $i++) { 
  124.     $ctype = 0; 
  125.     $cstep = 0; 
  126.     $cur = substr($title, $i, 1); 
  127.     if ($cur == "&") { 
  128.       if (substr($title, $i, 4) == "&lt;") { 
  129.         $cstep = 4; 
  130.         $length += 4; 
  131.         $i += 3; 
  132.         $realnum++; 
  133.         if ($magic) { 
  134.           $alen++; 
  135.         } 
  136.       } elseif (substr($title, $i, 4) == "&gt;") { 
  137.         $cstep = 4; 
  138.         $length += 4; 
  139.         $i += 3; 
  140.         $realnum++; 
  141.         if ($magic) { 
  142.           $alen++; 
  143.         } 
  144.       } elseif (substr($title, $i, 5) == "&amp;") { 
  145.         $cstep = 5; 
  146.         $length += 5; 
  147.         $i += 4; 
  148.         $realnum++; 
  149.         if ($magic) { 
  150.           $alen++; 
  151.         } 
  152.       } elseif (substr($title, $i, 6) == "&quot;") { 
  153.         $cstep = 6; 
  154.         $length += 6; 
  155.         $i += 5; 
  156.         $realnum++; 
  157.         if ($magic) { 
  158.           $alen++; 
  159.         } 
  160.       } elseif (preg_match("/&#(\d+);?/i", substr($title, $i), $match)) { 
  161.         $cstep = strlen($match[0]); 
  162.         $length += strlen($match[0]); 
  163.         $i += strlen($match[0]) - 1; 
  164.         $realnum++; 
  165.         if ($magic) { 
  166.           $blen++; 
  167.           $ctype = 1; 
  168.         } 
  169.       } 
  170.     } else { 
  171.       if (ord($cur) >= 252) { 
  172.         $cstep = 6; 
  173.         $length += 6; 
  174.         $i += 5; 
  175.         $realnum++; 
  176.         if ($magic) { 
  177.           $blen++; 
  178.           $ctype = 1; 
  179.         } 
  180.       } elseif (ord($cur) >= 248) { 
  181.         $cstep = 5; 
  182.         $length += 5; 
  183.         $i += 4; 
  184.         $realnum++; 
  185.         if ($magic) { 
  186.           $ctype = 1; 
  187.           $blen++; 
  188.         } 
  189.       } elseif (ord($cur) >= 240) { 
  190.         $cstep = 4; 
  191.         $length += 4; 
  192.         $i += 3; 
  193.         $realnum++; 
  194.         if ($magic) { 
  195.           $blen++; 
  196.           $ctype = 1; 
  197.         } 
  198.       } elseif (ord($cur) >= 224) { 
  199.         $cstep = 3; 
  200.         $length += 3; 
  201.         $i += 2; 
  202.         $realnum++; 
  203.         if ($magic) { 
  204.           $ctype = 1; 
  205.           $blen++; 
  206.         } 
  207.       } elseif (ord($cur) >= 192) { 
  208.         $ctype = 2; 
  209.         $length += 2; 
  210.         $i += 1; 
  211.         $realnum++; 
  212.         if ($magic) { 
  213.           $blen++; 
  214.           $ctype = 1; 
  215.         } 
  216.       } elseif (ord($cur) >= 128) { 
  217.         $length += 1; 
  218.       } else { 
  219.         $cstep = 1; 
  220.         $length += 1; 
  221.         $realnum++; 
  222.         if ($magic) { 
  223.           if (ord($cur) >= 65 && ord($cur) <= 90) { 
  224.             $blen++; 
  225.           } else { 
  226.             $alen++; 
  227.           } 
  228.         } 
  229.       } 
  230.     } 
  231.     if ($magic) { 
  232.       if (($blen * 2 + $alen) == ($len * 2)) break; 
  233.       if (($blen * 2 + $alen) == ($len * 2) + 1) { 
  234.         if ($ctype == 1) { 
  235.           $length -= $cstep; 
  236.           break; 
  237.         } else { 
  238.           break; 
  239.         } 
  240.       } 
  241.     } else { 
  242.       if ($realnum == $len) break; 
  243.     } 
  244.   } 
  245.   unset($cur); 
  246.   unset($alen); 
  247.   unset($blen); 
  248.   unset($realnum); 
  249.   unset($ctype); 
  250.   unset($cstep); 
  251.   return substr($title, $start, $length); 
  252. } 
  253. function utf8Substr($str, $from, $len) 
  254. { 
  255.   return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $from . '}' . 
  256.     '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $len . '}).*#s', 
  257.     '$1', $str); 
  258. } 
  259. $title = "你哈珀niad1纳斯达wop asdni你爱谁都没阿斯顿撒旦12ccs- sd"; 
  260. $title = utf8Substr($title, 0, 15); 
  261. echo $title; 
  262. ?>

Tags: PHP切割汉字

分享到: