最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php利用正则表达式读取不规范的xml文档
时间:2022-06-25 02:40:15 编辑:袖梨 来源:一聚教程网
如果在你的程序中收到这样的字符串:
| 代码如下 | 复制代码 |
那么,恭喜你,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); |
|
继续浏览有关 的文章
相关文章
- jm天堂网页版官方登录入口-jm天堂网页版直接登录入口 12-14
- 蝉妈妈网页版直达入口-蝉妈妈app官方正版入口在哪 12-14
- 豆包AI智能在线网页解析神器-豆包AI智能在线会议纪要生成助手 12-14
- 小红书Web官网登录入口-小红书官方网页版一键登录 12-14
- 苍云阅读app如何快速找到目录-目录入口位置 12-14
- 苹果ID登录官网入口 - 苹果Apple ID账户登录页面一键直达 12-14