当前位置:首页 > PHP教程 > php高级应用 > 列表

php截取html字符串自动补全html标签

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-28 14:31:22 浏览: 评论:0 

文章来总结一下关于利用php截取html字符串自动补全html标签,实际开发中会经常碰到,很多人直接先strip_tags过滤掉html标签,但是就只剩下纯文本了,可读性非常差,下面是一个函数,代码如下:

  1. /** 
  2.  * 截取HTML,并自动补全闭合 
  3.  * @param $html 
  4.  * @param $length 
  5.  * @param $end 
  6.  */ 
  7. function subHtml($html,$length) { 
  8.  $result = ''
  9.  $tagStack = array(); 
  10.  $len = 0; 
  11.  
  12.  $contents = preg_split("~(<[^>]+?>)~si",$html, -1,PREG_SPLIT_NO_EMPTY| PREG_SPLIT_DELIM_CAPTURE); 
  13.  foreach($contents as $tag
  14.  { 
  15.  if (trim($tag)=="")&nbsp; continue
  16.  if(preg_match("~<([a-z0-9]+)[^/>]*?/>~si",$tag)){ 
  17.  $result .= $tag
  18.  }else if(preg_match("~</([a-z0-9]+)[^/>]*?>~si",$tag,$match)){ 
  19.  if($tagStack[count($tagStack)-1] == $match[1]){ 
  20.  array_pop($tagStack); 
  21.  $result .= $tag
  22.  } 
  23.  }else if(preg_match("~<([a-z0-9]+)[^/>]*?>~si",$tag,$match)){ 
  24.  array_push($tagStack,$match[1]); 
  25.  $result .= $tag
  26.  }else if(preg_match("~<!--.*?-->~si",$tag)){ 
  27.  $result .= $tag
  28.  }else
  29.  if($len + mstrlen($tag) < $length){ 
  30.  $result .= $tag
  31.  $len += mstrlen($tag);  
  32.  }else { 
  33.  $str = msubstr($tag,0,$length-$len+1); 
  34.  $result .= $str
  35.  break
  36.  } 
  37.  
  38.  } 
  39.  } 
  40.  while(!emptyempty($tagStack)){ 
  41.  $result .= '</'.array_pop($tagStack).'>'
  42.  } 
  43.  return&nbsp; $result
  44.  
  45. /** 
  46.  * 截取中文字符串 
  47.  * @param $string 字符串 
  48.  * @param $start 起始位 
  49.  * @param $length 长度 
  50.  * @param $charset&nbsp; 编码 
  51.  * @param $dot 附加字串 
  52.  */ 
  53. function msubstr($string$start$length,$dot='',$charset = 'UTF-8') { 
  54.  $string = str_replace(array('&amp;''&quot;''&lt;''&gt;','&nbsp;'), array('&''"''<''>',' '), $string); 
  55.  if(strlen($string) <= $length) { 
  56.  return $string
  57.  } 
  58.  
  59.  if(strtolower($charset) == 'utf-8') { 
  60.  $n = $tn = $noc = 0; 
  61.  while($n < strlen($string)) { 
  62.  $t = ord($string[$n]); 
  63.  if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) { 
  64.  $tn = 1; $n++; 
  65.  } elseif(194 <= $t && $t <= 223) { 
  66.  $tn = 2; $n += 2; 
  67.  } elseif(224 <= $t && $t <= 239) { 
  68.  $tn = 3; $n += 3; 
  69.  } elseif(240 <= $t && $t <= 247) { 
  70.  $tn = 4; $n += 4; 
  71.  } elseif(248 <= $t && $t <= 251) { 
  72.  $tn = 5; $n += 5; 
  73.  } elseif($t == 252 || $t == 253) { 
  74.  $tn = 6; $n += 6; 
  75.  } else { 
  76.  $n++; 
  77.  } 
  78.  $noc++; 
  79.  if($noc >= $length) { 
  80.  break
  81.  } 
  82.  } 
  83.  if($noc > $length) { 
  84.  $n -= $tn
  85.  } 
  86.  $strcut = substr($string, 0, $n); 
  87.  } else { 
  88.  for($i = 0; $i < $length$i++) { 
  89.  $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i]; 
  90.  } 
  91.  } 
  92.  
  93.  return $strcut.$dot
  94.  
  95. /** 
  96.  * 取得字符串的长度,包括中英文。 
  97.  */ 
  98. function mstrlen($str,$charset = 'UTF-8'){ 
  99.  if (function_exists('mb_substr')) { 
  100.  $length=mb_strlen($str,$charset); 
  101.  } elseif (function_exists('iconv_substr')) { 
  102.  $length=iconv_strlen($str,$charset); 
  103.  } else {//开源代码phpfensi.com 
  104.  preg_match_all("/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/"$text$ar);&nbsp;  
  105.  $length=count($ar[0]); 
  106.  } 
  107.  return $length

实例,代码如下:

  1. * @param 要截取的HTML $str 
  2. * @param 截取的数量 $num 
  3. * @param 是否需要加上更多 $more 
  4. * @return 截取串 
  5. */ 
  6. function phpos_chsubstr_ahtml($str,$num,$more=false) 
  7.     $leng=strlen($str); 
  8.       if($num>=$leng)      return $str
  9.     $word=0; 
  10.     $i=0;                        /** 字符串指针 **/ 
  11.     $stag=array(array());        /** 存放开始HTML的标志 **/ 
  12.     $etag=array(array());        /** 存放结束HTML的标志 **/ 
  13.     $sp = 0; 
  14.     $ep = 0; 
  15.       while($word!=$num
  16.       { 
  17.  
  18.           if(ord($str[$i])>128) 
  19.           { 
  20.             //$re.=substr($str,$i,3); 
  21.             $i+=3; 
  22.             $word++; 
  23.           } 
  24.           else if ($str[$i]=='<'
  25.           { 
  26.               if ($str[$i+1] == '!'
  27.               { 
  28.                 $i++; 
  29.                   continue
  30.               } 
  31.  
  32.               if ($str[$i+1]=='/')     
  33.               { 
  34.                 $ptag=&$etag ; 
  35.                 $k=&$ep
  36.                 $i+=2; 
  37.               } 
  38.               else                     
  39.               { 
  40.                 $ptag=&$stag
  41.                 $i+=1; 
  42.                 $k=&$sp
  43.               } 
  44.  
  45.               for(;$i<$leng;$i++)         
  46.               { 
  47.                   if ($str[$i] == ' '
  48.                   { 
  49.                     $ptag[$k] = implode('',$ptag[$k]); 
  50.                     $k++; 
  51.                       break
  52.                   } 
  53.                   if ($str[$i] != '>')  
  54.                   { 
  55.                     $ptag[$k][]=$str[$i]; 
  56.                       continue
  57.                   } 
  58.                   else                 
  59.                   { 
  60.                     $ptag[$k] = implode('',$ptag[$k]); 
  61.                     $k++; 
  62.                       break
  63.                   } 
  64.               } 
  65.             $i++; 
  66.               continue
  67.           } 
  68.           else 
  69.           { 
  70.             //$re.=substr($str,$i,1); 
  71.             $word++; 
  72.             $i++; 
  73.           } 
  74.       } 
  75.       foreach ($etag as $val
  76.       { 
  77.         $key1=array_search($val,$stag); 
  78.           if ($key1 !== false)          unset($stag[$key]); 
  79.       } 
  80.       foreach ($stag as $key => $val
  81.       { 
  82.           if (in_array($val,array('br','img'))) unset($stag[$key1]); 
  83.       } 
  84.     array_reverse($stag); 
  85.     $ends = '</'.implode('></',$stag).'>'
  86.     $re = substr($str,0,$i).$ends
  87.       if($more)    $re.='...'
  88.       return $re

PHP截取字符串,生成文章摘要,我们在写BLOG时经常需要显示文章前一部分,但是又怕不恰当截断破坏封闭标签以造成整个文档结构破坏,代码如下:

  1. function text_zhaiyao($text,$length){ //文章摘要生成函数  $test:内容 $length:摘要长度 
  2.     global $Briefing_Length
  3.     mb_regex_encoding("UTF-8"); 
  4.     if(mb_strlen($text) <= $length ) return $text
  5.     $Foremost = mb_substr($text, 0, $length); 
  6.     $re = "<(/?) 
  7.     (P|DIV|H1|H2|H3|H4|H5|H6|ADDRESS|PRE|TABLE|TR|TD|TH|INPUT|SELECT|TEXTAREA|OBJECT|A|UL|OL|LI| 
  8.     BASE|META|LINK|HR|BR|PARAM|IMG|AREA|INPUT|SPAN)[^>]*(>?)"; 
  9.     $Single = "/BASE|META|LINK|HR|BR|PARAM|IMG|AREA|INPUT|BR/i"
  10.       
  11.     $Stack = array(); $posStack = array(); 
  12.       
  13.     mb_ereg_search_init($Foremost$re'i'); 
  14.       
  15.     while($pos = mb_ereg_search_pos()){ 
  16.     $match = mb_ereg_search_getregs(); 
  17.     /* [Child-matching Formulation]: 
  18.       
  19.     $matche[1] : A "/" charactor indicating whether current "<...>" Friction is 
  20.     Closing Part 
  21.     $matche[2] : Element Name. 
  22.     $matche[3] : Right > of a "<...>" Friction 
  23.     */ 
  24.     if($match[1]==""){ 
  25.     $Elem = $match[2]; 
  26.     if(mb_eregi($Single$Elem) && $match[3] !=""){ 
  27.     continue
  28.     } 

Tags: php截取html php补全html标签

分享到: