最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php利用正则表达式读取不规范的xml文档
时间:2014-09-19 编辑:简简单单 来源:一聚教程网
如果在你的程序中收到这样的字符串:
代码如下 | 复制代码 |
那么,恭喜你,php中我们常用的几种方法
都不会生效,如:
代码如下 | 复制代码 |
$array = (array)new SimpleXMLElement($xml_str); |
所以我们只能自己写个方法喽
代码如下:
代码如下 | 复制代码 |
function parse_xml_to_array($xmlstr,$loopTag){ $args = explode(''.$loopTag.'>',$xmlstr); $returns = array(); if($args){ $reg = '/<(\w+)[^>]*>([\x00-\xFF]*)<\/\1>/'; foreach($args as $item){ $item = str_replace('<'.$loopTag.'>','',$item); if(preg_match_all($reg, $item, $matches)) { if(isset($matches[1]) && isset($matches[2])){ $returns[] = array_combine($matches[1],$matches[2]); } } } } unset($args); return $returns; } $arr = parse_xml_to_array($xml,'ReportList'); var_dump($arr); |
继续浏览有关 的文章
相关文章
- PHP导出数据超时的优化建议解读 10-31
- PHP之mysql位运算解析 10-31
- Laravel实现登录跳转功能解析 10-31
- php双向队列解读 10-31
- Laravel异常上下文解决教程 10-24
- php数组查询元素位置方法介绍 10-24