Home > Backend Development > PHP Tutorial > Customized PHP string interception function

Customized PHP string interception function

WBOY
Release: 2016-07-25 09:10:50
Original
718 people have browsed it
自定义的PHP字符串截取函数
  1. function get_substr($string,$start='0',$length='')
  2. {
  3.   $start = (int)$start;
  4.   $length = (int)$length;
  5.   $i = 0;
  6.   if(!$string)
  7.   {
  8.     return;
  9.   }
  10.   if($start>=0)
  11.   {
  12.     while($i<$start)
  13.     {
  14.       if(ord($string[$i])>127)
  15.       {
  16.         $i = $i+2;
  17.       }
  18.       else
  19.       {
  20.         $i++;
  21.       }
  22.     }
  23.     $start = $i;
  24.     if($length=='')
  25.     {
  26.       return substr($string,$start);
  27.     }
  28.     elseif($length>0)
  29.     {
  30.       $end = $start+$length;
  31.       while($i<$end)
  32.       {
  33.         if(ord($string[$i])>127)
  34.         {
  35.           $i = $i+2;
  36.         }
  37.         else
  38.         {
  39.           $i++;
  40.         }
  41.       }
  42.       if($end != $i-1)
  43.       {
  44.         $end = $i;
  45.       }
  46.       else
  47.       {
  48.         $end--;
  49.       }
  50.       $length = $end-$start;
  51.       return substr($string,$start,$length);
  52.     }
  53.     elseif($length==0)
  54.     {
  55.       return;
  56.     }
  57.     else
  58.     {
  59.       $length = strlen($string)-abs($length)-$start;
  60.       return get_substr($string,$start,$length);
  61.     }
  62.   }
  63.   else
  64.   {
  65.     $start = strlen($string)-abs($start);
  66.     return get_substr($string,$start,$length);
  67.   }
  68. }
  69. ?>
复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template