av手机免费在线观看,国产女人在线视频,国产xxxx免费,捆绑调教一二三区,97影院最新理论片,色之久久综合,国产精品日韩欧美一区二区三区

php語言

php摘要生成函數(shù)詳解

時(shí)間:2025-02-17 14:23:58 php語言 我要投稿
  • 相關(guān)推薦

php摘要生成函數(shù)詳解

  以前也寫過一個(gè)PHP文章摘要生成方法(函數(shù)), 不過,不怎么好用,也出現(xiàn)亂碼,現(xiàn)在再發(fā)布一個(gè),這個(gè)函數(shù)是在某開源系統(tǒng)上拆下來了,希望對大家用用。

  在使用的時(shí)候,得先把要生成摘要的內(nèi)容strip_tags()一下,當(dāng)然,你也可以把strip_tags()直接添加到函數(shù)中,我沒有搞,自己添加吧。下面是函數(shù):

  復(fù)制代碼 代碼如下:

  function cutstr($string, $length,$charset,$dot) {//字符,截取長度,字符集,結(jié)尾符

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

  return $string;

  }

  $pre = chr(1);

  $end = chr(1);

  //保護(hù)特殊字符串

  $string = str_replace(array('&', '"', '<', '>'), array($pre.'&'.$end, $pre.'"'.$end, $pre.'<'.$end, $pre.'>'.$end), $string);

  $strcut = '';

  if(strtolower($charset) == 'utf-8') {

  $n = $tn = $noc = 0;

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

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

  if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {

  $tn = 1; $n++; $noc++;

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

  $tn = 2; $n += 2; $noc += 2;

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

  $tn = 3; $n += 3; $noc += 2;

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

  $tn = 4; $n += 4; $noc += 2;

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

  $tn = 5; $n += 5; $noc += 2;

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

  $tn = 6; $n += 6; $noc += 2;

  } else {

  $n++;

  }

  if($noc >= $length) {

  break;

  }

  }

  if($noc > $length) {

  $n -= $tn;

  }

  $strcut = substr($string, 0, $n);

  } else {

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

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

  }

  }

  //還原特殊字符串

  $strcut = str_replace(array($pre.'&'.$end, $pre.'"'.$end, $pre.'<'.$end, $pre.'>'.$end), array('&', '"', '<', '>'), $strcut);

  //修復(fù)出現(xiàn)特殊字符串截段的問題

  $pos = strrpos($s, chr(1));

  if($pos !== false) {

  $strcut = substr($s,0,$pos);

  }

  return $strcut.$dot;

  }

【php摘要生成函數(shù)詳解】相關(guān)文章:

PHP時(shí)間和日期函數(shù)詳解10-17

PHP之sprintf函數(shù)用法詳解10-21

php的date()日期時(shí)間函數(shù)詳解11-12

PHP自帶的幾個(gè)實(shí)用的數(shù)組函數(shù)詳解10-07

PHP編碼轉(zhuǎn)換函數(shù)應(yīng)用技巧詳解08-26

PHP中strtotime函數(shù)使用方法詳解11-16

PHP中生成UUID自定義函數(shù)分享05-11

PHP常用字符串相關(guān)函數(shù)詳解11-03

詳解PHP用substr函數(shù)截取字符串08-23