Syntax error using parse_ini_file() when file's value's contain exclamation points and equal signs

Scott B picture Scott B · Jul 13, 2011 · Viewed 9.5k times · Source

The function below takes the "test-backup.ini" file, parses it and inputs the values into the DB via the update_option() method.

However, when the ini file's values contain special characters like exlamation points (!) and equal signs (=) (and others I would guess), its throwing a PHP syntax error at parse_ini_file($file):

Syntax error, unexpected "!", etc...

For example, given this content as the test-backup.ini file...

[settings]
line1 = asc
line2 = /*.blog ul li {margin-bottom:0 !important;}*/
line3 = true
line4 = <meta name="google-site-verification" content="" />

I get syntax errors on line2 for the "!" and on line 4 for the "="

How should I filter the $file before passing it to parse_ini_file() to deal with these characters to that they are preserved when passed to the update_option() call?

All I've found thus far is this:

Characters {}|&~![()" must not be used anywhere in the key and have a special meaning in the value.

$file = WP_PLUGIN_DIR.'/test/test-backup.ini';

if (file_exists($file) && is_readable($file))
{       
    $ini_array = parse_ini_file($file); //errors when value contains =, !, etc
    foreach ($ini_array as $key=>$value) {
        update_option($key, $value); 
    } 
    echo 'The settings have been saved';
}
else
{
    echo 'alternate response here';
}

?>

Answer

Fabrizio D&#39;Ammassa picture Fabrizio D'Ammassa · Jul 13, 2011

You should put your values between double quotes this way:

line1 = asc
line2 = "/*.blog ul li {margin-bottom:0 !important;}*/"
line3 = true
line4 = "<meta name=\"google-site-verification\" content=\"\" />"

Hope this helps