熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> PHP編程 >> 正文

php:根據中文裁減字符串函數方法

2022-06-13   來源: PHP編程 

  php:根據中文裁減字符串函數方法

  define(CHARSET UTF // 系統默認編碼

  /**

  * 根據中文裁減字符串

  * @param $string 字符串

  * @param $length 長度

  * @param $doc 縮略後綴

  * @return 返回帶省略號被裁減好的字符串

  */

  function cutstr( $string $length $dot = ) {

  if ( strlen( $string ) <= $length ) {

  return $string;

  }

  $pre = chr(

  $end = chr(

  $string = str_replace( array( & < > array( $pre & $end $pre $end $pre < $end $pre > $end ) $string )

  $strcut = ;

  if ( strtolower( CHARSET ) == utf ) {

  $n = $tn = $noc = ;

  while ( $n < strlen( $string ) ) {

  $t = ord( $string[$n] )

  if ( $t == || $t == || ( <= $t && $t <= ) ) {

  $tn = ;

  $n++;

  $noc++;

  } elseif ( <= $t && $t <= ) {

  $tn = ;

  $n += ;

  $noc += ;

  } elseif ( <= $t && $t <= ) {

  $tn = ;

  $n += ;

  $noc += ;

  } elseif ( <= $t && $t <= ) {

  $tn = ;

  $n += ;

  $noc += ;

  } elseif ( <= $t && $t <= ) {

  $tn = ;

  $n += ;

  $noc += ;

  } elseif ( $t == || $t == ) {

  $tn = ;

  $n += ;

  $noc += ;

  } else {

  $n++;

  }

  if ( $noc >= $length ) {

  break;

  }

  }

  if ( $noc > $length ) {

  $n = $tn;

  }

  $strcut = substr( $string $n )

  } else {

  for ( $i = ; $i < $length; $i++ ) {

  $strcut = ord( $string[$i] ) > ? $string[$i] $string[++$i] : $string[$i];

  }

  }

  $strcut = str_replace( array( $pre & $end $pre $end $pre < $end $pre > $end ) array( & < > $strcut )

  $pos = strrpos( $strcut chr( ) )

  if ( $pos !== false ) {

  $strcut = substr( $strcut $pos )

  }

  return $strcut $dot;

  }


From:http://tw.wingwit.com/Article/program/PHP/201311/20833.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.