0

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?

3
  • ^\w+(?=\s*=\s*"[^"]*\b5:45\b) Commented Feb 8, 2016 at 9:45
  • Is that an ini file? Commented Feb 8, 2016 at 9:46
  • @SunShine You already have a function called parse_ini_file() in PHP, which helps you do this. Commented Feb 8, 2016 at 9:47

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

8 Comments

Is there any limit for that ? I have some 5300 lines of strings in the ini file and I am not getting all the strings in the array.
@SunShine I believe so. What's the length of your ini file?
@SunShine Length or Depth?
more than 25000 words, 5000 lines
@SunShine Can I give you a better approach for this? Can you try JSON or YAML? Coz I believe this would be too much for an ini file.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.