Ahalya Hegde
Ahalya Hegde

Reputation: 1641

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?

Upvotes: 0

Views: 59

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167192

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. :)

Upvotes: 2

Related Questions