<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>
<channel>
	<title>Comments on: Loose and multiline parse_ini_file function in Php</title>
	<atom:link href="http://mach13.com/loose-and-multiline-parse_ini_file-function-in-php/feed" rel="self" type="application/rss+xml" />
	<link>http://mach13.com/loose-and-multiline-parse_ini_file-function-in-php</link>
	<description>Internet Solutions</description>
	<pubDate>Fri, 10 Feb 2012 18:28:25 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Johnny</title>
		<link>http://mach13.com/loose-and-multiline-parse_ini_file-function-in-php/comment-page-1#comment-662</link>
		<dc:creator>Johnny</dc:creator>
		<pubDate>Sun, 15 Jan 2012 15:29:04 +0000</pubDate>
		<guid isPermaLink="false">http://mach13.com/?p=64#comment-662</guid>
		<description>Works! (Tried other three from google search.) Thank you.

Although I do not trust long regular expressions...</description>
		<content:encoded><![CDATA[<p>Works! (Tried other three from google search.) Thank you.</p>
<p>Although I do not trust long regular expressions&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raj</title>
		<link>http://mach13.com/loose-and-multiline-parse_ini_file-function-in-php/comment-page-1#comment-656</link>
		<dc:creator>Raj</dc:creator>
		<pubDate>Thu, 10 Feb 2011 14:47:29 +0000</pubDate>
		<guid isPermaLink="false">http://mach13.com/?p=64#comment-656</guid>
		<description>Nice code.

@nerik:

I had the same problem. I solved it this way:

Replace the preg_match_all(....); stuff with

preg_match_all('#^\s*((\['.$s.'\])&#124;(("?)'.$s.'\\5\s*=\s{0}("?)(.*?)\\7))\s*(;[^\n]*?)?$#ms', @file_get_contents($iIniFile), $aMatches, PREG_SET_ORDER);

Could be done better, I'm new to regular expressions - it looked like chinese to me at first.</description>
		<content:encoded><![CDATA[<p>Nice code.</p>
<p>@nerik:</p>
<p>I had the same problem. I solved it this way:</p>
<p>Replace the preg_match_all(&#8230;.); stuff with</p>
<p>preg_match_all(&#8217;#^\s*((\['.$s.'\])|((&#8221;?)&#8217;.$s.&#8217;\\5\s*=\s{0}(&#8221;?)(.*?)\\7))\s*(;[^\n]*?)?$#ms&#8217;, @file_get_contents($iIniFile), $aMatches, PREG_SET_ORDER);</p>
<p>Could be done better, I&#8217;m new to regular expressions - it looked like chinese to me at first.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ed Greenberg</title>
		<link>http://mach13.com/loose-and-multiline-parse_ini_file-function-in-php/comment-page-1#comment-649</link>
		<dc:creator>Ed Greenberg</dc:creator>
		<pubDate>Fri, 03 Sep 2010 15:49:59 +0000</pubDate>
		<guid isPermaLink="false">http://mach13.com/?p=64#comment-649</guid>
		<description>This is a very nice piece of work, and I really appreciate your posting it. I was able to use it today, and it made my life easier. 

Many thanks.</description>
		<content:encoded><![CDATA[<p>This is a very nice piece of work, and I really appreciate your posting it. I was able to use it today, and it made my life easier. </p>
<p>Many thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nerik</title>
		<link>http://mach13.com/loose-and-multiline-parse_ini_file-function-in-php/comment-page-1#comment-641</link>
		<dc:creator>nerik</dc:creator>
		<pubDate>Thu, 18 Jun 2009 13:44:08 +0000</pubDate>
		<guid isPermaLink="false">http://mach13.com/?p=64#comment-641</guid>
		<description>Hi,

first of all nice work!

Just one issue. 
With an INI file like:

[first_section]
foo=
bar=fast

i expected an array like:
Array ( [first_section] =&#62; Array ( [foo] =&#62; (--empty/null--) [bar] =&#62; fast))

But when I run the script this is my output:
Array ( [first_section] =&#62; Array ( [foo] =&#62; bar=fast))

It seems there's a problem if one of my key values is blank.

Can you help me ?</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>first of all nice work!</p>
<p>Just one issue.<br />
With an INI file like:</p>
<p>[first_section]<br />
foo=<br />
bar=fast</p>
<p>i expected an array like:<br />
Array ( [first_section] =&gt; Array ( [foo] =&gt; (&#8211;empty/null&#8211;) [bar] =&gt; fast))</p>
<p>But when I run the script this is my output:<br />
Array ( [first_section] =&gt; Array ( [foo] =&gt; bar=fast))</p>
<p>It seems there&#8217;s a problem if one of my key values is blank.</p>
<p>Can you help me ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: someone</title>
		<link>http://mach13.com/loose-and-multiline-parse_ini_file-function-in-php/comment-page-1#comment-637</link>
		<dc:creator>someone</dc:creator>
		<pubDate>Sat, 11 Apr 2009 16:13:27 +0000</pubDate>
		<guid isPermaLink="false">http://mach13.com/?p=64#comment-637</guid>
		<description>nice, but whats the licence?</description>
		<content:encoded><![CDATA[<p>nice, but whats the licence?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bitMaster</title>
		<link>http://mach13.com/loose-and-multiline-parse_ini_file-function-in-php/comment-page-1#comment-632</link>
		<dc:creator>bitMaster</dc:creator>
		<pubDate>Fri, 13 Mar 2009 14:47:14 +0000</pubDate>
		<guid isPermaLink="false">http://mach13.com/?p=64#comment-632</guid>
		<description>Excellent piece of code!

I made some changes to suit my needs, here's what's come out of it... it's slightly verbose, I thought of leaving it as is for anyone to better inspect... it handles multiline the smarty way (with triple quotes), as the code you provided would not work with conditions like the following:

var = "blabla
This cose is gonna "break"
because of the quote on line end"

My version goes single line with both single and double quotes, and implements the multiline like this:

var = """
This is a smarty-like multiline variable
"""

I didn't implement delimiter escaping, but at this point it would be trivial.

Here it goes:

function parseDataFile($pFile) {
		$aResult  =
		$aMatches = array();
	
		$a = &#38;$aResult;
		$s = '\s*([[:alnum:]_\- \*]+?)\s*';
	
		$RX	= '';

		$RX .=	'[\s\n]*';													// Any space and empty lines, and discard them
		$RX .=	'(?:';
		$RX .=		'(?:\[\s*(?P[^\n]+?)\s*\])';					// if it's a section, match it as "section"
		$RX .=		'&#124;';													// else...
		$RX .=		'(?:'; 													
		$RX .=			'(?P"?)(?P[^\n]+?)(?P=vardelim)';	// get the var name into "var" and it's delim (should there be one) into "vardelim"
		$RX .=			'\s*=\s*';											// skip the equal sign
		$RX .=			'(?:';												// two possible layouts:
		$RX .=				'(?s:"""\n(?P.*?)\n"""\n)';			// multiline, delimited by """, as in PHP ini files
		$RX .=				'&#124;';											// or...
		$RX .=				'((?P["\']?)(?P.*?)(?P=delim))';	// single line, catching the delimiter, which could be " or '
		$RX .=			')';
		$RX .=		')';
		$RX .=	')';
		$RX	.=	'\s*(?P;[^\n]*?)?$';								// Any comment afterwards gets fetched as "comment", just in case

		preg_match_all('#' . $RX . '#m', @file_get_contents($pFile), $aMatches, PREG_SET_ORDER);
	
		foreach ($aMatches as $aMatch) {
			if (empty($aMatch['section']))	$a[ $aMatch['var'] ] = ( !isset($aMatch['val']) ? $aMatch['multiline'] : $aMatch['val'] );
			else							$a = &#38;$aResult[ $aMatch['section'] ];
		}
	
		return $aResult;
	}


Any run against test-cases is very welcome, would save me debug time ;)

Cya!</description>
		<content:encoded><![CDATA[<p>Excellent piece of code!</p>
<p>I made some changes to suit my needs, here&#8217;s what&#8217;s come out of it&#8230; it&#8217;s slightly verbose, I thought of leaving it as is for anyone to better inspect&#8230; it handles multiline the smarty way (with triple quotes), as the code you provided would not work with conditions like the following:</p>
<p>var = &#8220;blabla<br />
This cose is gonna &#8220;break&#8221;<br />
because of the quote on line end&#8221;</p>
<p>My version goes single line with both single and double quotes, and implements the multiline like this:</p>
<p>var = &#8220;&#8221;"<br />
This is a smarty-like multiline variable<br />
&#8220;&#8221;"</p>
<p>I didn&#8217;t implement delimiter escaping, but at this point it would be trivial.</p>
<p>Here it goes:</p>
<p>function parseDataFile($pFile) {<br />
		$aResult  =<br />
		$aMatches = array();</p>
<p>		$a = &amp;$aResult;<br />
		$s = &#8216;\s*([[:alnum:]_\- \*]+?)\s*&#8217;;</p>
<p>		$RX	= &#8221;;</p>
<p>		$RX .=	&#8216;[\s\n]*&#8217;;													// Any space and empty lines, and discard them<br />
		$RX .=	&#8216;(?:&#8217;;<br />
		$RX .=		&#8216;(?:\[\s*(?P[^\n]+?)\s*\])&#8217;;					// if it&#8217;s a section, match it as &#8220;section&#8221;<br />
		$RX .=		&#8216;|&#8217;;													// else&#8230;<br />
		$RX .=		&#8216;(?:&#8217;;<br />
		$RX .=			&#8216;(?P&#8221;?)(?P[^\n]+?)(?P=vardelim)&#8217;;	// get the var name into &#8220;var&#8221; and it&#8217;s delim (should there be one) into &#8220;vardelim&#8221;<br />
		$RX .=			&#8216;\s*=\s*&#8217;;											// skip the equal sign<br />
		$RX .=			&#8216;(?:&#8217;;												// two possible layouts:<br />
		$RX .=				&#8216;(?s:&#8221;"&#8221;\n(?P.*?)\n&#8221;"&#8221;\n)&#8217;;			// multiline, delimited by &#8220;&#8221;", as in PHP ini files<br />
		$RX .=				&#8216;|&#8217;;											// or&#8230;<br />
		$RX .=				&#8216;((?P["\']?)(?P.*?)(?P=delim))&#8217;;	// single line, catching the delimiter, which could be &#8221; or &#8216;<br />
		$RX .=			&#8216;)&#8217;;<br />
		$RX .=		&#8216;)&#8217;;<br />
		$RX .=	&#8216;)&#8217;;<br />
		$RX	.=	&#8216;\s*(?P;[^\n]*?)?$&#8217;;								// Any comment afterwards gets fetched as &#8220;comment&#8221;, just in case</p>
<p>		preg_match_all(&#8217;#&#8217; . $RX . &#8216;#m&#8217;, @file_get_contents($pFile), $aMatches, PREG_SET_ORDER);</p>
<p>		foreach ($aMatches as $aMatch) {<br />
			if (empty($aMatch['section']))	$a[ $aMatch['var'] ] = ( !isset($aMatch['val']) ? $aMatch['multiline'] : $aMatch['val'] );<br />
			else							$a = &amp;$aResult[ $aMatch['section'] ];<br />
		}</p>
<p>		return $aResult;<br />
	}</p>
<p>Any run against test-cases is very welcome, would save me debug time <img src='http://mach13.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Cya!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael</title>
		<link>http://mach13.com/loose-and-multiline-parse_ini_file-function-in-php/comment-page-1#comment-631</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Sat, 07 Mar 2009 17:12:06 +0000</pubDate>
		<guid isPermaLink="false">http://mach13.com/?p=64#comment-631</guid>
		<description>How simple and concise!  I love it.

I needed it to handle Smarty-style multi-line values, like so:

longtext="""
this is a bunch of text
"""

I thought it would be easy to add an additional processing block in the middle.  This is what I added:

&#124;(("?)'.$s.'\\5\s*=\s*(""")(.*?)""")

between the two existing blocks in the (&#124;) section. I figured it would capture the multi-line values while leaving alone the standard single-quote values. Sadly, I am but a young padawan to your Jedi master regex skills, and my change killed normal single-quote handling.  Sigh.</description>
		<content:encoded><![CDATA[<p>How simple and concise!  I love it.</p>
<p>I needed it to handle Smarty-style multi-line values, like so:</p>
<p>longtext=&#8221;"&#8221;<br />
this is a bunch of text<br />
&#8220;&#8221;"</p>
<p>I thought it would be easy to add an additional processing block in the middle.  This is what I added:</p>
<p>|((&#8221;?)&#8217;.$s.&#8217;\\5\s*=\s*(&#8221;"&#8221;)(.*?)&#8221;"&#8221;)</p>
<p>between the two existing blocks in the (|) section. I figured it would capture the multi-line values while leaving alone the standard single-quote values. Sadly, I am but a young padawan to your Jedi master regex skills, and my change killed normal single-quote handling.  Sigh.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Johnson</title>
		<link>http://mach13.com/loose-and-multiline-parse_ini_file-function-in-php/comment-page-1#comment-23</link>
		<dc:creator>Johnson</dc:creator>
		<pubDate>Wed, 18 Feb 2009 06:32:00 +0000</pubDate>
		<guid isPermaLink="false">http://mach13.com/?p=64#comment-23</guid>
		<description>Good. It works well after your update.
Thanks.</description>
		<content:encoded><![CDATA[<p>Good. It works well after your update.<br />
Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: admin</title>
		<link>http://mach13.com/loose-and-multiline-parse_ini_file-function-in-php/comment-page-1#comment-22</link>
		<dc:creator>admin</dc:creator>
		<pubDate>Mon, 16 Feb 2009 09:14:13 +0000</pubDate>
		<guid isPermaLink="false">http://mach13.com/?p=64#comment-22</guid>
		<description>You are right, Johnson

The PHP function "empty" considers that the string "0" is empty.

The line "if (!empty($aMatch[8]))"
has to be changed to : "if (empty($aMatch[2]))"
for safer Section detection.

The code above has been updated

Thanks</description>
		<content:encoded><![CDATA[<p>You are right, Johnson</p>
<p>The PHP function &#8220;empty&#8221; considers that the string &#8220;0&#8243; is empty.</p>
<p>The line &#8220;if (!empty($aMatch[8]))&#8221;<br />
has to be changed to : &#8220;if (empty($aMatch[2]))&#8221;<br />
for safer Section detection.</p>
<p>The code above has been updated</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Johnson</title>
		<link>http://mach13.com/loose-and-multiline-parse_ini_file-function-in-php/comment-page-1#comment-21</link>
		<dc:creator>Johnson</dc:creator>
		<pubDate>Mon, 16 Feb 2009 05:15:59 +0000</pubDate>
		<guid isPermaLink="false">http://mach13.com/?p=64#comment-21</guid>
		<description>It seems it has a problem to parse the value of key is 0. For example:
aa1 = bbb
aa2 = 0      ; Error
aa3 = "bbb"</description>
		<content:encoded><![CDATA[<p>It seems it has a problem to parse the value of key is 0. For example:<br />
aa1 = bbb<br />
aa2 = 0      ; Error<br />
aa3 = &#8220;bbb&#8221;</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- e-pla.net::website monitoring -->



