1

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?

4
  • ini files normally do not require double quotes around the values. See en.wikipedia.org/wiki/INI_file Commented Jul 3, 2016 at 19:54
  • @PaulH There is no formal standard format for INI files. It's an informal standard. Some dialects support quoting (in particular, Windows' INF files, which use an INI format for a very particular purpose), some don't, and others may require it. Commented Jul 3, 2016 at 20:04
  • @Rhymoid yes, but Mini seems to think quotes are necessary for parse_ini_file() Commented Jul 3, 2016 at 20:06
  • @PaulH I thought Mini thought that quoting allows for line breaks in values, which normally ends a value. Commented Jul 3, 2016 at 20:16

1 Answer 1

3

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 )
Sign up to request clarification or add additional context in comments.

3 Comments

I'm delete my answer, but you can use str_replace this is optimal for replace text, str_replace('C ','; ',$ini_string)
@Naumov str_replace will do the trick in this case, but not in all cases. C shall only be replaced if first character of a line followed by space.
Ty, This worked for me, I thought it was because it was missing the double quotes around the value, but as you said it's the "C" that probbaly stands "Comment" infront of the line that messed with parse_ini

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.