html保留標籤截字

來源:互聯網
上載者:User
主要思路是分析真箇文字,碰到標籤則開始處理,碰到非標籤開始計數,只考慮了UTF8
  1.  function smarty_modifier_truncate_utf8_html($str,$num = 80,$more_tag = '...')
  2. {
  3.     $str = trim($str);
  4.     $leng=strlen($str);
  5.     if($num>=$leng)      
  6.         return $str;
  7.     $word=0;
  8.     $i=0;                        /** 字串指標 **/
  9.     $stag=array();        /** 存放開始HTML的標誌 **/
  10.     $etag=array();        /** 存放結束HTML的標誌 **/
  11.     $sp = 0;
  12.     $ep = 0;
  13.     while($word<=$num)
  14.     {
  15.         if($i>$leng)    
  16.             break;
  17.         if($str[$i] == '<')
  18.         {
  19.             if($str[$i+1] == '!')
  20.             {
  21.                 $i = strpos($str, '-->', $i+1) + 1;
  22.             }
  23.             else if ($str[$i+1]=='/')    
  24.             {
  25.                 $new_pos = strpos($str, '>', $i+1);
  26.                 $etag[] = substr( $str, $i+2, $new_pos - ($i+2) );
  27.                 $i = $new_pos + 1;
  28.             }
  29.             else                    
  30.             {
  31.                 $tag_end_pos = strpos($str, '>', $i+1);
  32.                 $tag_space_pos = strpos($str, ' ', $i+1);
  33.                 if(!$tag_end_pos)   
  34.                     $tag_end_pos = $leng;
  35.                 if(!$tag_space_pos) 
  36.                     $tag_space_pos = $leng;
  37.                 if($tag_end_pos>$tag_space_pos)//have element abbritues
  38.                 {
  39.                     $stag[] = substr( $str, $i+1, $tag_space_pos - ($i+1) );
  40.                     $i = $tag_end_pos + 1;
  41.                 }
  42.                 else//no element abbritues
  43.                 {
  44.                     $stag[] = substr( $str, $i+1, $tag_end_pos - ($i+1) );
  45.                     $i = $tag_end_pos + 1;
  46.                 }
  47.             }       
  48.         }
  49.         else
  50.         {
  51.             if(ord($str[$i])>128)
  52.             {
  53.                 //$re.=substr($str,$i,3);
  54.                 $i+=3;
  55.                 $word++;
  56.             }
  57.             else
  58.             {
  59.                 $i++;
  60.                 if($str[$i-1]!="/n" && $str[$i-1]!="/r")    
  61.                 {
  62.                     $word++;
  63.                 }
  64.             }
  65.         }
  66.     }
  67.     if($i>$leng)    
  68.         $i = $leng;
  69.     //print_r($stag);
  70.     //echo '<hr>';
  71.     //print_r($etag);
  72.     foreach ($etag as $val)
  73.     {
  74.         $key=array_search($val,$stag);
  75.         if ($key !== false)        
  76.             unset($stag[$key]);
  77.     }
  78.     foreach ($stag as $key => $val)
  79.     {
  80.         if (in_array(strtolower($val),array('br','img'))) 
  81.             unset($stag[$key]);
  82.     }
  83.     $stag = array_reverse($stag);
  84.     $ends = '</'.implode('></',$stag).'>';
  85.     $re = substr($str,0,$i).$ends;
  86.     $re .= $more_tag;
  87.     return $re;
  88. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.