Last active
August 3, 2019 08:52
-
-
Save Achterstraat/ac4a264c3e461e7179ba00510ef5dc13 to your computer and use it in GitHub Desktop.
Improved fetch_parsed_body() function that convert the results in an ordered array/object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fetch_parsed_body($object = false) | |
$result = json_decode($data, true); | |
if(is_null($result)) | |
{ | |
$result = []; | |
$lines = urldecode($this->result_body); | |
foreach(array_filter(explode("\n", $lines), 'strlen') as $line) | |
{ | |
$key = (preg_match('~^([A-Z]{1,5})\=~', $line) ? strtok($line, '=') : false); | |
if(empty($key)) | |
{ | |
parse_str($line, $result); | |
$result = (empty($result['list']) ? $result : $result['list']); | |
} | |
else | |
{ | |
$line = substr(trim($line), strlen($key)+1); | |
if(strlen($line) > 0) | |
{ | |
preg_match_all('~([A-z0-9\.-_]+)=(?<!=["])([^&]+)(?<!=["])~', $line, $parts); | |
if(isset($parts[1], $parts[2]) && !empty($parts[1]) && !empty($parts[2])) | |
{ | |
$line = array_combine($parts[1], $parts[2]); | |
} | |
$result[$key] = $line; | |
} | |
} | |
} | |
} | |
return (empty($result) ? null : ($object ? (object)$result : (array)$result)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment