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

解析对php数组的纯文本响应,php,arrays,parsing,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 iQBRAwkBAAD6tlVkDEwBAV

我有以下格式的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中的任何键值

请帮帮我

我试过这个

函数parseDataarray&$line { $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
}
代码非常简单

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

把你的期望结果放多一点。还有你的努力?这是一个数组。我在问题中提到过,你在使用什么API?var_dump$API_response说什么?@Viral问题中的一个是API的响应。它是纯文本。@SarithaNair你看到我的答案了吗?