发一个XML操作类(正则版)

<?php /* *作者:pangmen *日期:2007-11-14 *功能:操作XML文件(增、删、改、查) *版本:正则版 *附:大家可以随意修改此类,合适的话发我一份:) * 我的E-mail:ad2008bobo@yahoo.com.cn */ class OpXML { private $fpath; private $enter; private $root; function __construct($root,$fpath) { $this->fpath=$fpath; $this->root=$root; $this->enter=chr(13).chr(10); $this->checkFile(); } /* *函数名:insert *说明:插入一条记录 */ public function insert($fields) { $content=$this->getFileContent(); preg_match_all('|<key>(/d+?)&lt;//key&gt;|',$content,$matches); rsort($matches[1]); $newkey=$matches[1][0]+1; $record='<record>'.$this-&gt;enter; $record.='<key>'.$newkey.'</key>'.$this-&gt;enter; foreach($fields as $k=&gt;$v) { $record.="&lt;$k&gt;$v$K&gt;".$this-&gt;enter; } $record.='</record>'.$this-&gt;enter.$this-&gt;enter; $this-&gt;save(preg_replace('/(?=&lt;//'.$this-&gt;root.'&gt;)/',$record,$content)); return true; } /* *函数名:insertM *说明:插入多条记录,参数类型必须是二维数组 */ public function insertM($arr) { $content=$this-&gt;getFileContent(); preg_match_all('|<key>(/d+?)&lt;//key&gt;|',$content,$matches); rsort($matches[1]); $newkey=$matches[1][0]+1; $record=''; for($i=0;$i<count .>" {&gt;enter; $record.='<key>'.$newkey.'</key>'.$this-&gt;enter; foreach($arr[$i] as $k=&gt;$v) { $record.="&lt;$k&gt;$v$K&gt;".$this-&gt;enter; } $record.=''.$this-&gt;enter.$this-&gt;enter; $newkey++; } $this-&gt;save(preg_replace('/(?=&lt;//'.$this-&gt;root.'&gt;)/',$record,$content)); return true; } /* *函数名:checkFile *说明:如果文件不存在,则创建之,并初始化 * 否则检查文件规则是否被破坏 */ private function checkFile() { if(!file_exists($this-&gt;fpath)) { $xmlstr=''; $xmlstr='<?xml version="1.0" encoding="UTF-8"?>'.$this-&gt;enter; $xmlstr.='&lt;'.$this-&gt;root.'&gt;'.$this-&gt;enter.$this-&gt;enter; $xmlstr.=''.$THIS-&gt;root.'&gt;'; $this-&gt;save($xmlstr); } else { $content=$this-&gt;getFileContent(); $bool_statement=preg_match('/&lt;/?xml version="1/.0".*?/?&gt;/',$content)==0 ? false : true; $bool_root=preg_match('/&lt;'.$this-&gt;root.'&gt;.*&lt;//'.$this-&gt;root.'&gt;/s',$content)==0 ? false : true; preg_match_all('|(<record>(?:.+?)&lt;//record&gt;)|s',$content,$matches); for($i=0;$i<count getrecordbyid function public exit echo if false : true rsort break>/s*<key>(/d+)/key&gt;/s*[/s/S]*/s*/record&gt;$/"&gt;getFileContent(); preg_match('/<record>(/s*<key>'.$id.'&lt;//key&gt;.+?)&lt;//record&gt;/s',$content,$matches); preg_match_all('|(&lt;.+)|',$matches[1],$arr); $arrstr=''; for($i=0;$i<count preg_match>(.+)&lt;//(.+)&gt;/',$arr[1][$i],$farr); $arrstr.="'$farr[2]'=&gt;'$farr[1]'"; if($i<count function public updaterecordbyid return eval>getFileContent(); foreach($form_arr as $k=&gt;$v) { $re='/(<key>'.$id.'&lt;//key&gt;[/s/S]*?&lt;'.$k.'&gt;).+?(&lt;//'.$k.'&gt;)/s'; $content=preg_replace($re,'${1}'.$v.'$2',$content); } $this-&gt;save($content); return true; } public function deleteRecordById($id) { $content=$this-&gt;getFileContent(); $content=preg_replace('/<record>/s*<key>'.$id.'+.+?&lt;//record&gt;(/s{4})?/s','',$content); $this-&gt;save($content); } /* *函数名:getList *说明:获取所有记录,返回一个二维数组 * 先获取所有record记录,再获取字段名和值 */ public function getList() { $content=$this-&gt;getFileContent(); $rs=array(); preg_match_all('|<record>(.+?)&lt;//record&gt;|s',$content,$matches); for($i=0;$i<count . preg_match_all>)|',$matches[1][$i],$tmparr); $arrstr=''; for($j=0;$j<count preg_match>(.+)&lt;//(.+)&gt;/',$tmparr[1][$j],$farr); $arrstr.="'$farr[2]'=&gt;'$farr[1]'"; if($j<count function return getfilecontent private eval>fpath,'r'); $content=fread($hd,filesize($this-&gt;fpath)); fclose($hd); return $content; } private function save($content) { $hd=fopen($this-&gt;fpath,'w'); fwrite($hd,$content); fclose($hd); } } ?&gt;<pre></pre></count></count></count></record></key></record></key></count></count></key></record></key></count></record></count></key></key>

使用例子

<?php header('content-type: text/html; charset=utf-8'); include('./libs/x.class.php'); $rootname='ROOT';//根标签名 $fpath='./libs/ooooooooooooooo.xml';//文件的路径,不用手动创建文件 $x=new OpXML('ROOT',$fpath); //增 $arr=array('username'=>'pangmen','sex'=>'男','tel'=>'139999999','address'=>'北京'); $x->insert($arr); //删 //$x->deleteRecordById(1); //改 //$arr=array('username'=>'pangmen','sex'=>'男','tel'=>'139999999','address'=>'北京'); //$x->updateRecordById(1,$arr); //查(一条记录) //print_r($x->getRecordById(1)); //查(所有记录) //print_r($x->getList()); ?>

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值