截取HTML字符串 允许忽略HTML标志不计

本文介绍了一个PHP函数,用于截取包含HTML标签的字符串,并确保不会切断HTML标签,同时可选择性地添加省略号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php
/**
* 截取HTML字符串 允许忽略HTML标志不计
*
* Author:学无止境
* Email:xjtdy888@163.com
* QQ: 339534039
* Home:http://www.phpos.org
* Blog:http://hi.baidu.com/phps
*
* 转载请保留作者信息


* @param 要截取的HTML $str
* @param 截取的数量 $num
* @param 是否需要加上更多 $more
* @return 截取串
*/
function phpos_chsubstr_ahtml($str,$num,$more=false)
{
    
$leng=strlen($str);
      
if($num>=$leng)      return $str;
    
$word=0;
    
$i=0;                        /** 字符串指针 **/
    
$stag=array(array());        /** 存放开始HTML的标志 **/
    
$etag=array(array());        /** 存放结束HTML的标志 **/
    
$sp = 0;
    
$ep = 0;
      
while($word!=$num)
      {

          
if(ord($str[$i])>128)
          {
            
//$re.=substr($str,$i,3);
            $i+=3;
            
$word++;
          }
          
else if ($str[$i]=='<')
          {
              
if ($str[$i+1== '!')
              {
                
$i++;
                  
continue;
              }

              
if ($str[$i+1]=='/')    
              {
                
$ptag=&$etag ;
                
$k=&$ep;
                
$i+=2;
              }
              
else                    
              {
                
$ptag=&$stag;
                
$i+=1;
                
$k=&$sp;
              }

              
for(;$i<$leng;$i++)        
              {
                  
if ($str[$i== ' ')
                  {
                    
$ptag[$k= implode('',$ptag[$k]);
                    
$k++;
                      
break;
                  }
                  
if ($str[$i!= '>'
                  {
                    
$ptag[$k][]=$str[$i];
                      
continue;
                  }
                  
else                
                  {
                    
$ptag[$k= implode('',$ptag[$k]);
                    
$k++;
                      
break;
                  }
              }
            
$i++;
              
continue;
          }
          
else
          {
            
//$re.=substr($str,$i,1);
            $word++;
            
$i++;
          }
      }
      
foreach ($etag as $val)
      {
        
$key1=array_search($val,$stag);
          
if ($key1 !== false)          unset($stag[$key]);
      }
      
foreach ($stag as $key => $val)
      {
          
if (in_array($val,array('br','img'))) unset($stag[$key1]);
      }
    
array_reverse($stag);
    
$ends = '</'.implode('></',$stag).'>';
    
$re = substr($str,0,$i).$ends;
      
if($more)    $re.='...';
      
return $re;
}

$str=<<<EOF
<h3>What is the <acronym>GNU</acronym> pr<a><a><a>oject?</h3>
<p>The <acronym>GNU</acronym> Project was launched in 1984 to develop a complete Unix-like operating system which is <a href="http://www.gnu.org/philosophy/free-sw.html">free software</a>: the <acronym>GNU</acronym> system. Variants of the <acronym>GNU</acronym> operating system, which use the kernel called Linux, are now widely used; though these systems are often referred to as &ldquo;Linux&rdquo;, they are more accurately called <a href="http://www.gnu.org/gnu/linux-and-gnu.html">GNU/Linux systems</a>. </p>
<p><acronym>GNU</acronym> is a recursive acronym for &ldquo;GNU's Not Unix&rdquo;; it is pronounced <em>guh-noo</em>, approximately like <em>canoe</em>.</p>
<h3>What is Free Software?</h3>
<p>&ldquo;<a href="http://www.gnu.org/philosophy/free-sw.html">Free software</a>&rdquo; is a matter of liberty, not price. To understand the concept, you should think of &ldquo;free&rdquo; as in &ldquo;free speech&rdquo;, not as in &ldquo;free beer&rdquo;.</p>
<p>Free software is a matter of the users
' freedom to run, copy, distribute, study, change and improve the software. More precisely, it refers to four kinds of freedom, for the users of the software:</p>
<ul>
      
<li>The freedom to run the program, for any purpose (freedom 0). </li>
      
<li>The freedom to study how the program works, and adapt it to your needs (freedom 1). Access to the source code is a precondition for this. </li>
      
<li>The freedom to redistribute copies so you can help your neighbor (freedom 2). </li>
      
<li>The freedom to improve the program, and release your improvements to the public, so that the whole community benefits (freedom 3). Access to the source code is a precondition for this. </li>
</ul>
<h3>What is the Free Software Foundation?</h3>
<p>The <a href="http://www.fsf.org/">Free Software Foundation</a> (<abbr title="Free Software Foundation">) is the principal organizational sponsor of the Project. The receives very little funding from corporations or grant-making foundations, but relies on support from individuals like you. </abbr>) is the principal organizational sponsor of the Project. The receives very little funding from corporations or grant-making foundations, but relies on support from individuals like you. </p>
<p>Please consider helping the <abbr>by , or by . If you use Free Software in your business, you can also consider or as a way to support the . </abbr>by , or by . If you use Free Software in your business, you can also consider or as a way to support the . </p>
<p>The <acronym>GNU</acronym> project supports the mission of the <abbr>to preserve, protect and promote the freedom to use, study, copy, modify, and redistribute computer software, and to defend the rights of Free Software users. We support the on the Internet, , and the unimpeded by private monopolies. You can also learn more about these issues in the book . </abbr>to preserve, protect and promote the freedom to use, study, copy, modify, and redistribute computer software, and to defend the rights of Free Software users. We support the on the Internet, , and the unimpeded by private monopolies. You can also learn more about these issues in the book . </p>
<!--
Keep link lines at 
72 characters or lynx will break them poorly.
Obviously
, we list ONLY the most useful/important URLs here.
Keep it short and sweet
: 3 lines and 2 columns is already enough
--><!-- BEGIN GNUmenu -->
EOF;
echo phpos_chsubstr_ahtml($str,800);
?>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值