自定义的PHP字符串截取函数
- 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);
- }
- }
- ?>
复制代码
|