文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>fgetss()的BUG及纠正

fgetss()的BUG及纠正

时间:2007-02-17  来源:PHP爱好者

<?
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
/*
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
作者:朱江 [email protected]
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
毕业于北京工业大学
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
工作嘛,呵呵,不说也罢,那是一个并不让我引以为荣的地方
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN

c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
php4.X的系统所提供的fgetss()功能不完善,不能滤干净HTML TAG,分析
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
其中忽略了某些情况,以下代码是本人在开发过程中自制的土炮。
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN

c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
关于fgetss的BUG,可用如下代码研究一下:
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
$fp=fopen("index.html","r");
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
while (! feof($fp))
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
{
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
$ms=fgetss($fp);
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
printf($ms);
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
}
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
fclose($fp);
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN

c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
版杈 :免费
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
*/
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
function mygets($myFile)
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
{
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
//while(!feof($myFile))
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
//{
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
$myline = fgets($myFile, 255);
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN

c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
$big=strlen(strstr($myline,">"));
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
$small=strlen(strstr($myline,"<"));
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
if($big>$small) //此句很重要,如果HTML代码中间有换行时有用
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
{ //如果一行中首先出现的是>而不是<时有用
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
$myline=strstr($myline,">");
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
$myline=substr($myline,1);
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
}
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN

c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
$len=strlen($myline);
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
$startskip=false;
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
$outstring=""; //important!
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN

c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
for($i=1;$i<=$len;$i++) //去掉所有HTML代码
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
{
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
$a=substr($myline,$i-1,1);
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
switch($a)
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
{
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
case "<":
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
$startskip=true;
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
//$myline=substr($myline,">");
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
//$myline=strstr($myline,1);
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
break;
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
case ">":
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
//$myline=substr($myline,1);
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
$startskip=false;
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
break;
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
default:
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
}
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
if(!$startskip && $a!=">") $outstring=$outstring.$a;
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
}
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
$outstring=str_replace("&nbsp;"," ",$outstring);
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
//当然,以&开头的东东如果不想要可以再加,如法炮制,这里只是过滤&nbsp;
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
$outstring=str_replace(" ","",$outstring);
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
$outstring=str_replace(" ","",$outstring); //双引号内为全角空格
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
$outstring=str_replace("n","",$outstring);
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
return $outstring;
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
//}
c hina it power . c omo6rIogVPkxSXiJldYfJOEvweN
}
?>
php爱好者站 http://www.phpfans.net 网页制作|网站建设|数据采集.
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载