/**
* 截取HTML字符串 允許忽略HTML標誌不計
*
* Author:學無止境
* Email:[email protected]
* 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 “Linux”, 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 “GNU's Not Unix”; it is pronounced <em>guh-noo</em>, approximately like <em>canoe</em>.</p>
<h3>What is Free Software?</h3>
<p>“<a href="http://www.gnu.org/philosophy/free-sw.html">Free software</a>” is a matter of liberty, not price. To understand the concept, you should think of “free” as in “free speech”, not as in “free beer”.</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);
?>