xml 通过正则抓取字段

$str = '
	
	
	
	
	
	
	
	
	
	
	
	
	
	1
	
	
';
        $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.'>'.'\"");//  该字符串长度;
            $v = substr($temp_a[1],0,-$str_len);
            $value = $v;
//            dump($v);
        }
        dump($arr);
        echo $arr['appid'];
        exit;


-----------------------------------------------------------------------------------------
array(16) {
  ["appid"] => string(45) ""
  ["bank_type"] => string(38) ""
  ["cash_fee"] => string(34) ""
  ["fee_type"] => string(36) ""
  ["is_subscribe"] => string(42) ""
  ["mch_id"] => string(39) ""
  ["nonce_str"] => string(67) ""
  ["openid"] => string(57) ""
  ["out_trade_no"] => string(58) ""
  ["result_code"] => string(46) ""
  ["return_code"] => string(46) ""
  ["sign"] => string(57) ""
  ["time_end"] => string(47) ""
  ["total_fee"] => string(24) "1"
  ["trade_type"] => string(43) ""
  ["transaction_id"] => string(73) ""
}
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

  

转载于:https://www.cnblogs.com/pansidong/p/8390800.html

你可能感兴趣的:(xml 通过正则抓取字段)