2011-10-18 49 views
1

返回變量或數組我有三個可能的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在這一點上對我來說仍然有點困惑) 到目前爲止,我一直在旋轉我的車輪。

謝謝!

+0

嘗試SimpleXML。它基本上將XML樹變成了一個等效的PHP數據結構。 –

+0

由於$ var不是正確的XML格式,所以在嘗試SimpleXML時似乎錯誤。謝謝你的建議! – Rob

回答

1

當你有一個簡單的模式,你可以用sscanf解析它,並將它指定數組鍵:

$var = "<STATUS>SUCCESS</STATUS><BR><TIME>Mon Oct 17 20:44:41 PDT 2011</TIME>"; 

$r = sscanf($var, '<STATUS>%[^<]</STATUS><BR><TIME>%[^<]</TIME>', 
      $array['STATUS'], $array['TIME']); 

$array

Array 
(
    [STATUS] => SUCCESS 
    [TIME] => Mon Oct 17 20:44:41 PDT 2011 
) 

Demo/Demo (old)

0

我糾正正則表達式以涵蓋每種情況。你現在要做的就是創建一個case語句來填充數組的字段。對你的三個字符串的正則表達式的

$regex = "/<(STATUS)>(.*)<\/STATUS>(?:<\/?BR>)?(?:<(TIME)>(.*)<\/TIME>)?(?:<(VALIDATION MESSAGE)>(.*)<\/VALIDATION MESSAGE>)?/"; 

preg_match($regex, $var, $m); 


print_r($res) 

// Simply create an empty associative array with the fields you need,defaulting  everything to '', then write a switch statement based on the sizeof($m) and assign the fields. Each case produces a different count. 

結果...

Array ([0] => SUCCESS 
Mon Oct 17 20:44:41 PDT 2011 [1] => STATUS [2] => SUCCESS [3] => TIME [4] => Mon Oct 17 20:44:41 PDT 2011) 

Array ([0] => REJECTED [1] => STATUS [2] => REJECTED) 

Array ([0] => ERROR 
200-Service and Zipcode is required. [1] => STATUS [2] => ERROR [3] => [4] => [5] => VALIDATION MESSAGE [6] => 200-Service and Zipcode is required.) 

如果答案確實解決您的問題,請增加左邊的櫃檯。

0

我將hakre的解決方案與if/else一起使用來提取所需的輸出。我沒有留下完整的3變量數組,但它可以很容易地添加...我決定我不需要額外的空變量。我很確定有一種更優雅的方法,但這是迄今爲止我所想到的最好的方法。感謝hakre & Len幫我解決這個問題!這裏是代碼:

<? 
# NOTES: 
# This is just demo code to test if the code works. 
# The echo/print code is just to show me what's going on as the script processes 

    # SIMULATED (Manual) HTTP Post Response Variable 
    # uncomment each to test 
    $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>100-Invalid Username and Password.</VALIDATION MESSAGE>"; 
    // OR 
    #$var = "<STATUS>Unknown Value</STATUS><BR><WHO KNOWS>Uncaptured</WHO KNOWS>"; 

    $arrayPRE = array_combine 
    (
     array('STATUS'), 
     sscanf($var, '<STATUS>%[^<]</STATUS>') 
    ); 

    echo 'arrayPRE:<pre>'; 
    print_r($arrayPRE); 
    echo '</pre><hr>'; 

if ($arrayPRE['STATUS']=='SUCCESS'){ 

    $array = array_combine 
     (
      array('STATUS', 'TIME'), 
      sscanf($var, '<STATUS>%[^<]</STATUS><BR><TIME>%[^<]</TIME>') 
     ); 
    echo 'SUCCESS<pre>'; 
    print_r($array); 
    echo '</pre>'; 

}elseif($arrayPRE['STATUS']=='REJECTED'){ 

    $array = array_combine 
     (
      array('STATUS'), 
      sscanf($var, '<STATUS>%[^<]</STATUS>') 
     ); 
    echo 'REJECTED<pre>'; 
    print_r($array); 
    echo '</pre>'; 

}elseif($arrayPRE['STATUS']=='ERROR'){ 

    $array = array_combine 
     (
      array('STATUS', 'VALIDATION MESSAGE'), 
      sscanf($var, '<STATUS>%[^<]</STATUS><BR><VALIDATION MESSAGE>%[^<]</VALIDATION MESSAGE>') 
     ); 
    echo 'ERROR<pre>'; 
    print_r($array); 
    echo '</pre>'; 

}else{ 
    //unknown Response - ALERT ADMIN/SEND and RECORD $var 
    echo 'Unknown Response<br>'; 
    echo htmlspecialchars($var);// making the code readable in a browser 
}//end if ($arrayPRE['STATUS']==''{ 


?>