Jack hardcastle
Jack hardcastle

Reputation: 2875

Parse un-named paired PHP ini file

I have a .ini file with the following contents

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

And I'm trying to parse this with parse_ini_file function, however it's unable to parse it.

I want to avoid locations[] = before each value in the ini file, is there any way around this, to just create an array containing all of the values in the ini file?

Upvotes: 2

Views: 55

Answers (2)

Thomas
Thomas

Reputation: 367

The PHP-function file() will read the file to an array where each line is an element.

See: http://php.net/manual/en/function.file.php

For example:

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

Additionally, to get rid of the newline-characters after each element and to ignore empty lines in the file, you may add flags (use the bitwise OR-operator to add multiple) to the function, like so:

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

Upvotes: 3

Nutshell
Nutshell

Reputation: 8537

Try file_get_contents() function :

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

Upvotes: 0

Related Questions