Chinaunix首页 | 论坛 | 博客
  • 博客访问: 136390
  • 博文数量: 62
  • 博客积分: 2501
  • 博客等级: 少校
  • 技术积分: 640
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-25 12:20
文章分类

全部博文(62)

文章存档

2013年(2)

2011年(1)

2007年(59)

我的朋友
最近访客

分类:

2007-06-10 20:58:27

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

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