一个XML操作类(正则版)[转]
上一篇 / 下一篇 2007-12-20 15:05:28 / 个人分类:PHP开发
<?phpPHPChina 开源社区门户9QyIs2k;e/c+q'WS1d!M
/*
Z+p$]4Rn,r0*日期:2007-11-14PHPChina 开源社区门户#rk4N,II"^~E
*功能:操作XML文件(增、删、改、查)PHPChina 开源社区门户+G"e
f,A9R
*版本:正则版
`1ZzLq2i
s%X0*/PHPChina 开源社区门户1_c$T"s x0E`&[
class OpXML
A og)u6D%O0{
/_l;a.f4UB,o3{0 private $fpath;
ri
m)xS}0 private $enter;PHPChina 开源社区门户\uXhJ.\
}
private $root;
t\&Q*BUy0 function __construct($root,$fpath)PHPChina 开源社区门户;kTh1Q3nX;Eu
{PHPChina 开源社区门户)G6x;G!a s%N?t0D
$this->fpath=$fpath;
2O-ny
v#UlR'K0 $this->root=$root;PHPChina 开源社区门户];A2a9bf
r
$this->enter=chr(13).chr(10);
*`"~M W
y_0 $this->checkFile();
l:KbBK
v6T4?@0 }
3Is#qH4`i2IR+np2A0 /*PHPChina 开源社区门户rzO,w,tR5y8[
L$T0_
*函数名:insert
-lj2s4N1U0Q[
q0 *说明:插入一条记录PHPChina 开源社区门户)AJ2Taz
*/
h\JRwY$z!?W0 public function insert($fields)PHPChina 开源社区门户6h-W9V5{q
{PHPChina 开源社区门户.k3u+_$}N
$content=$this->getFileContent();
y7Y_4]J5a0 preg_match_all('|<key>(\d+?)<\/key>|',$content,$matches);
|)dW"qG0 rsort($matches[1]);PHPChina 开源社区门户mS-c6jwW!J
$newkey=$matches[1][0]+1;
H9IIkj8H4D0 PHPChina 开源社区门户p3aHxaAs2f
$record='<record>'.$this->enter;
(n@8w*y
kT9A8b7?0 $record.='<key>'.$newkey.'</key>'.$this->enter;
5J5_;Cw#\ i8fH
L0 foreach($fields as $k=>$v)PHPChina 开源社区门户3D|%emc[/E)V
{
I&w.V5}4?4E4T+}0 $record.="<$k>$v</$k>".$this->enter;PHPChina 开源社区门户f5LG!YF$I&ORP
}PHPChina 开源社区门户a3t#}`Q3J Q"{7X
$record.='</record>'.$this->enter.$this->enter;PHPChina 开源社区门户 `~%O:y8?/~)gO.k vS
$this->save(preg_replace('/(?=<\/'.$this->root.'>)/',$record,$content));PHPChina 开源社区门户5D8F^WxfQ
return true;PHPChina 开源社区门户
H*o`)bRL"f"oDq1i
}
4b2C3]z\mM0m0 /*
0i B&}!X/F-z0 *函数名:checkFile
#@\KkuA0 *说明:如果文件不存在,则创建之,并初始化PHPChina 开源社区门户q3vA#f9~P
* 否则检查文件规则是否被破坏PHPChina 开源社区门户l)k%J8\k| w
*/PHPChina 开源社区门户.J zb*f
f*i
private function checkFile()
)O D2QU;j*oe0 {
.d%KlzQ0 if(!file_exists($this->fpath))
.t&[p6B vO|#I0 {PHPChina 开源社区门户Ae:eA
j4o@
$xmlstr='';PHPChina 开源社区门户`cLz3b,]
$xmlstr='<?xml version="1.0" encoding="UTF-8"?>'.$this->enter;PHPChina 开源社区门户H"D$dDt
$xmlstr.='<'.$this->root.'>'.$this->enter.$this->enter;PHPChina 开源社区门户KK2GM,v;i ER
$xmlstr.='</'.$this->root.'>';PHPChina 开源社区门户8Q9^ |7G\|/oF
$this->save($xmlstr);
iS@q1tVY0 }PHPChina 开源社区门户e#[#A]&W
elsePHPChina 开源社区门户+Rj*b!K'NB
{PHPChina 开源社区门户,whZe/G^
$content=$this->getFileContent();
[j_*{-t!nY0 $bool_statement=preg_match('/<\?xml version="1\.0".*?\?>/',$content)==0 ? false : true;
e7t@7K
Cmh0 $bool_root=preg_match('/<'.$this->root.'>.*<\/'.$this->root.'>/s',$content)==0 ? false : true;PHPChina 开源社区门户e s7hAj.c"jHa?
preg_match_all('|(<record>(?:.+?)<\/record>)|s',$content,$matches);PHPChina 开源社区门户`
Ggz2[$v;Q+n`
for($i=0;$i<count($matches[1]);$i++)PHPChina 开源社区门户(Me|^2OO(K.o
{PHPChina 开源社区门户 a-a$x*Bh
aIYL
$re='/^<record>\s*<key>(\d+)<\/key>\s*[\s\S]*\s*<\/record>$/';
(`0` Wz+GW#}0 $bool_record=preg_match($re,$matches[1][$i],$arr)==0 ? false : true;PHPChina 开源社区门户 n@.y*daP;O0A
$keys[]=$arr[1];
*~0zsr
Y3v0 if(!$bool_record) break;PHPChina 开源社区门户 `8`O9z8n)j*R7J
}PHPChina 开源社区门户,T Vv\;[&kN,BL^*D
rsort($keys);
{zSyEa&Zcb0 $bool_repeat=preg_match('/(\d),\1/',join(',',$keys),$wb)==0 ? true : false;