在Linux中,.DEB文件具有“控制”文本文件,其排列如下:
Name: Value
Size: Value
Information: Mutliline
value
将控制文件放入类似的PHP数组的最佳方法是什么:
Array ( "Name" => Value, "Size" => Value, "Information" => Value);
请记住,值可以是多行的,并包含“:”分隔符。
谢谢!
答案 0 :(得分:1)
$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);
按照此处所述实施:http://www.debian.org/doc/debian-policy/ch-controlfields.html
如果我犯了错误,欢迎改正!