一个XML操作类(正则版)[转]

上一篇 / 下一篇  2007-12-20 15:05:28 / 个人分类:PHP开发

PHPChina 开源社区门户t&wj3J~k&c

<?phpPHPChina 开源社区门户9QyIs2k;e/c+q'WS1d!M
/*
Z+p$]4Rn,r0*日期:2007-11-14PHPChina 开源社区门户#rk4N,II"^ ~E
*功能:操作XML文件(增、删、改、查)PHPChina 开源社区门户+G"e f,A9R
*版本:正则版
`1Z zLq2i 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#U lR'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);
|)d W"qG0  rsort($matches[1]);PHPChina 开源社区门户mS-c6jwW!J
  $newkey=$matches[1][0]+1;
H9IIkj8H4D0 PHPChina 开源社区门户 p3a HxaAs2f
  $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#}`Q3JQ"{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`)b RL"f"o Dq1i
 }
4b2C3]z\mM0m0 /*
0iB&}!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%K lzQ0  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;iER
   $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!n Y0   $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 aI YL
    $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&Z cb0   $bool_repeat=preg_match('/(\d),\1/',join(',',$keys),$wb)==0 ? true : false;
jy.`3wk~ c0   if(!($bool_statement && $bool_root && $bool_record && $bool_repeat))
L\v]o0   {
hrY[A$d?0    echo '文件规则已被破坏';PHPChina 开源社区门户1~%t&a G%li!A"S
    exit;PHPChina 开源社区门户Y2?8`6^)d [`:fM
   }PHPChina 开源社区门户P&K4e*If9RH
  }PHPChina 开源社区门户$E"U Thy\_
 }PHPChina 开源社区门户w1@ qi(H}E
 /*
'@Pc x qW0 *函数名:getRecordById
naViU2]W0 *说明:根据主键ID,获取字段信息PHPChina 开源社区门户 z XC%j!j
 */PHPChina 开源社区门户Ttu3E U![+QYv\$B
 public function getRecordById($id)PHPChina 开源社区门户u(N:g,H_6P|
 {
.T+a%[P7@8k$V0  $content=$this->getFileContent();
vb'`7v?_c0  preg_match('/<record>(\s*<key>'.$id.'<\/key>.+?)<\/record>/s',$content,$matches);PHPChina 开源社区门户)M'H$A r8x
  preg_match_all('|(<.+</.+>)|',$matches[1],$arr);PHPChina 开源社区门户M @+Uy4i:v1K`
  $arrstr='';PHPChina 开源社区门户*I5mqY m:|)qc*B
  for($i=0;$i<count($arr[1]);$i++)PHPChina 开源社区门户'ylrPt,R
  {PHPChina 开源社区门户/@ ~y7f} UxI e
   preg_match('/^.+>(.+)<\/(.+)>/',$arr[1][$i],$farr);PHPChina 开源社区门户 V+vL/C&_.C!h^
   $arrstr.="'$farr[2]'=>'$farr[1]'";PHPChina 开源社区门户 fc+{yYj1}xr7oSQ
   if($i<count($arr[1])-1) $arrstr.=',';
v1N sc(E4h/o8aiC^0  }
e9_ OR)fI.W*Gq)l0@e0  eval("\$row=array($arrstr);");
Y$Y X:hH2Pa0  return $row;PHPChina 开源社区门户?j/n8F4[*S%` o+q
 }
Zu4HS0X&XB6K g0 /*PHPChina 开源社区门户 E ]-OF'V
 *函数名:checkFilePHPChina 开源社区门户.T3|!z'dI'{
 *说明:根据主键ID,更新字段
{$w:MW%CR,P!`3qw0 */
'i`_%Y(j~0 public function updateRecordById($id,$form_arr=array())
AI+~1T(I6yY0 {PHPChina 开源社区门户 zFY i7vx5b
  $content=$this->getFileContent();
7bU0E5f.A4U0  foreach($form_arr as $k=>$v)PHPChina 开源社区门户7xM"_g#V
  {
`/l"v2GW.PY0   $re='/(<key>'.$id.'<\/key>[\s\S]*?<'.$k.'>).+?(<\/'.$k.'>)/s';
^ Y'S^7o0Pz @}0   $content=preg_replace($re,'${1}'.$v.'$2',$content);
dM6]5K*L${0I5[Q.F0  }PHPChina 开源社区门户Ta6e5H _2~eK
  $this->save($content);PHPChina 开源社区门户)B`'G GO?J,Ace,f7N
  return true;PHPChina 开源社区门户;_qAg ];B:X
 }
+gL E5B:x]2z0 public function deleteRecordById($id)PHPChina 开源社区门户 y6eT#F ?L3~
 {PHPChina 开源社区门户+z ^f Vu`-W
  $content=$this->getFileContent();
n^M I.y%~9R$\0  $content=preg_replace('/<record>\s*<key>'.$id.'+.+?<\/record>(\s{4})?/s','',$content);
A6aO9H$Y0  $this->save($content);PHPChina 开源社区门户]3U$z P ?:x.oz
 }
!Z.q ]+df4b;mYf%z0 /*PHPChina 开源社区门户d-NV6D5E:e%|)x
 *函数名:getListPHPChina 开源社区门户,a7ovO}3Bp[
 *说明:获取所有记录,返回一个二维数组PHPChina 开源社区门户&y,k/n_"x4U
 *     先获取所有record记录,再获取字段名和值PHPChina 开源社区门户3C%b[T4J'r8^5g,Q8d
 */PHPChina 开源社区门户?9y|kGl6V
 public function getList()PHPChina 开源社区门户]z'Zj*q
 {PHPChina 开源社区门户!X&FiE P
  $content=$this->getFileContent();
M4gvc7V E:u0  $rs=array();PHPChina 开源社区门户J+zP\)po
  preg_match_all('|<record>(.+?)<\/record>|s',$content,$matches);PHPChina 开源社区门户w,W.f w#M#c T-o*y
  for($i=0;$i<count($matches[1]);$i++)
b:AWXkl/_k)d9h0  {PHPChina 开源社区门户:yL.YC E*w:z.P
   preg_match_all('|(<.+</.+>)|',$matches[1][$i],$tmparr);
+{'i7gL W7H_/X3OL0   $arrstr='';
~H2JG![0   for($j=0;$j<count($tmparr[1]);$j++)PHPChina 开源社区门户La*P&z [0Q
   {
,T+@-j'\8P/Rxq0    preg_match('/^.+>(.+)<\/(.+)>/',$tmparr[1][$j],$farr);PHPChina 开源社区门户z$T Q)mN5ev
    $arrstr.="'$farr[2]'=>'$farr[1]'";PHPChina 开源社区门户'J1B}%{POH
    if($j<count($tmparr[1])-1) $arrstr.=',';PHPChina 开源社区门户M1@BHX
   }
F)O}.Mj0   eval("\$rs[]=array($arrstr);");PHPChina 开源社区门户 g9UNEY2xp/K:o
  }PHPChina 开源社区门户el|,iJc
  return $rs;PHPChina 开源社区门户%V0~:s5Hi0n&kc
 }PHPChina 开源社区门户'od1[(Qol g9U
 private function getFileContent()
E3^8}+b"A0 {
-Sp.G f~0  $hd=fopen($this->fpath,'r');
q1L#m0oc_*P0  $content=fread($hd,filesize($this->fpath));PHPChina 开源社区门户#l\ww9?o&b
  fclose($hd);
{.U&K]M%~e0  return $content;
^0I,z{\+e0 }
#wUx4xv1q-~ T(hKN0 private function save($content)
!`!vbz)rh^0 {
;LRjNY0  $hd=fopen($this->fpath,'w');PHPChina 开源社区门户5M"u0^ty@`
  fwrite($hd,$content);PHPChina 开源社区门户s [?9V7I"spoH
  fclose($hd);PHPChina 开源社区门户N4b?c%WExO!}
 }PHPChina 开源社区门户-bz#i0OH
}
'N0Ip0n H0a(x.`*pCR0?>PHPChina 开源社区门户@f(Y$FH"AMP {
PHPChina 开源社区门户E1FM*A d s(f

(|9}v$e9[7B.]0 PHPChina 开源社区门户vv0N.[:Ul

7MB?A/q-w6\iM0应用示例:
t!tv1_6Rn%a E ^zR0header('content-type: text/html; charset=utf-8');PHPChina 开源社区门户{(dlfi"uma
include('./libs/x.class.php');
[mwnc0$rootname='ROOT';//根标签名
'Q&z[|3b w d0S*rt8U0$fpath='./libs/ooooooooooooooo.xml';//文件的路径,不用手动创建文件PHPChina 开源社区门户G2j PuQ X[
$x=new OpXML('ROOT',$fpath);PHPChina 开源社区门户 U/P8X_9}io T
//增PHPChina 开源社区门户 U A9x6YJ
$arr=array('username'=>'pangmen','sex'=>'男','tel'=>'139999999','address'=>'北京');
w:@9f2T6m"Pt5G!n u0$x->insert($arr);
sDj+JKm8q0//删PHPChina 开源社区门户gd8S&EO.|F4K3rc
//$x->deleteRecordById(1);PHPChina 开源社区门户RG H:Xd@&j
//改PHPChina 开源社区门户0M1J%v? pr
//$arr=array('username'=>'pangmen','sex'=>'男','tel'=>'139999999','address'=>'北京');
!\V F uK*~v`1V;L0//$x->updateRecordById(1,$arr);
{p$bEQM.wK0zV0//查(一条记录)
_+y hb ``b0//print_r($x->getRecordById(1));
Z zz(d6EO0//查(所有记录)PHPChina 开源社区门户`ku'sn+l!]P
//print_r($x->getList());

4Qk"A4|)t4Y0

TAG: XML 正则 PHP开发

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

我的存档

数据统计

  • 访问量: 200
  • 日志数: 3
  • 建立时间: 2007-12-20
  • 更新时间: 2007-12-20

RSS订阅

Open Toolbar