当前位置:首页> PHP教程> PHP精通
关键字
文章内容
最强的PHP字符串截取函数
 
 
修改时间:[2009/05/13 05:47]    阅读次数:[1124]    发表者:[起缘]
 

<?php
/***********************************************************************/
/*                                                                     */
/*  Author:                                                     */
/*  HomePage: www.cnphper.com                                          */
/*  Email: [email protected]                                             */
/*  QQ:1984412                                                         */
/*                                                                     */
/***********************************************************************/

function get_substr($string,$start='0',$length='')
{
 $start = (int)$start;
 $length = (int)$length;
 $i = 0;
 if(!$string)
 {
  return;
 }
 if($start>=0)
 {
  while($i<$start)
  {
   if(ord($string[$i])>127)
   {
    $i = $i+2;
   }
   else
   {
    $i++;
   }
  }
  $start = $i;
  if($length=='')
  {
   return substr($string,$start);
  } 
  elseif($length>0)
  {
   $end = $start+$length;
   while($i<$end)
   {
    if(ord($string[$i])>127)
    {
     $i = $i+2;
    }
    else
    {
     $i++;
    }
   }
   if($end != $i-1)
   {
    $end = $i;
   }
   else
   {
    $end--;
   }
   $length = $end-$start;
   return substr($string,$start,$length);
  }
  elseif($length==0)
  {
   return;
  }
  else
  {
   $length = strlen($string)-abs($length)-$start;
   return get_substr($string,$start,$length);
  }
 }
 else
 {
  $start = strlen($string)-abs($start);
  return get_substr($string,$start,$length);
 } 
}

?>