|
我以前寫的,不過一直都沒機(jī)會(huì)用了,文本不如數(shù)據(jù)庫
數(shù)據(jù)是以行保存的,以/n結(jié)尾,注意你輸入的數(shù)據(jù)必須以"/n"結(jié)尾的,這是幾個(gè)最基本的類成員,文本的特別要注意效率問題,比如你要更新文件的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); 的
class text{
var $key="<?die;?>"; //數(shù)據(jù)保護(hù)
var $keylen; //數(shù)據(jù)保護(hù)長度
function text() //類的構(gòu)造器
{
$this->keylen=strlen($this->lkeylen); //得到保護(hù)長度
}
function read($file) //讀文件,返回一個(gè)數(shù)組
{
if(file_exists($file)):
$data=@file($file);
$data[0]=substr($data[0],$this->keylen);
return $a;
endif;
}
function write($file,$data=array()) //寫文件,數(shù)據(jù)為一個(gè)數(shù)組
{
if($fp=fopen($file,"w")):
flock($fp,3);
fwrite($fp,$this->key);
foeach($data as $v)fwrite($fp,$v);
return true;
endif;
}
function add($file,$msg)
//向文件最后添加數(shù)據(jù)(不存在就先建立文件)
{
$a=$this->read($file); //這個(gè)不管文件存在不
$a[]=$msg;
return $this->write($file,$a);
}
function insert($file,$n,$msg) //向第$n前插入數(shù)據(jù)
{
if($a=$this->read($file)):
$a[$n]=$msg.$a[$n];
return true;
endif;
}
function updata($file,$n,$msg) //更新$n數(shù)據(jù)
{
if($a=$this->read($file)):
$a[$n]=$msg;
return true;
endif
}
function del($file,$n) //刪除$n數(shù)據(jù)
{
return $this->updata($file,$n,"");
}
}
php技術(shù):php中文本操作的類,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。