截取HTML字符串 允许忽略HTML标志不计
来源:互联网 发布:java怎么获取当前日期 编辑:程序博客网 时间:2024/04/29 17:33
<?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 “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);
?>
/**
* 截取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 “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);
?>
- 截取HTML字符串 允许忽略HTML标志不计
- 截取HTML字符串
- PHP截取HTML字符串
- php 截取HTML字符串
- HTML标志
- php读取html 截取字符串
- 去除HTML格式 截取字符串
- 忽略HTML标签的字符串替换问题
- 截取包含html格式的字符串
- PHP截取html字符串(二)
- 截取字符串 -- html标签闭合问题
- JAVA截取带有HTML的字符串[转]
- PHP 截取含有HTML的字符串
- 人人笔试题 截取html字符串
- php中英文字符串截取函数(包括html)
- smarty截取字符串与过滤HTML标签
- 一个html截取字符串的小工具
- php截取html字符串自动补全html标签
- 程序员的爱恋
- 终于申请好了自己的博客,欢迎来踩踩!
- [发布]冷风服务管理器1.2
- L.A.M.P环境配置文档—CentOS 5.0
- 编程感想
- 截取HTML字符串 允许忽略HTML标志不计
- C#中加强ListView控件的功能
- 今日软件集
- SOA & Webservice系列课程(1):面向服务的实质 -- 讲师: 吴延安
- 获取正则表达式匹配的字符串
- 安装VS2005 SP1之后无法更改或卸载VS2005的处理方法
- DATAGRIDVIEW
- VS2005中解决方案管理器中看不到解决方案节点的解决办法
- 常用正则表达式