Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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';
}

?>

like image 809
Scott B Avatar asked Jul 13 '11 19:07

Scott B


2 Answers

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

like image 125
Fabrizio D'Ammassa Avatar answered Nov 02 '22 22:11

Fabrizio D'Ammassa


You don't need to change your ini files you can just add the INI_SCANNER_RAW option.

parse_ini_file('your.ini', true, INI_SCANNER_RAW);

will do the job.

like image 42
chickenchilli Avatar answered Nov 02 '22 20:11

chickenchilli