php余分な文字を切り取るには省略記号で代用します


文字列の切り取りは符号化の問題に関連する場合があり、自分で書いたものが不完全であれば、切り取りに文字化けの問題が発生しやすく、以下の小さな関数機能ブロックは通常、文章の説明とタイトルを切り取る際に使用されます.
function cut_str($sourcestr,$cutlength)
{
   $returnstr=;
   $i=0;
   $n=0;
   $str_length=strlen($sourcestr);//       
   while (($n<$cutlength) and ($i<=$str_length))
   {
      $temp_str=substr($sourcestr,$i,1);
      $ascnum=Ord($temp_str);//       $i    ascii 
      if ($ascnum>=224)    //  ASCII   224,
      {
$returnstr=$returnstr.substr($sourcestr,$i,3); //  UTF-8    , 3                    
         $i=$i+3;            //  Byte  3
         $n++;            //     1
      }
      elseif ($ascnum>=192) //  ASCII   192,
      {
         $returnstr=$returnstr.substr($sourcestr,$i,2); //  UTF-8    , 2            
         $i=$i+2;            //  Byte  2
         $n++;            //     1
      }
      elseif ($ascnum>=65 && $ascnum<=90) //       ,
      {
         $returnstr=$returnstr.substr($sourcestr,$i,1);
         $i=$i+1;            //   Byte   1 
         $n++;            //       ,            
      }
      else                //     ,             ,
      {
         $returnstr=$returnstr.substr($sourcestr,$i,1);
         $i=$i+1;            //   Byte  1 
         $n=$n+0.5;        //                  …
      }
   }
         if ($str_length>$i){
          $returnstr = $returnstr . "…";//             
      }
    return $returnstr;
}