防止截取摘要时将html标签截断

做文章系统首页时经常需要截断文章,只显示文章一部分摘要内容,这样将不会导致文章过多时使首页看起来过于臃肿。

截断含html标签的文章内容时会碰到一些问题,比如截取的地方正好是img标签,这样就会使整个页面变形。

在网上找到一种解决方法http://www.hua2r.com/blog/20c3dfecb972ccb0421cd0402bfa1106.xml

本BLOG系统首页就是用这种思路来截取字符串的。

具体思路为:

  1. 用正则将html标签和文章内容分开
  2. html标签不参加字符串长度计算(如果碰到html标签,直接拼加,是真正内容时,才计算长度)
  3. 根据html标签进/出栈(Stack,先进后出)
  4. 循环第2步,3步
  5. 超过长度,跳出循环,如果栈中有html标记,将html标记补到内容后面

上面网址列出的代码片段有几个小问题,没有考虑到font标签的处理情况。下面贴出俺修改后的

  1. function htmlSubString($content$maxLength=300) {   
  2.     $content = preg_split('/(<[^>]+?>)/i'$content, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);   
  3.     $outString = '';   
  4.     $curLength = 0;   
  5.     $tagStack = array();   
  6.     foreach($content as $value) {   
  7.         if(trim($value) == ''continue;   
  8.         $outString .= $value;   
  9.         if (preg_match('/^([^<>]+)$/i'$value)) {   
  10.             $curLength += strlen($value);   
  11.             if($maxLength < $curLengthbreak;   
  12.         } else if(preg_match('/<IMG([^>]+)?>/i'$value) || preg_match('/<PARAM([^>]+)?>/i'$value) || preg_match('/<!([^>]+)?>/i'$value) || preg_match('/<HR([^>]+)?>/i'$value) || preg_match('/<BR([^>]+)?>/i'$value)) {   
  13.             continue;   
  14.         } else if(preg_match('/<\/([^>]+?)>/i'$value$matches)) {   
  15.             array_pop($tagStack);   
  16.         } else if(preg_match('/<(\w*)\s*.*/i'$value$matches)) {   
  17.             array_push($tagStack$matches[1]);   
  18.         } else {   
  19.             break;   
  20.         }   
  21.     }   
  22.   
  23.     $patchStack = array_reverse($tagStack);   
  24.     foreach($patchStack as $tagName) {   
  25.         $outString .= "</$TAGNAME>";   
  26.     }   
  27.     return $outString;   
  28. }  
 

本文相关评论|Comments

hey, nice blog. i v learned a lot.

just stop by and say thx.
 

发表该文评论|Send Comment

相关文章|Related Articles
  • 没有相关文章
相关标签|Related Tags