简体   繁体   中英

Using PHP Function parse_ini_file() on an invalid ini file

I have a ini file comming from an external program, that I have no control over. The ini file does not have double quotes around the values Example:

[Setup]
C SetupCode=Code of the currently installed setup (JOW
C machine configuration). Use a maximum of 8 characters.

using parse_ini_file() gives me: syntax error, unexpected ')' I guess i should read the file raw in php and add double quotes around the values like this:

[Setup]
C SetupCode="Code of the currently installed setup (JOW
C machine configuration). Use a maximum of 8 characters."

Is this the best practice, and if so how would I do it?

The INI file format is an informal standard. Variations exist, see https://en.wikipedia.org/wiki/INI_file .
C seems to stand for Comment, but parse_ini_file() does not understand that. So read the ini file in a string, replace the C with ; , then use parse_ini_string()

<?php
// $ini_string = file_get_contents(...);

// test data
$ini_string = "
[Setup]
C SetupCode=Code of the currently installed setup (JOW
C machine configuration). Use a maximum of 8 characters.
SetupCode=my C ode
";

// replace 'C ' at start of line with '; ' multiline
$ini_string = preg_replace('/^C /m', '; ', $ini_string);

$ini_array = parse_ini_string($ini_string);


print_r($ini_array); // outputs Array ( [SetupCode] => my C ode )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM