php汉字的截取完美解决方案

php汉字的截取完美解决方案 函数巧妙地运用了正则表达式, 用起来很方便, 就像 substr 的用法一样, 可以正向截取也可反相截取, 思路值得学习.
函数巧妙地运用了正则表达式, 用起来很方便, 就像 substr 的用法一样, 可以正向截取也可反相截取, 思路值得学习.

PHP代码:
  1. <?php
  2. function c_substr($string, $from, $length = null){
  3.     preg_match_all('/[x80-xff]?./', $string, $match);
  4.     if(is_null($length)){
  5.         $result = implode('', array_slice($match[0], $from));
  6.     }else{
  7.         $result = implode('', array_slice($match[0], $from, $length));
  8.     }
  9.     return $result;
  10. }
  11. $str = "zhon华人min共和guo";
  12. $from = 3;
  13. $length = 7;
  14. echo(c_substr($str, $from, $length));
  15. // 输出: n华人min共
  16. //还有utf-8的
  17. /*
  18. Regarding windix's function to handle UTF-8 strings:
  19. one can use the "u" modifier on the regular expression so that the pattern string is treated as UTF-8
  20. (available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32).
  21. This way the function works for other encodings too (like Greek for example).
  22. The modified function would read like this:
  23. */
  24. function utf8_substr($str,$start) {
  25. $null = "";
  26.    preg_match_all("/./u", $str, $ar);
  27.    if(func_num_args() >= 3) {
  28.        $end = func_get_arg(2);
  29.        return join($null, array_slice($ar[0],$start,$end));
  30.    } else {
  31.        return join($null, array_slice($ar[0],$start));
  32.    }
  33. }
  34. ?>
  之二:
  1. <?php
  2. /***************************************************/
  3. /*                                                                     */
  4. /*  Author:                                                     */
  5. /*  HomePage: www..com                                          */
  6. /*  Email: phforum@163.com                                             */
  7. /*  QQ:1984412                                                         */
  8. /*                                                                     */
  9. /***************************************************/
  10. function get_substr($string,$start='0',$length='')
  11. {
  12. $start = (int)$start;
  13. $length = (int)$length;
  14. $i = 0;
  15. if(!$string)
  16. {
  17.   return;
  18. }
  19. if($start>=0)
  20. {
  21.   while($i<$start)
  22.   {
  23.    if(ord($string[$i])>127)
  24.    {
  25.     $i = $i+2;
  26.    }
  27.    else
  28.    {
  29.     $i++;
  30.    }
  31.   }
  32.   $start = $i;
  33.   if($length=='')
  34.   {
  35.    return substr($string,$start);
  36.   }
  37.   elseif($length>0)
  38.   {
  39.    $end = $start+$length;
  40.    while($i<$end)
  41.    {
  42.     if(ord($string[$i])>127)
  43.     {
  44.      $i = $i+2;
  45.     }
  46.     else
  47.     {
  48.      $i++;
  49.     }
  50.    }
  51.    if($end != $i-1)
  52.    {
  53.     $end = $i;
  54.    }
  55.    else
  56.    {
  57.     $end--;
  58.    }
  59.    $length = $end-$start;
  60.    return substr($string,$start,$length);
  61.   }
  62.   elseif($length==0)
  63.   {
  64.    return;
  65.   }
  66.   else
  67.   {
  68.    $length = strlen($string)-abs($length)-$start;
  69.    return get_substr($string,$start,$length);
  70.   }
  71. }
  72. else
  73. {
  74.   $start = strlen($string)-abs($start);
  75.   return get_substr($string,$start,$length);
  76. }
  77. }
  78. ?>
引用通告地址: 点击获取引用地址
评论: 0 | 引用: 0 | 阅读: 2398
发表评论
昵 称: 密 码:
网 址: 邮 箱:
选 项:    
头 像:
内 容: