一个xml问题 按你需要的XML格式输出就可以了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <?phpclass 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,'${1}'.$v.'$2',$content); } $this->save($content); return true; } public function deleteRecordById($id) { $content=$this->getFileContent(); $content=preg_replace('/<record>\s*<key>'.$id.'+.+?<\/record>(\s{4})?/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); }}?> php生成有图片的文章时,我想让文章把图片包住.怎么办? extention_dir 和 include_path是干什么的啊? 关于信息采集系统 高人请进! PHP 如何测试服务器是否支持运行exe文件? shell_exec 不行 smartemplate分块缓存问题,请唠叨做答 group by 问题 问一个菜鸟问题! 完美的网上商店系统 关于邮件函数 如何将BMP图象转换为Png格式? 同个页面有的会显示,有的不显示,高手帮我看一下什么问题。谢谢。 关于php中替换js和vbs的正则表达式
<?php
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,'${1}'.$v.'$2',$content);
}
$this->save($content);
return true;
} public function deleteRecordById($id)
{
$content=$this->getFileContent();
$content=preg_replace('/<record>\s*<key>'.$id.'+.+?<\/record>(\s{4})?/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);
}
}
?>