Search code examples
phpparsingfile-read

How can parse these file pattern in php?


I have some .ini file to read by PHP. Then, the styles of file contents are as follows;

[title1]
delete_date = 0101,0211,1115

[title2]
setting_day_name = aaa, bbb
setting_day = 0811, 1201

[title3]
company = c1,c2,c3

I want some arrays with each sessions(title1, title2, title3) like this;

title1 = array(0101,0211,1115);
title2 = array({'setting_day_name' => aaa, 'setting_day' => '0811'}, 
{setting_day_name: bbb, setting_day: 1201});
title3 = array(c1,c2,c3);

I think how to divide sessions and items in each session is important. How can I pass this file?


Solution

  • You should try to use the parse_ini_file function of PHP (https://www.php.net/manual/en/function.parse-ini-file.php), available since PHP 4.

    In your case:

    $data = parse_ini_file("my_file.ini", true); // true to decode sections
    
    /*
    Result:
    $data === [
      'title1' => [
        'delete_date' => '0101,0211,1115'
        ...
    */