$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;
-----------------------------------------------------------------------------------------
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