一个XML操作类(正则版)[转]
上一篇 / 下一篇 2007-12-20 15:05:28 / 个人分类:PHP开发
i R5a8Z2ZJqw
I HE!u0<?phpPHPChina 开源社区门户V$p#cz'Tr{
/*PHPChina 开源社区门户`5nM.`W*O8F
*日期:2007-11-14
$j,y;Xj"MDLN0*功能:操作XML文件(增、删、改、查)
bm\M]cc:`0*版本:正则版
s_ai kYn ?0*/
jCpCF'V0class OpXML
6?[P9T]Q9}:PXW0{PHPChina 开源社区门户~He+u/@0i
private $fpath;PHPChina 开源社区门户;pA{g2zp+q1U
private $enter;PHPChina 开源社区门户EC'f&F/mqf
private $root;PHPChina 开源社区门户yiNjq@O/O
function __construct($root,$fpath)PHPChina 开源社区门户%m L{2IR?r
\Z
{
$^],F^+Z!S'A`"[Z0 $this->fpath=$fpath;PHPChina 开源社区门户Dso^(]jl
$this->root=$root;
.H-mvnN;z\k0 $this->enter=chr(13).chr(10);PHPChina 开源社区门户+c~$b1|
Nd'W ^
$this->checkFile();
#H/u3{~
oUD0 }
r'gpJPOf0 /*PHPChina 开源社区门户TBF9V+~?)A:a$c
*函数名:insertPHPChina 开源社区门户)T+t4gI9A
*说明:插入一条记录PHPChina 开源社区门户DR"F:X1vb$N%D
*/PHPChina 开源社区门户w3E|R`P
public function insert($fields)PHPChina 开源社区门户T%NmN&e/j8dlJ
{
;^)b%J,@ ^;w0 $content=$this->getFileContent();
?z Dx/Q+G0 preg_match_all('|<key>(\d+?)<\/key>|',$content,$matches);PHPChina 开源社区门户R#`c [*R:J{
OL
rsort($matches[1]);PHPChina 开源社区门户*}.W!O7X6f\Q
$newkey=$matches[1][0]+1;PHPChina 开源社区门户.S
BB
BJb
Iah0z mxD2T0 $record='<record>'.$this->enter;PHPChina 开源社区门户oa0`*Z2n
$record.='<key>'.$newkey.'</key>'.$this->enter;PHPChina 开源社区门户#p@7s$G I|r!R8c`
foreach($fields as $k=>$v)PHPChina 开源社区门户%D(n
p4r3R1\
{PHPChina 开源社区门户([2v!p.wvT0L
$record.="<$k>$v</$k>".$this->enter;PHPChina 开源社区门户;O[a3B;FC
}PHPChina 开源社区门户TSrV`e/i
$record.='</record>'.$this->enter.$this->enter;
8Y};U7U%]"m8Tq Y0 $this->save(preg_replace('/(?=<\/'.$this->root.'>)/',$record,$content));PHPChina 开源社区门户!G9S1B1@;hX
return true;PHPChina 开源社区门户"a\ GA8c ^-tJ
}
,y6ohCI5U*z0O0 /*
!B5x;h ]8_0 *函数名:checkFile
Deo5`C3}h/L0 *说明:如果文件不存在,则创建之,并初始化PHPChina 开源社区门户 Y$m~p-b5y
* 否则检查文件规则是否被破坏PHPChina 开源社区门户~Z&j"Q%UlK.t$N]
*/
+n5V:h'?Q V\4W-gu0 private function checkFile()
c4nC7V!]zO!YU0 {
|P[gN$y:Lv2I0 if(!file_exists($this->fpath))PHPChina 开源社区门户
[S3EhX%t2AM$t
{PHPChina 开源社区门户4j*Zi4Ke8d
$xmlstr='';
_YFODi_0 $xmlstr='<?xml version="1.0" encoding="UTF-8"?>'.$this->enter;
Q%hg~@+\0 $xmlstr.='<'.$this->root.'>'.$this->enter.$this->enter;PHPChina 开源社区门户-F$v%i:Fu5?|
$xmlstr.='</'.$this->root.'>';
h4K vM Q
B,l(S)uj0 $this->save($xmlstr);PHPChina 开源社区门户'xL$aq%K9m
}
#XV DU_2|TX0 else
/S ?#OS&}+`9y(\0 {PHPChina 开源社区门户&_*y r
~L-ZyOkzlq
$content=$this->getFileContent();
R4p])v(Zr0 $bool_statement=preg_match('/<\?xml version="1\.0".*?\?>/',$content)==0 ? false : true;
y7_!wn!gI
?0 $bool_root=preg_match('/<'.$this->root.'>.*<\/'.$this->root.'>/s',$content)==0 ? false : true;
p+R&u;Y,S/]
lq0 preg_match_all('|(<record>(?:.+?)<\/record>)|s',$content,$matches);
m:BGM#OH_0 for($i=0;$i<count($matches[1]);$i++)PHPChina 开源社区门户i;sa4xTQ @Gf8J
{PHPChina 开源社区门户P2E0Gc(s I
$re='/^<record>\s*<key>(\d+)<\/key>\s*[\s\S]*\s*<\/record>$/';PHPChina 开源社区门户,zJ'_Y[6y6B
$bool_record=preg_match($re,$matches[1][$i],$arr)==0 ? false : true;PHPChina 开源社区门户{{+nS[#J
z
T
$keys[]=$arr[1];PHPChina 开源社区门户1J5z@+T1_8sk9S
if(!$bool_record) break;
Pr8d3Gb k0 }PHPChina 开源社区门户BK~+A|I`X)@#V
rsort($keys);PHPChina 开源社区门户1c,lp6We
|oi1~8l|
$bool_repeat=preg_match('/(\d),\1/',join(',',$keys),$wb)==0 ? true : false;
feVT9A]M-aO9T0 if(!($bool_statement && $bool_root && $bool_record && $bool_repeat))PHPChina 开源社区门户!E/vpnMp5S0D
{
k9E,gNjKM0 echo '文件规则已被破坏';
rj\T8gF[C0 exit;PHPChina 开源社区门户q8R6@b|1@'j;o3bJ
}
OG~XJ&C0 }PHPChina 开源社区门户y @ ?%dD/Tz
}PHPChina 开源社区门户-i.td \t!Y9nR
/*PHPChina 开源社区门户(k&T(K}WQ2JAo
*函数名:getRecordById
nLj@ T
~0 *说明:根据主键ID,获取字段信息PHPChina 开源社区门户2Z/S'Pt;dL,t5zu;X&a
*/
/b+Uo
eD#F,H,d0 public function getRecordById($id)PHPChina 开源社区门户Mm5[lf
{
[p4nDE0 $content=$this->getFileContent();PHPChina 开源社区门户/E HhU
]NY
preg_match('/<record>(\s*<key>'.$id.'<\/key>.+?)<\/record>/s',$content,$matches);PHPChina 开源社区门户q}:_#\#f)a.T
preg_match_all('|(<.+</.+>)|',$matches[1],$arr);
8g_8a7\|0 $arrstr='';
{,d v!O/i5j0 for($i=0;$i<count($arr[1]);$i++)PHPChina 开源社区门户K`C5?
a1S\
{
pn#oZ6}j0 preg_match('/^.+>(.+)<\/(.+)>/',$arr[1][$i],$farr);
g tS:X._NSjO_-Y B0 $arrstr.="'$farr[2]'=>'$farr[1]'";PHPChina 开源社区门户 X D*K\G-gJ-ej"["g`
if($i<count($arr[1])-1) $arrstr.=',';
8n*Yg~:HI0 }PHPChina 开源社区门户"tP3t4T/rF
eval("\$row=array($arrstr);");PHPChina 开源社区门户0v
T:i2e
E
lI4{#@kL
return $row;PHPChina 开源社区门户-N*~8g"qc
}
/M$D^#@(^0 /*
e#hq@IUI!iI0 *函数名:checkFilePHPChina 开源社区门户,H7Q
sr9a,R9J8g,r%W
*说明:根据主键ID,更新字段PHPChina 开源社区门户7E9T;]/bmb.CR
*/
|.b&z;_$f8[7I _0 public function updateRecordById($id,$form_arr=array())PHPChina 开源社区门户&Qc|'UT1z7w
{PHPChina 开源社区门户2|Qvd*X-Y3L
$content=$this->getFileContent();PHPChina 开源社区门户-A+Iq!B8ix1b u
foreach($form_arr as $k=>$v)PHPChina 开源社区门户oO4njYU
{PHPChina 开源社区门户p?$s R i$rH_kPl8|
$re='/(<key>'.$id.'<\/key>[\s\S]*?<'.$k.'>).+?(<\/'.$k.'>)/s';
'S^$EjI"\ox9~9Y0 $content=preg_replace($re,'${1}'.$v.'$2',$content);
N4H:P/az:O$^M)[0 }
4|z:O2M,ay9NrU
?0 $this->save($content);PHPChina 开源社区门户d.`/tt8o
XX&r
return true;PHPChina 开源社区门户1fth!].t(v._o
}