php中文本操作的類
更新時(shí)間:2007年03月17日 00:00:00 作者:
給大家一個(gè)簡(jiǎn)單的文本操作的類
我以前寫的,不過(guò)一直都沒機(jī)會(huì)用了,文本不如數(shù)據(jù)庫(kù)
數(shù)據(jù)是以行保存的,以\n結(jié)尾,注意你輸入的數(shù)據(jù)必須以"\n"結(jié)尾的,這是幾個(gè)最基本的類成員,文本的特別要注意效率問(wè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); 的
class text{
var $key="<?die;?>"; //數(shù)據(jù)保護(hù)
var $keylen; //數(shù)據(jù)保護(hù)長(zhǎng)度
function text() //類的構(gòu)造器
{
$this->keylen=strlen($this->lkeylen); //得到保護(hù)長(zhǎng)度
}
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,"");
}
}
我以前寫的,不過(guò)一直都沒機(jī)會(huì)用了,文本不如數(shù)據(jù)庫(kù)
數(shù)據(jù)是以行保存的,以\n結(jié)尾,注意你輸入的數(shù)據(jù)必須以"\n"結(jié)尾的,這是幾個(gè)最基本的類成員,文本的特別要注意效率問(wè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); 的
class text{
var $key="<?die;?>"; //數(shù)據(jù)保護(hù)
var $keylen; //數(shù)據(jù)保護(hù)長(zhǎng)度
function text() //類的構(gòu)造器
{
$this->keylen=strlen($this->lkeylen); //得到保護(hù)長(zhǎng)度
}
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,"");
}
}
相關(guān)文章
PHP中sleep()函數(shù)的實(shí)用場(chǎng)景以及注意事項(xiàng)
sleep()函數(shù)是PHP中的一個(gè)休眠函數(shù),可以讓程序在指定的時(shí)間內(nèi)暫停執(zhí)行,以達(dá)到延遲執(zhí)行的效果,本文介紹使用sleep()函數(shù)的實(shí)用場(chǎng)景以及注意事項(xiàng)2023-09-09PHP的時(shí)間戳與具體時(shí)間轉(zhuǎn)化的簡(jiǎn)單實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇PHP的時(shí)間戳與具體時(shí)間轉(zhuǎn)化的簡(jiǎn)單實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06PHP socket 模擬POST 請(qǐng)求實(shí)例代碼
這篇文章主要介紹了 PHP 使用socket 模擬POST的方法,結(jié)合實(shí)例分析了sockets 模擬 POST 常用技巧,需要的朋友可以參考下2016-07-07