开发者

PHP Parse INI File gives me error about equal sign

I'm trying to parse out an INI file that has a URL as one of the variables to parse. Problem is, the URL contains a '=' in it, and parse_ini_file spits out an error. I tried to escape the character, but to no av开发者_如何学运维ail. Does this happen to anybody else? And if so, has anybody fixed it?


Have you enclosed the value in quotes? It shouldn't be a problem to have = in the value as long as you have quotes around your value. Example:

key1="http://www.google.com?q=test";


much better would be use INI_SCANNER_RAW as 3rd parameter of parse_ini_file

parse_ini_file($file, true, INI_SCANNER_RAW);


I had the same problem and it drove me insane! The problem ended up being something silly ... I had created the .ini file in Windows, using a file that I renamed to .ini. Apparently there was some markup left which was seen by PHP, but not in my Notepad++.

I deleted the .ini and created one on my Linux host. This resolved the problem. If you're using WAMP or XAMPP on Windows, try to create a new file with just notepad, which disregards any markup.

I know this is an old topic, but I ended up here looking for the same problem, so it might help someone else.


Here is a quick solution to fix parse_ini_* problems with equality sign. You can use also regex, exploding arrays, etc.

function parseIniFile($file) {
if (!is_file($file)) return null;
$iniFileContent = file_get_contents($file);
return parseIniString($iniFileContent);
}

/* solves the equalitiy sign problem */
function parseIniString($iniFileContent==''){
$iniArray = array();
$iniFileContentArray = explode("\n", $iniFileContent);
foreach ($iniFileContentArray as $iniFileContentArrayRow){
    $iniArrayKey = substr($iniFileContentArrayRow, 0, strpos($iniFileContentArrayRow, '='));
    $iniArrayValue = substr($iniFileContentArrayRow, (strpos($iniFileContentArrayRow, '=')+1));
    $iniArray[$iniArrayKey] = $iniArrayValue;
}
return $iniArray;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜