当前位置: 首页 > 图文教程 > 网络编程 > PHP > php中文本操作的类

PHP
一键删除顽固的空文件夹 软件下载
获得Google PR值的PHP代码
解决控件遮挡问题:关于有窗口元素和无窗口元素
Discuz!5的PHP代码高亮显示插件(黑暗中的舞者更新)
phpmyadmin中配置文件现在需要绝密的短语密码的解决方法
一个数据采集类
php中的一个中文字符串截取函数
自己前几天写的无限分类类
简单采集了yahoo的一些数据
回答PHPCHINA上的几个问题:URL映射
模拟flock实现文件锁定
一个捕获函数输出的函数
cache_lite试用
再次研究下cache_lite
PHP数据缓存技术
修改了一个很不错的验证码,共享之。
谈谈PHP的输入输出流
使用apache模块rewrite_module (转)
让PHP支持页面回退的两种方法[转]
mysql中存储过程、函数的一些问题

PHP 中的 php中文本操作的类


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 94 ::
收藏到网摘: n/a

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