返回變量或數組我有三個可能的HTTP發佈返回,我試圖從PHP 5.1.6中獲取數組。 我不完全確定這個(對不起)的術語。如何解析XML樣式HTTP發佈返回到PHP 5.1.6
<?PHP
// Values the $var could be:
$var = "<STATUS>SUCCESS</STATUS><BR><TIME>Mon Oct 17 20:44:41 PDT 2011</TIME>";
// OR
$var = "<STATUS>REJECTED</STATUS>";
// OR
$var = "<STATUS>ERROR</STATUS></BR><VALIDATION MESSAGE>200-Service and Zipcode is required.</VALIDATION MESSAGE> ";
// Output I'd like to see:
$array = Array (
['STATUS'] => 'SUCCESS',
['TIME'] => 'Mon Oct 17 20:44:41 PDT 2011',
['VALIDATION MESSAGE'] => ''
);
// OR
$array = Array (
['STATUS'] => 'REJECTED',
['TIME'] => '',
['VALIDATION MESSAGE'] => ''
);
//OR
$array = Array (
['STATUS'] => 'ERROR',
['TIME'] => '',
['VALIDATION MESSAGE'] => '200-Service and Zipcode is required.'
);
//Another way to look at the desired output array would be:
/*
Array
(
[STATUS] => SUCCESS
[TIME] => Mon Oct 17 20:44:41 PDT 2011
[VALIDATION MESSAGE] =>
)
// OR
Array
(
[STATUS] => REJECTED
[TIME] =>
[VALIDATION MESSAGE] =>
)
// OR
Array
(
[STATUS] => ERROR
[TIME] => Mon Oct 17 20:44:41 PDT 2011
[VALIDATION MESSAGE] =>
)
*/
?>
也許preg_match或??? (preg_match在這一點上對我來說仍然有點困惑) 到目前爲止,我一直在旋轉我的車輪。
謝謝!
嘗試SimpleXML。它基本上將XML樹變成了一個等效的PHP數據結構。 –
由於$ var不是正確的XML格式,所以在嘗試SimpleXML時似乎錯誤。謝謝你的建議! – Rob