Board logo

标题: php 判断字符串是不是xml格式 [打印本页]

作者: xiexie    时间: 2012-1-19 23:45     标题: php 判断字符串是不是xml格式

/*---------------------------------
 xml数据处理文件
 author:fangzhen 3.28
----------------------------------*/

function xml_to_array($source, $type=0){
      $xmlParser = xml_parser_create();
      if(!xml_parse($xmlParser,$source,true)){  //判断xml格式的正确性
             xml_parser_free($xmlParser);
             return false;
       }
      //$type 0为字符串,1为文件
      if($type==1){
             $xmlStr = simplexml_load_file($source);
      }else{
             $xmlStr = simplexml_load_string($source);
      }
      $arrStr = array();
      $source = serialize($xmlStr);
      $source = str_replace('O:16:"SimpleXMLElement"', 'a', $source);
      $arrStr = unserialize($source);
      return $arrStr;
}

[ 本帖最后由 xiexie 于 2012-1-19 23:48 编辑 ]




欢迎光临 PHP开发笔记 (http://www.phpvi.com/) Powered by Discuz! 6.1.0