多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內(nèi)最全I(xiàn)T社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > php開源 > php教程 > php截斷帶html字符串文章內(nèi)容的方法

php截斷帶html字符串文章內(nèi)容的方法

來源:程序員人生   發(fā)布時間:2013-10-26 05:12:14 閱讀次數(shù):4277次

文章截斷使用主要是在列表頁面時我沒有寫描述這樣只能在文章中截取字符串了,但使用php 自帶函數(shù)會導(dǎo)致div未結(jié)束,從而頁面混亂了,那么要如何解決此問題呢?

博主寫好一篇文章,博客后臺一般會在搜索頁面或者列表頁面給出文章標(biāo)題和截斷了的的文章部分作為進(jìn)一步閱讀的入口。

Function: mb_substr( $str, $start, $length, $encoding )

$str,需要截斷的字符串

$start,截斷開始處

$length,長度(注意,這個跟mb_strimwidth不同,1就代表一個中文字符)

$encoding,編碼,我設(shè)為 utf-8

例,截斷文章標(biāo)題,控制在15個文字,代碼如下:

<?php echo mb_substr('www.phpfensi.com原創(chuàng)', 0, 15,"utf-8"); ?>

這樣對于純文本沒問題,但是我的是中間有html標(biāo)簽的于是問題來了,怎樣截斷一篇文章,注意,這篇文章不僅僅是普通的字符串文本,而是包含了各種格式化標(biāo)簽和樣式內(nèi)容的文本,如果處理不當(dāng),這些閉合標(biāo)簽無法正常關(guān)閉,從而破壞整個文檔流。

如果單純是純文本,下面這個函數(shù)差不多是夠用的,代碼如下:

  1. <?php 
  2.     /** 
  3.      * 字符串截取,支持中文和其他編碼 
  4.      * 
  5.      * @param string $str 需要轉(zhuǎn)換的字符串 
  6.      * @param string $start 開始位置 
  7.      * @param string $length 截取長度 
  8.      * @param string $charset 編碼格式 
  9.      * @param string $suffix 截斷字符串后綴 
  10.      * @return string 
  11.      */ 
  12.     function substr_ext($str$start=0, $length$charset="utf-8"$suffix=""
  13.     { 
  14.         if(function_exists("mb_substr")){ 
  15.              return mb_substr($str$start$length$charset).$suffix
  16.     } 
  17.         elseif(function_exists('iconv_substr')){ 
  18.              return iconv_substr($str,$start,$length,$charset).$suffix
  19.         } 
  20.         $re['utf-8']  = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/"
  21.         $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/"
  22.         $re['gbk']    = "/[x01-x7f]|[x81-xfe][x40-xfe]/"
  23.         $re['big5']   = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/"
  24.         preg_match_all($re[$charset], $str$match); 
  25.         $slice = join("",array_slice($match[0], $start$length)); 
  26.         return $slice.$suffix
  27.     } 

但是,如果需要截斷是網(wǎng)頁中的某部分格式化文本,上面的函數(shù)就不夠用了,它不具備處理格式化標(biāo)簽的能力。

這時,需要一個新函數(shù),它應(yīng)該是以上函數(shù)的升級加強(qiáng)版,它必須有能力正確的處理標(biāo)簽,下面找到一個

strip_tags() 函數(shù)剝?nèi)?HTML、XML 以及 PHP 的標(biāo)簽。

例子1,代碼如下:

  1. <?php 
  2. echo strip_tags("Hello <b>world!</b>"); 
  3. ?> 

輸出:Hello world!

這樣就好做了我們只要在上面基礎(chǔ)上如下操作,代碼如下:

  1. <?php 
  2. $a = strip_tags("Hello <b>world!</b>"); 
  3. substr_ext( $a,10) ; 
  4. //但是發(fā)現(xiàn)html不見了這個也不是什么好的解決辦法了。 
  5. ?> 

接著google 發(fā)現(xiàn)cns寫了一個支持html截取字符串的函數(shù),代碼如下:

  1. /** 
  2.  
  3.  * 獲取字符在字符串中第N次出現(xiàn)的位置 
  4.  * @param string $text 字符串 
  5.  * @param string $key 字符 
  6.  * @param int $int N 
  7.  * @return int  
  8.  */ 
  9. function strpos_int($text$key$int
  10.     $keylen = strlen($key); 
  11.     global $textlen
  12.     if (!$textlen
  13.         $textlen = strlen($text); 
  14.     static $textpos = 0; 
  15.     $pos = strpos($text$key); 
  16.     $int--; 
  17.     if ($pos
  18.     { 
  19.         if ($int == 0) 
  20.             $textpos+=$pos
  21.         else 
  22.             $textpos+=$pos + $keylen
  23.     } 
  24.     else 
  25.     { 
  26.         $int = 0; 
  27.         $textpos = $textlen
  28.     } 
  29.     if ($int > 0) 
  30.     { 
  31.         strpos_int(substr($text$pos + $keylen), $key$int); 
  32.     } 
  33.     return $textpos
  34.  
  35. /** 
  36.  * 截取HTML 
  37.  * @param string $string  HTML 字符串 
  38.  * @param int $length 截取的長度 
  39.  * @param string $dot 
  40.  * @param string $append 
  41.  * @return string 
  42.  */ 
  43. function cuthtml($string$length$dot = ' ...'$append = ""
  44.     $str = strip_tags($string);//先過濾標(biāo)簽 
  45.     $new_str = iconv_substr($str, 0, $length'utf-8'); 
  46.     $last = iconv_substr($new_str, -1, 1, 'utf-8'); 
  47.     $sc = substr_count($new_str$last); 
  48.     $position = strpos_int($string$last$sc); //獲取截取真實(shí)的長度 
  49.     if (function_exists('tidy_parse_string'))//服務(wù)器開啟tidy的話 直接用函數(shù)不全html代碼即可 
  50.     { 
  51.         $options = array("show-body-only" => true); 
  52.         return tidy_parse_string(mb_substr($string, 0, $position) . $dot . $append$options'UTF8'); 
  53.     } else //沒有開啟tidy 
  54.     { 
  55.         if (strlen($string) <= $position
  56.         { 
  57.             return $string
  58.         } 
  59.         $pre = chr(1); 
  60.         $end = chr(1); 
  61.         $string = str_replace(array('&''"''<''>'), array($pre . '&' . $end$pre . '"' . $end$pre . '<' . $end$pre . '>' . $end), $string); 
  62.         $strcut = ''
  63.         $n = $tn = $noc = 0; 
  64.         while ($n < strlen($string)) 
  65.         { 
  66.             $t = ord($string[$n]); 
  67.             if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) 
  68.             { 
  69.                 $tn = 1; 
  70.                 $n++; 
  71.                 $noc++; 
  72.             } elseif (194 <= $t && $t <= 223) 
  73.             { 
  74.                 $tn = 2; 
  75.                 $n += 2; 
  76.                 $noc += 2; 
  77.             } elseif (224 <= $t && $t <= 239) 
  78.             { 
  79.                 $tn = 3; 
  80.                 $n += 3; 
  81.                 $noc += 2; 
  82.             } elseif (240 <= $t && $t <= 247) 
  83.             { 
  84.                 $tn = 4; 
  85.                 $n += 4; 
  86.                 $noc += 2; 
  87.             } elseif (248 <= $t && $t <= 251) 
  88.             { 
  89.                 $tn = 5; 
  90.                 $n += 5; 
  91.                 $noc += 2; 
  92.             } elseif ($t == 252 || $t == 253) 
  93.             { 
  94.                 $tn = 6; 
  95.                 $n += 6; 
  96.                 $noc += 2; 
  97.             } else 
  98.             { 
  99.                 $n++; 
  100.             } 
  101.             if ($noc >= $position
  102.             { 
  103.                 break
  104.             } 
  105.         } 
  106.         if ($noc > $position
  107.         { 
  108.             $n -= $tn
  109.         } 
  110.         $strcut = substr($string, 0, $n); 
  111.         $strcut = str_replace(array($pre . '&' . $end$pre . '"' . $end$pre . '<' . $end$pre . '>' . $end), array('&''"''<''>'), $strcut); 
  112.         $pos = strrpos($strcutchr(1)); 
  113.         if ($pos !== false) 
  114.         { 
  115.             $strcut = substr($strcut, 0, $pos); 
  116.         } 
  117.         return $strcut . $dot . $append
  118.     } 
  119. }
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 国产成人一区在线播放 | 成人国产一区二区三区精品 | 在线免费视频a | 另类图片综合网 | 免费福利在线 | 奇米4444 | 中文字幕乱码视频 | 最近中文免费字幕8 | 国产成人综合欧美精品久久 | 高清在线观看视频 | 免费羞羞视频 | 欧美高清性刺激毛片 | 最新lutube亚洲看片在线观看 | 久久国产精品久久久久久久久久 | 亚洲精品乱码中文字幕无线 | 亚洲成a人在线播放www | 欧美一级特黄aa大片视频 | 高清不卡一区 | 国产精品一区二区三区四区五区 | 国产区精品视频 | 性欧美video视频另类 | 国产亚洲综合精品一区二区三区 | 久操精品| 中文字幕第30页 | 欧美人与性禽xxxx | 日本欧美韩国一区二区三区 | 色网色| 久久好色 | 欧美色图偷窥自拍 | 视频一区二区在线 | 日本不卡一区视频 | 成人毛片18女人毛片 | 日韩中文字幕视频在线 | 日本欧美一区二区三区乱码 | a级毛片黄片 | 欧美wwwxxxx | 免费看欧美毛片大片免费看 | 高清一级做a爱过程免费视频 | 午夜色视频在线观看 | 中文字幕在线观看免费 | 国产九色 |