php文本教学,php中文本操作的类
給大家一個簡單的文本操作的類。
我以前寫的,不過一直都沒機(jī)會用了,文本不如數(shù)據(jù)庫。數(shù)據(jù)是以行保存的,以\n結(jié)尾,注意你輸入的數(shù)據(jù)必須以"\n"結(jié)尾的,這是幾個最基本的類成員,文本的特別要注意效率問題,比如你要更新文件的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); 的
復(fù)制代碼 代碼如下:
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) //讀文件,返回一個數(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ù)為一個數(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); //這個不管文件存在不
$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,"");
}
}
總結(jié)
以上是生活随笔為你收集整理的php文本教学,php中文本操作的类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 多线程 start,Java
- 下一篇: matlab基数排序,c语言 数据结构