如何使用PHP 5.1.6解析XML样式HTTP Post返回变量或数组

时间:2011-10-18 20:30:18

标签: php xml

我有三个可能的HTTP Post返回,我试图用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对我来说仍然有些困惑) 到目前为止,我一直在转动轮子。

谢谢!

3 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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. ) 

如果答案确实解决了您的问题,请在左侧增加计数器。

答案 2 :(得分:0)

我将hakre的解决方案与if / else结合使用来提取所需的输出。我确实遗漏了完整的3变量数组,但它可以轻松添加...我决定我不需要额外的空变量。我确定有一个更优雅的方法,但这是迄今为止我提出的最好的方法。感谢hakre&amp; 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']==''{


?>