PEAR's Configuration Object class and commas
PHP's PEAR library of packages has a nice configuration file parsing tool called, aptly enough, Config.php. (It appears to not be well updated--it hasn't been reported touched since 2007.) For most uses, it's pretty convenient and quick to use, especially if you are using and maintaining different types of configuration files, like Windows INI files, for your applications.
I seem to have found an interesting quirk in how it processes lines with commas in the text. I have a configuration file with a stanza like:
If I then echo out the content of $fileTypes, I'll get:
[files] fileroot="." filetypes="Type1,Type 2,Type3,Type4"Note that there are no spaces around the commas.
What I would like to have happen is that I parse that using regular INIFile rules:
$configRoot =& $config->parseConfig($configFile,"IniFile");
$filesConfig =& $configRoot->getItem("section","files");
$fileRoot=$filesConfig->getItem("directive","fileroot")->getContent();
$fileTypes=$filesConfig->getItem("directive","filetypes")->getContent();
(This isn't meant to be the best example of PHP code extant, only code that is able to elicit the bug I want to show.)If I then echo out the content of $fileTypes, I'll get:
Type1,Type 2,Type3,Type4Note that the spaces in "Type 2" are preserved. If I change the filetypes line to read
filetypes="Type1,○Type2,Type3,Type4"Where "○" represents a space (ASCII 32), I get
Type 2,Type3,Type4The first element has been lost! If, however, I type
filetypes="Type1○,Type 2,Type3,Type4"(note that there are spaces in the "Type 2" entry, but the 2 abuts a comma, as does the capital T) I get:
Type1○,Type 2,Type3,Type4The entire entry is there, including the space before the comma! (This is made clear by doing a
var_dump(split(",",$fileTypes)) and getting
array 0 => string 'Type1' (length=5) 1 => string 'Type 2' (length=6) 2 => string 'Type3' (length=5) 3 => string 'Type4' (length=5)in the first case,
array 0 => string 'Type 2' (length=6) 1 => string 'Type3' (length=5) 2 => string 'Type4' (length=5)in the second, and
array 0 => string 'Type1 ' (length=6) 1 => string 'Type 2' (length=6) 2 => string 'Type3' (length=5) 3 => string 'Type4' (length=5)in the third. (Formatting courtesy of xdebug).