我正在嘗試爲server.properties文件創建某種圖形用戶界面,這是Minecraft的,文件是這樣佈局的;解析.properties文件到數組中
level-name: world
server-ip: 123.123.123
該文件還可以有這樣的東西## properties.file等等等等,單線條,這可能會增加混亂
所以基本上我需要拆分出來爲可讀的格式
的方式我正在嘗試爲server.properties文件創建某種圖形用戶界面,這是Minecraft的,文件是這樣佈局的;解析.properties文件到數組中
level-name: world
server-ip: 123.123.123
該文件還可以有這樣的東西## properties.file等等等等,單線條,這可能會增加混亂
所以基本上我需要拆分出來爲可讀的格式
的方式像這樣的東西應該爲你工作:
$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
// ->)
的
感謝朋友,這正是我需要的 –
'$ lines = file($ file_path,FILE_IGNORE_NEW_LINES);'更簡單。 –
可能重複[PHP是否允許\ *屬性文件中的Java?](http://stackoverflow.com/questions/5900077/does-php -ALLOW的屬性-file-as-in-java) – hakre
想知道在這裏沒有資格作爲要求的理由。這裏有一個空間,想知道和問這裏可以這麼說,它通常被描述爲「家庭作業」,請參閱http://stackoverflow.com/questions/how-to-ask – hakre
試過了什麼? – Alfabravo