最新下载
热门教程
- 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); |
继续浏览有关 的文章
相关文章
- 剑与远征格温妮丝赏金试炼攻略大全 04-24
- 物华弥新商店物品购买推荐 物华弥新商店买什么物品好 04-24
- 光遇4.24每日任务怎么做 光遇4月24日每日任务做法攻略 04-24
- 光遇4.24红石碎片在哪里 光遇4月24日红石碎片位置攻略 04-24
- 光遇4.24大蜡烛在哪里 光遇4月24日大蜡烛位置攻略 04-24
- 光遇4.24免费魔法有什么 光遇4月24日免费魔法收集攻略 04-24