<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>
            php語言

            php摘要生成函數詳解

            時間:2025-02-17 14:23:58 php語言 我要投稿
            • 相關推薦

            php摘要生成函數詳解

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

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

              復制代碼 代碼如下:

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

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

              return $string;

              }

              $pre = chr(1);

              $end = chr(1);

              //保護特殊字符串

              $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);

              //修復出現特殊字符串截段的問題

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

              if($pos !== false) {

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

              }

              return $strcut.$dot;

              }

            【php摘要生成函數詳解】相關文章:

            PHP時間和日期函數詳解10-17

            PHP之sprintf函數用法詳解10-21

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

            PHP自帶的幾個實用的數組函數詳解10-07

            PHP編碼轉換函數應用技巧詳解08-26

            PHP中strtotime函數使用方法詳解11-16

            PHP中生成UUID自定義函數分享05-11

            PHP常用字符串相關函數詳解11-03

            詳解PHP用substr函數截取字符串08-23

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看