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

php截取html字符串及自动补全html标签的方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-08 18:09:45 浏览: 评论:0 

这篇文章主要介绍了php截取html字符串及自动补全html标签的方法,是php对html操作非常实用的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了php截取html字符串及自动补全html标签的方法。分享给大家供大家参考。具体分析如下:

这里总结一下关于利用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.  * 截取中文字符串 
  48.  * @param $string 字符串 
  49.  * @param $start 起始位 
  50.  * @param $length 长度 
  51.  * @param $charset&nbsp; 编码 
  52.  * @param $dot 附加字串 
  53.  */ 
  54. function msubstr($string, $start, $length,$dot='',$charset = 'UTF-8') { 
  55.  $string = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;','&nbsp;'), array('&', '"', '<', '>',' '), $string); 
  56.  if(strlen($string) <= $length) { 
  57.  return $string; 
  58.  } 
  59.  
  60.  if(strtolower($charset) == 'utf-8') { 
  61.  $n = $tn = $noc = 0; 
  62.  while($n < strlen($string)) { 
  63.  $t = ord($string[$n]); 
  64.  if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) { 
  65.  $tn = 1; $n++; 
  66.  } elseif(194 <= $t && $t <= 223) { 
  67.  $tn = 2; $n += 2; 
  68.  } elseif(224 <= $t && $t <= 239) { 
  69.  $tn = 3; $n += 3; 
  70.  } elseif(240 <= $t && $t <= 247) { 
  71.  $tn = 4; $n += 4; 
  72.  } elseif(248 <= $t && $t <= 251) { 
  73.  $tn = 5; $n += 5; 
  74.  } elseif($t == 252 || $t == 253) { 
  75.  $tn = 6; $n += 6; 
  76.  } else { 
  77.  $n++; 
  78.  } 
  79.  $noc++; 
  80.  if($noc >= $length) { 
  81.  break; 
  82.  } 
  83.  } 
  84.  if($noc > $length) { 
  85.  $n -= $tn; 
  86.  } 
  87.  $strcut = substr($string, 0, $n); 
  88.  } else { 
  89.  for($i = 0; $i < $length; $i++) { 
  90.  $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i]; 
  91.  } 
  92.  } 
  93.  
  94.  return $strcut.$dot; 
  95. } 
  96.  
  97. /** 
  98.  * 取得字符串的长度,包括中英文。 
  99.  */ 
  100. function mstrlen($str,$charset = 'UTF-8'){ 
  101.  if (function_exists('mb_substr')) { 
  102.  $length=mb_strlen($str,$charset); 
  103.  } elseif (function_exists('iconv_substr')) { 
  104.  $length=iconv_strlen($str,$charset); 
  105.  } else { 
  106.  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;  
  107.  $length=count($ar[0]); 
  108.  } 
  109.  return $length; 
  110. } 

实例,代码如下:

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

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

分享到: