Here is a simple function I wrote to parse record-jar data. I need it in a php5.3 application to validate bcp47 language tags. It converts the language subtag registry among other things, into a nested php array. Any suggestions on how I can improve it?
/**
* Quick script to parse any record-jar data into an array.
*
* In particular, this is of use to someone trying to validate bcp47 language codes.
* @see http://tools.ietf.org/search/bcp47#section-3.1.1
*/
function parse_record_jar($record_jar_string) {
$records = array();
$record = array();
foreach(explode('%%', $record_jar_string) as $recordstr) {
array_splice($record, null); // truncate $record
foreach(array_filter(explode("\n", $recordstr)) as $line) {
$parts = array_map('trim', explode(":", $line, 2));
if (count($parts) === 2) {
list($key, $value) = $parts;
} else {
// $key retains its value from the previous iteration.
$value = $parts[0];
}
if (!isset($record[$key])) {
$record[$key] = '';
}
$record[$key] .= $value;
}
$records[] = $record;
}
return $records;
}