php中文本操作的类

yipeiwu_com6年前PHP代码库
给大家一个简单的文本操作的类 
我以前写的,不过一直都没机会用了,文本不如数据库
数据是以行保存的,以\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); 的


class text{
var $key="<?die;?>"; //数据保护
var $keylen; //数据保护长度
function text() //类的构造器
{
$this->keylen=strlen($this->lkeylen); //得到保护长度
}
function read($file) //读文件,返回一个数组
{
if(file_exists($file)):
$data=@file($file);
$data[0]=substr($data[0],$this->keylen);
return $a;
endif;
}
function write($file,$data=array()) //写文件,数据为一个数组
{
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) 
//向文件最后添加数据(不存在就先建立文件)
{
$a=$this->read($file); //这个不管文件存在不
$a[]=$msg;
return $this->write($file,$a);
}
function insert($file,$n,$msg) //向第$n前插入数据
{
if($a=$this->read($file)):
$a[$n]=$msg.$a[$n];
return true;
endif;
}
function updata($file,$n,$msg) //更新$n数据
{
if($a=$this->read($file)):
$a[$n]=$msg;
return true;
endif
}
function del($file,$n) //删除$n数据
{
return $this->updata($file,$n,"");
}
}

相关文章

curl 出现错误的调试方法(必看)

实例如下: private function httpGet($url) { $curl = curl_init(); curl_setopt($curl,...

php实现 data url的图片生成与保存 原创

Data URL是在本地直接绘制图片,不是从服务器加载,所以节省了HTTP连接,起到加速网页的作用。 语法: data:image/jpg;   ...

php解决crontab定时任务不能写入文件问题的方法分析

本文实例讲述了php解决crontab定时任务不能写入文件问题的方法。分享给大家供大家参考,具体如下: 今天使用crontab写的定时任务没有执行,很纳闷。 crontab.php...

PHP rawurlencode与urlencode函数的深入分析

问题:2个函数都是针对字符串转义使其适合做文件名。该用哪个?哪个更标准? 结论:rawurlencode遵守是94年国际标准备忘录RFC 1738,urlencode实现的是传统做法,和...

php连接微软MSSQL(sql server)完全攻略

php连接微软MSSQL(sql server)完全攻略

在研究ezSQL的时候就看到了mssql_connect()等一些php提供的连接MSSQL的函数,本以为php这个开源的风靡世界的编程语言对连接微软的数据应该是不在话下的,但是到真正执...