给大家一个简单的文本操作的类 我以前写的,不过一直都没机会用了,文本不如数据库 数据是以行保存的,以n结尾,注意你输入的数据必须以"n"结尾的,这是几个最基本的类成员,文本的特别要注意效率问题,比如你要更新文件的2,3,4行,用: for($i=2;$i<=4;$i++)$this->update($file,$i,$msg);不如这样的: $data=$this->read($file); for($i=2;$i<=4;$i++)$data[$i]=$msg; $this->write($file,$data);的 classtext{ var$key="<?die;?>";//数据保护 var$keylen;//数据保护长度 functiontext()//类的构造器 { $this->keylen=strlen($this->lkeylen);//得到保护长度 } functionread($file)//读文件,返回一个数组 { if(file_exists($file)): $data=@file($file); $data[0]=substr($data[0],$this->keylen); return$a; endif; } functionwrite($file,$data=array())//写文件,数据为一个数组 { if($fp=fopen($file,"w")): flock($fp,3); fwrite($fp,$this->key); foeach($dataas$v)fwrite($fp,$v); returntrue; endif; } functionadd($file,$msg) //向文件最后添加数据(不存在就先建立文件) { $a=$this->read($file);//这个不管文件存在不 $a[]=$msg; return$this->write($file,$a); } functioninsert($file,$n,$msg)//向第$n前插入数据 { if($a=$this->read($file)): $a[$n]=$msg.$a[$n]; returntrue; endif; } functionupdata($file,$msg)//更新$n数据 { if($a=$this->read($file)): $a[$n]=$msg; returntrue; endif } functiondel($file,$n)//删除$n数据 { return$this->updata($file,""); } }
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|