解析未命名的配对 PHP ini 文件


Parse un-named paired PHP ini file

>我有一个包含以下内容的.ini文件

SIGNALBOX/LOCATION
STATION
STATION CONCOURSE
STATION PLATFORM
STATION STEPS/STAIRS
TRACK
TUNNEL
WORKSHOP

我正在尝试使用parse_ini_file函数解析它,但它无法解析它。

我想避免在 ini 文件中的每个值之前locations[] =,有什么办法可以解决这个问题,只创建一个包含 ini 文件中所有值的数组?

PHP 函数 file() 会将文件读取到一个数组中,其中每一行都是一个元素。

请参阅:http://php.net/manual/en/function.file.php

例如:

<?php
    $locations = file('locations.ini');
    print_r($locations);
?>

此外,要去掉每个元素后面的换行符并忽略文件中的空行,你可以向函数添加标志(使用按位 OR 运算符添加多个),如下所示:

<?php
    $locations = file('locations.ini', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    print_r($locations);
?>

尝试 file_get_contents() 函数:

<?php
    $ini_array = file_get_contents("yourinifile.ini");
    $location_array = explode("'n", $ini_array );
    print_r($location_array);    
?>