php中文本操作的类

yipeiwu_com5年前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,"");
}
}

相关文章

php中的四舍五入函数代码(floor函数、ceil函数、round与intval)

floor函数和ceil函数互相搭配起来可以使php 处理的数据更加真实可靠。 一、先来看floor函数: 语法: float floor ( float value ) 说明: 返回不...

WordPress中获取页面链接和标题的相关PHP函数用法解析

get_permalink()(获取文章或页面链接) get_permalink() 用来根据固定连接返回文章或者页面的链接。在获取链接时 get_permalink() 函数需要知道要...

用PHP实现维护文件代码

PHP有很多与文件系统相关的函数,不仅可以帮助你打开文件,还可以显示目录内容、移动文件等。很多人甚至使用PHP写出了基于Web的文件管理器。 首先需要提醒一些关于文件路径的东西:在Win...

PHP Global定义全局变量使用说明

很不习惯PHP中的变量作用域,PHP中函数变量和全局是完全隔绝的,也就是无法相互访问。比如下面这样:复制代码 代码如下:$test = 123; abc(); //这里什么都不...

php微信公众开发之获取周边酒店信息的方法

本文实例讲述了php微信公众开发之获取周边酒店信息的方法。分享给大家供大家参考。具体分析如下: 关注微信公众之后发送回复地理位置信息,即可回复周边附近的酒店信息列表,下面我就来给各位介绍...