PHP parse .ini file problems with newlines / need regex? -


i have trouble parsing .ini files have values not enclosed quotes , newlines in it. here example:

[section1] id=xyz  # comment foo=bar  description=lorem ipsum dolor sit amet, consectetur adipisicing elit, sed eiusmod  tempor incididunt ut labore et dolore magna aliqua. ut enim ad minim veniam,  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo  consequat. duis aute irure dolor in reprehenderit in voluptate velit esse  cillum dolore eu fugiat nulla pariatur. excepteur sint occaecat cupidatat non  proident, sunt in culpa qui officia deserunt mollit anim id est laborum. screenshot=url-goes-here.png categories=some,categories  vendor=abc  [section2] description=lorem ipsum dolor sit amet, consectetur adipisicing elit, sed eiusmod  tempor incididunt ut labore et dolore magna aliqua. ut enim ad minim veniam,  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo  consequat. duis aute irure dolor in reprehenderit in voluptate velit esse  cillum dolore eu fugiat nulla pariatur. excepteur sint occaecat cupidatat non  proident, sunt in culpa qui officia deserunt mollit anim id est laborum.   lorem ipsum dolor sit amet, consectetur adipisicing elit, sed eiusmod  tempor incididunt ut labore et dolore magna aliqua. ut enim ad minim veniam,   quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo  consequat. duis aute irure dolor in reprehenderit in voluptate velit esse  cillum dolore eu fugiat nulla pariatur. excepteur sint occaecat cupidatat non  proident, sunt in culpa qui officia deserunt mollit anim id est laborum. somekey=somevalue 

when try parse string parse_ini_string($file_content, true, ini_scanner_raw);, returns either false or returns first line of description. e. g.

["description"]=> "lorem ipsum dolor sit amet, consectetur adipisicing elit, sed eiusmod" // next lines missing 

i tried remove newlines , enclose values in quotes, can´t find regex works. need pattern matches each key/value until next key/value or until comment begins.

unfortunately key begins after blank line, not. , values can have blank lines in (look @ description in section2).

so question is, how modify/cleanup string readable parse_ini_string?

you can describe multiline key/value pattern:

/^\w+=\n*(?:\r++(?!\w+=|[[#;])\n+)+/m 

the ini_scanner_normal default option allows multiline values enclosed between quotes, need add quotes:

$content = preg_replace('~^\w+=\k\n*(?:\r++(?!\w+=|[[#;])\n+)+~m', '"$0"', $content); 

pattern details:

~                  # pattern delimiter ^                  # start of line \w+                # key name = \k                 # discards characters on left match result \n*                # 0 or more characters except newlines (?:                # non-capturing group: eventual empty lines until non empty line     \r++           # 1 or more newlines     (?!\w+=|[[#;]) # not followed key/value, section or comment     \n+            # 1 or more characters except newlines )+                 # @ least 1 occurence ~m                 # switch on multiline mode, ^ means "start of line" 

this pattern targets multiline values, other values stay unquoted.

notes: assumed each key, comment, section start @ beginning of line. if isn't case example leading spaces, can adapt pattern adding \h*+ after each newline.

if comments allowed anywhere in line, change \n [^#\r\n]


if want use ini_scanner_raw option, must remove newlines in values:

$pattern = '~(?:\g(?!\a)|^\w+=[^#\r\n]*)\k\r++(?!\w+=|[[#])([^#\r\n]+)~'; $content = preg_replace($pattern, ' $1', $content); 

the pattern matches groups of consecutive newlines character followed non empty line 1 one , replace consecutive newlines space.

an other way use first pattern time preg_replace_callback perform simple character translation in callback function. note way may interesting if want escape special or problematic characters.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -