SunShine February 2016

parsing strings in string.ini using PHP

I have a strings.ini file where I am defining all the strings like

tz1 = "UTC+4:30 Asia/Kabul"
tz2 = "UTC+5:45 Asia/Katmandu"
tz3 = "UTC-4:30 America/Caracas"

I have a substring value 5:45. Based on that I want to get LHS part tz2. How to do this using PHP?

Answers


Praveen Kumar February 2016

Let's say, you have settings.ini file. You can use parse_ini_file() function to do this:

settings.ini

[base]
    tz1 = "UTC+4:30 Asia/Kabul"
    tz2 = "UTC+5:45 Asia/Katmandu"
    tz3 = "UTC-4:30 America/Caracas"

PHP File

// Parse without sections
$ini_array = parse_ini_file("settings.ini");
print_r($ini_array);

This would output:

Array
(
    [tz1] => "UTC+4:30 Asia/Kabul"
    [tz2] => "UTC+5:45 Asia/Katmandu"
    [tz3] => "UTC-4:30 America/Caracas"
)

Now you can get the value of tz2 and get the value as:

$ini = explode(" ", $ini_array["tz2"]);
$ini = str_replace("UTC", "", $ini);

So, your $ini value will be: +5:45.

You can also loop through it to get both the LHS and RHS. :)

Post Status

Asked in February 2016
Viewed 2,835 times
Voted 4
Answered 1 times

Search




Leave an answer