<?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+?)<\/key>|',$content,$matches); rsort($matches[1]); $newkey=$matches[1][0]+1; $record='<record>'.$this->enter; $record.='<key>'.$newkey.'</key>'.$this->enter; foreach($fields as $k=>$v) { $record.="<$k>$v</$k>".$this->enter; } $record.='</record>'.$this->enter.$this->enter; $this->save(preg_replace('/(?=<\/'.$this->root.'>)/',$record,$content)); return true; } /* *º¯ÊýÃû:insertM *˵Ã÷:²åÈë¶àÌõ¼Ç¼£¬²ÎÊýÀàÐͱØÐëÊǶþάÊý×é */ public function insertM($arr) { $content=$this->getFileContent(); preg_match_all('|<key>(\d+?)<\/key>|',$content,$matches); rsort($matches[1]); $newkey=$matches[1][0]+1; $record=''; for($i=0;$i<count($arr);$i++) { $record.='<record>'.$this->enter; $record.='<key>'.$newkey.'</key>'.$this->enter; foreach($arr[$i] as $k=>$v) { $record.="<$k>$v</$k>".$this->enter; } $record.='</record>'.$this->enter.$this->enter; $newkey++; } $this->save(preg_replace('/(?=<\/'.$this->root.'>)/',$record,$content)); return true; } /* *º¯ÊýÃû:checkFile *˵Ã÷:Èç¹ûÎļþ²»´æÔÚ£¬Ôò´´½¨Ö®£¬²¢³õʼ»¯ * ·ñÔò¼ì²éÎļþ¹æÔòÊÇ·ñ±»ÆÆ»µ */ private function checkFile() { if(!file_exists($this->fpath)) { $xmlstr=''; $xmlstr='<?xml version="1.0" encoding="UTF-8"?>'.$this->enter; $xmlstr.='<'.$this->root.'>'.$this->enter.$this->enter; $xmlstr.='</'.$this->root.'>'; $this->save($xmlstr); } else { $content=$this->getFileContent(); $bool_statement=preg_match('/<\?xml version="1\\.0".*?\?>/',$content)==0 ? false : true; $bool_root=preg_match('/<'.$this->root.'>.*<\/'.$this->root.'>/s',$content)==0 ? false : true; preg_match_all('|(<record>(?:.+?)<\/record>)|s',$content,$matches); for($i=0;$i<count($matches[1]);$i++) { $re='/^<record>\\s*<key>(\\d+)<\\/key>\\s*[\\s\\S]*\\s*<\\/record>$/'; $bool_record=preg_match($re,$matches[1][$i],$arr)==0 ? false : true; $keys[]=$arr[1]; if(!$bool_record) break; } rsort($keys); $bool_repeat=preg_match('/(\d),\1/',join(',',$keys),$wb)==0 ? true : false; if(!($bool_statement && $bool_root && $bool_record && $bool_repeat)) { echo 'Îļþ¹æÔòÒѱ»ÆÆ»µ'; exit; } } } /* *º¯ÊýÃû:getRecordById *˵Ã÷:¸ù¾ÝÖ÷¼üID£¬»ñÈ¡×Ö¶ÎÐÅÏ¢ */ public function getRecordById($id) { $content=$this->getFileContent(); preg_match('/<record>(\s*<key>'.$id.'<\/key>.+?)<\/record>/s',$content,$matches); preg_match_all('|(<.+</.+>)|',$matches[1],$arr); $arrstr=''; for($i=0;$i<count($arr[1]);$i++) { preg_match('/^.+>(.+)<\/(.+)>/',$arr[1][$i],$farr); $arrstr.="'$farr[2]'=>'$farr[1]'"; if($i<count($arr[1])-1) $arrstr.=','; } eval("\$row=array($arrstr);"); return $row; } /* *º¯ÊýÃû:checkFile *˵Ã÷:¸ù¾ÝÖ÷¼üID£¬¸üÐÂ×ֶΠ*/ public function updateRecordById($id,$form_arr=array()) { $content=$this->getFileContent(); foreach($form_arr as $k=>$v) { $re='/(<key>'.$id.'<\/key>[\s\S]*?<'.$k.'>).+?(<\/'.$k.'>)/s'; $content=preg_replace($re,'$'.$v.'$2',$content); } $this->save($content); return true; } public function deleteRecordById($id) { $content=$this->getFileContent(); $content=preg_replace('/<record>\s*<key>'.$id.'+.+?<\/record>(\s)?/s','',$content); $this->save($content); } /* *º¯ÊýÃû:getList *˵Ã÷:»ñÈ¡ËùÓмǼ£¬·µ»ØÒ»¸ö¶þάÊý×é * ÏÈ»ñÈ¡ËùÓÐrecord¼Ç¼£¬ÔÙ»ñÈ¡×Ö¶ÎÃûºÍÖµ */ public function getList() { $content=$this->getFileContent(); $rs=array(); preg_match_all('|<record>(.+?)<\/record>|s',$content,$matches); for($i=0;$i<count($matches[1]);$i++) { preg_match_all('|(<.+</.+>)|',$matches[1][$i],$tmparr); $arrstr=''; for($j=0;$j<count($tmparr[1]);$j++) { preg_match('/^.+>(.+)<\/(.+)>/',$tmparr[1][$j],$farr); $arrstr.="'$farr[2]'=>'$farr[1]'"; if($j<count($tmparr[1])-1) $arrstr.=','; } eval("\$rs[]=array($arrstr);"); } return $rs; } private function getFileContent() { $hd=fopen($this->fpath,'r'); $content=fread($hd,filesize($this->fpath)); fclose($hd); return $content; } private function save($content) { $hd=fopen($this->fpath,'w'); fwrite($hd,$content); fclose($hd); } } ?>
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ