xml 通过正则抓取字段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
$str = '<xml>
    <appid><![CDATA[wxd49ea66070209a6e]]></appid>
    <bank_type><![CDATA[CFT]]></bank_type>
    <cash_fee><![CDATA[1]]></cash_fee>
    <fee_type><![CDATA[CNY]]></fee_type>
    <is_subscribe><![CDATA[Y]]></is_subscribe>
    <mch_id><![CDATA[1497568502]]></mch_id>
    <nonce_str><![CDATA[nwuqfmxz4gep7b2fk6svun03p12l04rb]]></nonce_str>
    <openid><![CDATA[oS_Kl1UfWDOyZ37Aov2cJfg4dogc]]></openid>
    <out_trade_no><![CDATA[20180130195025526]]></out_trade_no>
    <result_code><![CDATA[SUCCESS]]></result_code>
    <return_code><![CDATA[SUCCESS]]></return_code>
    <sign><![CDATA[9FFE299F266420606B6FA25ED2E3ABE1]]></sign>
    <time_end><![CDATA[20180130195102]]></time_end>
    <total_fee>1</total_fee>
    <trade_type><![CDATA[NATIVE]]></trade_type>
    <transaction_id><![CDATA[4200000071201801303667079420]]></transaction_id>
</xml>';
        $arr = array();
        $xmlTag = array(
            'appid','bank_type','cash_fee','fee_type','is_subscribe','mch_id',
            'nonce_str','openid','out_trade_no','result_code','return_code','sign',
            'time_end','total_fee','trade_type','transaction_id'
        );
        foreach($xmlTag as $x){
            //echo $x;
            preg_match_all("/<".$x.">.*<\/".$x.">/",$str,$temp);
                $arr[$x] = $temp[0][0];
        }
 
        //去除XML标签并组装数据
        dump($arr);
        $data = array();
        foreach($arr as $key => &$value) {
            $temp_a = explode('<'.$key.'>'.'<![CDATA[', $value);
            $str_len = strlen("]]</".$key.">\"");//  该字符串长度;
            $v = substr($temp_a[1],0,-$str_len);
            $value = $v;
//            dump($v);
        }
        dump($arr);
        echo $arr['appid'];
        exit;<br><br><br>-----------------------------------------------------------------------------------------array(16) {
  ["appid"] => string(45) "<appid><![CDATA[wxd49ea66070209a6e]]></appid>"
  ["bank_type"] => string(38) "<bank_type><![CDATA[CFT]]></bank_type>"
  ["cash_fee"] => string(34) "<cash_fee><![CDATA[1]]></cash_fee>"
  ["fee_type"] => string(36) "<fee_type><![CDATA[CNY]]></fee_type>"
  ["is_subscribe"] => string(42) "<is_subscribe><![CDATA[Y]]></is_subscribe>"
  ["mch_id"] => string(39) "<mch_id><![CDATA[1497568502]]></mch_id>"
  ["nonce_str"] => string(67) "<nonce_str><![CDATA[nwuqfmxz4gep7b2fk6svun03p12l04rb]]></nonce_str>"
  ["openid"] => string(57) "<openid><![CDATA[oS_Kl1UfWDOyZ37Aov2cJfg4dogc]]></openid>"
  ["out_trade_no"] => string(58) "<out_trade_no><![CDATA[20180130195025526]]></out_trade_no>"
  ["result_code"] => string(46) "<result_code><![CDATA[SUCCESS]]></result_code>"
  ["return_code"] => string(46) "<return_code><![CDATA[SUCCESS]]></return_code>"
  ["sign"] => string(57) "<sign><![CDATA[9FFE299F266420606B6FA25ED2E3ABE1]]></sign>"
  ["time_end"] => string(47) "<time_end><![CDATA[20180130195102]]></time_end>"
  ["total_fee"] => string(24) "<total_fee>1</total_fee>"
  ["trade_type"] => string(43) "<trade_type><![CDATA[NATIVE]]></trade_type>"
  ["transaction_id"] => string(73) "<transaction_id><![CDATA[4200000071201801303667079420]]></transaction_id>"
}
array(16) {
  ["appid"] => string(18) "wxd49ea66070209a6e"
  ["bank_type"] => string(3) "CFT"
  ["cash_fee"] => string(1) "1"
  ["fee_type"] => string(3) "CNY"
  ["is_subscribe"] => string(1) "Y"
  ["mch_id"] => string(10) "1497568502"
  ["nonce_str"] => string(32) "nwuqfmxz4gep7b2fk6svun03p12l04rb"
  ["openid"] => string(28) "oS_Kl1UfWDOyZ37Aov2cJfg4dogc"
  ["out_trade_no"] => string(17) "20180130195025526"
  ["result_code"] => string(7) "SUCCESS"
  ["return_code"] => string(7) "SUCCESS"
  ["sign"] => string(32) "9FFE299F266420606B6FA25ED2E3ABE1"
  ["time_end"] => string(14) "20180130195102"
  ["total_fee"] => bool(false)
  ["trade_type"] => string(6) "NATIVE"
  ["transaction_id"] => &string(28) "4200000071201801303667079420"
}
wxd49ea66070209a6e

  

posted @   盘思动  阅读(1507)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示