Vincent
Vincent

Reputation: 1760

How to parse MySQL config file my.cnf using parse_ini_file()

When using parse_ini_file('/etc/mysql/my.cnf') , the following error message is returned:

Warning: syntax error, unexpected '!' in /etc/mysql/my.cnf on line 141

This happens because of the last line of the config file, which is:

!includedir /etc/mysql/conf.d/

Is it possible to use parse_ini_file() in order to parse the MySQL config file and bypass the error related to the exclamation mark or should I look into preg_match() instead? Thank you.

Upvotes: 1

Views: 1357

Answers (1)

Kheshav Sewnundun
Kheshav Sewnundun

Reputation: 1244

Unfortunately parse_ini_file will not do the thing.
You should escape the invalid characters like

^!{}~

before parsing it in parse_ini_string You can consult the manual on the link: http://php.net/manual/en/function.parse-ini-string.php
The following code will do the trick

$myfile = file_get_contents("my.cnf");
$invalid = array("!","{","}","~","^");
$myfile = str_replace($invalid, "", $myfile);
$cfg = parse_ini_string($myfile,TRUE);
var_dump($cfg);

Upvotes: 3

Related Questions