麻辣堂|资源主站|开发论坛|在线手册
首页 Apache Linux Java MySQL 注册帮助 
PHP项目开发组是PHP开发资源网于2007组年建成立的项目开发团队,目前核心开发成员有27人, 项目协作成员8名.下设7个开发组,主要承接大/中型网站项目开发任务。

    由于开发任务较多,人员比较紧张,现面向社会招聘全职或者兼职开发人员,不管你是在校大学生,还是全职开发人员,以及SOHO都可以联系本站,我们可以长期合作,并为您带来丰厚的报酬。
  您现在的位置:PHP开发资源网 > 麻辣堂 > 详细资料
待解决
PHP截取字符串 包函HTML标志的也可截取 不会截取半个标签或者标签不成对
悬赏分:20 - 2007年08月22日

最近需要在HTML的内容中提取一段文本作为简介,如果用普通的办法提取那么将有可能出现截取了半个HTML标志的情况,会破坏页面布局, 为此添加了这么一个函数,可完美解决此问题!!注意在这里你截取的字符数是不含HTML标志的!!
[php]
<?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 = '';
$re = substr($str,0,$i).$ends;
if($more) $re.='...';
return $re;
}

$str=<<

What is the GNU project?


The GNU Project was launched in 1984 to develop a complete Unix-like operating system which is free software: the GNU system. Variants of the GNU 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 GNU/Linux systems.


GNU is a recursive acronym for “GNU's Not Unix”; it is pronounced guh-noo, approximately like canoe.


What is Free Software?


Free software” 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”.


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:



  • The freedom to run the program, for any purpose (freedom 0).

  • 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.

  • The freedom to redistribute copies so you can help your neighbor (freedom 2).

  • 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.


What is the Free Software Foundation?


The 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. ) 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.


Please consider helping the by , or by . If you use Free Software in your business, you can also consider or as a way to support the . by , or by . If you use Free Software in your business, you can also consider or as a way to support the .


The GNU project supports the mission of the 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 . 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 .



EOF;
echo phpos_chsubstr_ahtml($str,800);
?>
[/php]

提问者:xjtdy888   08-22 16:04
答复
路过。。。顺便帮顶:)
回答者:玉米づ冰冻可乐 - 瓦岗村民 8-22 09:10
我也来回答:
不管你有没有帮助我们,瓦岗寨8万村民将感谢你。。。。。

为防止灌水,您需要计算一道数学题: 答案:
35 + 24 = ? 请将计算结果填在上面

 
[]
©2007 PhpRes.COM