解析对php数组的纯文本响应

时间:2015-05-26 06:05:23

标签: php arrays parsing

我有以下格式的API响应

0000035901SM000000980000009800000125
0J0005              00064182
                    00000000
BEGIN
DATE=26.05.2015 09:01:48
SESSION=49500000031432619992
ERROR=0
RESULT=0
TRANSID=1000005699140

END
BEGIN SIGNATURE
iQBRAwkBAAD6tlVkDEwBAVOkAgCXHs1kj8u7E6tsdQLP8SFfkSDh9eYxkDYl/JdO
+2Lekurt2TfL68wxbCdaaWoT9Jy71luAvyAkgtNte/1FT22dsAHH
=GPcz
END SIGNATURE

我需要将BEGIN和END之间的文本解析为类似

的数组
array(
'DATE'=>'26.05.2015 09:01:48',...
)

解析它的最佳方法是什么。 BEGIN / END中的内容也可以改变。我想创建一个通用函数,以便可以解析BEGIN / END中的任何键值。

请帮帮我。

我试过这个

function parseData(array& $ lines)     {         $ result = array(); //这是存储当前块的地方

    // Process one line at a time...
    do {
        $l = trim(array_shift($lines));                // extract the first line into $l (and remove it from $lines)
        $p = str_getcsv($l, ' ', '"');                 // split $l into words (take care of strings eclosed in quotes)

        $key = array_shift($p);                        // get the first word from the line
        switch ($key)
        {
        default:                                       // simple "key value" lines
            $result[$key] = implode(' ', $p);          // combine the remaining words from $p into a single string
            break;

        case 'BEGIN':                                  // start a new block
            $key = array_shift($p);                    // the real key in the array follows the 'BEGIN' keyword
            if (end($p) == 'END') {                    // check if the entire block is on a single line
                array_pop($p);                         // remove 'END' from the end of the array
                if (count($p) == 1) {                  // only one word remaining in $p:
                     $result[$key] = array_shift($p);  //              it is the value
                } else {                               // more than one word in $p: this is a list of properties
                     $aa = array();                    // put the values of this one-line block here
                     while (count($p) > 1) {
                         $k = array_shift($p);         // they come in pairs: key followed by value
                         $v = array_shift($p);
                         $aa[$k] = $v;
                     }
                     $result[$key] = $aa;              // the entire line was parsed; store it
                }
            } else {                                   // the last word on the line is not 'END'
                $result[$key] = parseData($lines);     // recursively call this function to parse the inner block and store its result
            }                                          // it will return after it consumes the line that starts with 'END'
            break;

        case 'END':                                    // here ends the block
            return $result;                            // return the collected values
        }

    } while (count($lines));                           // ... until there are no more lines to process

    return $result;                                    // no more lines to process; return the collected data
}

1 个答案:

答案 0 :(得分:0)

代码非常简单

$lines = split("\n", $api);
while( ($l = trim(array_shift($lines))) != 'BEGIN' ); 
while( ($l = trim(array_shift($lines))) != 'END' )
  if ( preg_match('/^(\w+)=(.+)$/', $l, $matches) ) 
      ${$matches[1]}= $matches[2];