2013-01-09 30 views
-1

我正在尝试为server.properties文件创建某种图形用户界面,这是Minecraft的,文件是这样布局的;解析.properties文件到数组中

level-name: world 
server-ip: 123.123.123 

该文件还可以有这样的东西## properties.file等等等等,单线条,这可能会增加混乱

所以基本上我需要拆分出来为可读的格式

的方式
+2

可能重复[PHP是否允许\ *属性文件中的Java?](http://stackoverflow.com/questions/5900077/does-php -ALLOW的属性-file-as-in-java) – hakre

+0

想知道在这里没有资格作为要求的理由。这里有一个空间,想知道和问这里可以这么说,它通常被描述为“家庭作业”,请参阅http://stackoverflow.com/questions/how-to-ask – hakre

+0

试过了什么? – Alfabravo

回答

1

像这样的东西应该为你工作:

$file_path = '/some/path/to/properties/file.properties'; 
$lines = explode("\n", trim(file_get_contents($file_path))); 
$properties = array(); 
foreach ($lines as $line) { 
    $line = trim($line); 

    if (!$line || substr($line, 0, 1) == '#') // skip empty lines and comments 
     continue; 

    if (false !== ($pos = strpos($line, ':'))) { 
     $properties[trim(substr($line, 0, $pos))] = trim(substr($line, $pos + 1)); 
    } 
} 
print_r($properties); 
// -> Array 
// -> (
// ->  [level-name] => world 
// ->  [server-ip] => 123.123.123 
// ->) 
+0

感谢朋友,这正是我需要的 –

+0

'$ lines = file($ file_path,FILE_IGNORE_NEW_LINES);'更简单。 –