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';
}
?>
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With