In Linux, .DEB files have ‘control’ text files that are arranged as follows:
Name: Value Size: Value Information: Mutliline value
What is the best way to get the control file into a PHP array resembling:
Array ( "Name" => Value, "Size" => Value, "Information" => Value);
keeping in mind that the values can be multi-line and include the “:” separator.
Thanks!
Advertisement
Answer
$source = fopen('path/to/file'); $index = ''; while( ($line = fgets($source)) !== false ){ if(preg_match('/^s*$/', $line)) continue 1; // ignore empty lines // if(!preg_match('/^s+/', $line)){ // if the line does not start with whitespace then it has a new key-value pair // $items = explode(':', $line, 2); // separate at the first : // $index = strtolower($items[0]); // the keys are case insensitive // $value = preg_replace('/^s+/', '', $items[1]); // remove extra whitespace from the begining // $value = preg_replace('/s+$/', '', $value); // and from the end // } else{ // continue the value from the previous line // $value = preg_replace('/s+$/', '', $line); // remove whitespace only from the end // } $data[$index] .= $value; } fclose($source);
Implemented as described here: http://www.debian.org/doc/debian-policy/ch-controlfields.html
If I made mistakes corrections are welcomed!