PHP Parse INI file gives me an equal sign error

I am trying to parse an INI file with a URL as one of the parsing variables. The problem is that the url contains '=' in it, and parse_ini_file spits out an error. I tried to avoid the character, but to no avail. Does this happen to someone else? And if so, did someone fix it?

+4
source share
4 answers

Did you include the value in quotation marks? It should not be a problem to have = in the value if you have quotes around your value. Example:

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

it is much better to use INI_SCANNER_RAW as the third parameter parse_ini_file

 parse_ini_file($file, true, INI_SCANNER_RAW); 
+2
source

Here is a quick fix to parse_ini_ * problems with an equal sign. You can also use 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
source

I had the same problem and it drove me crazy! The problem ended up with something stupid ... I created a .ini file on Windows using a file that I renamed to .ini. There seems to be some markup left that was noticed by PHP, but not in my Notepad ++.

I uninstalled .ini and created it on my Linux host. This solved the problem. If you use WAMP or XAMPP on Windows, try creating a new file with just a notepad that ignores any markup.

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

0
source

Source: https://habr.com/ru/post/1341688/


All Articles