<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Alex Stetsenko &#187; PHP</title>
	<atom:link href="http://www.stetsenko.net/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stetsenko.net</link>
	<description>Life and software engineering :: thoughts and steps</description>
	<lastBuildDate>Tue, 17 Jan 2012 16:29:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>PHP: IP Address Validation</title>
		<link>http://www.stetsenko.net/2011/05/php-ip-address-validation/</link>
		<comments>http://www.stetsenko.net/2011/05/php-ip-address-validation/#comments</comments>
		<pubDate>Sat, 14 May 2011 15:44:53 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.stetsenko.net/?p=978</guid>
		<description><![CDATA[It&#8217;s common to use a regular expression to validate IP addresses. However I&#8217;ve run into couple reliable ways to validate IP address without help of regexps. This should make code cleaner and easier to read or debug in case of any issues. The first way is to use PHP function filter_var() which returns the filtered [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s common to use a regular expression to validate IP addresses.<br />
However I&#8217;ve run into couple reliable ways to validate IP address without help of regexps. This should make code cleaner and easier to read or debug in case of any issues.</p>
<p>The first way is to use PHP function <strong>filter_var()</strong> which returns the filtered data, or <em>false</em> if the filter fails.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$ip</span> <span style="color: #339933;">=</span> <span style="color: #990000;">filter_var</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #339933;">,</span> FILTER_VALIDATE_IP<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// IP address is valid</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that <strong>filter_var()</strong> has been supported since PHP 5.2. So it will not work for &#8216;older&#8217; production environments.<br />
So here comes another way to validate an IP address which should work even for PHP 4.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">long2ip</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">ip2long</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$ip</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// IP address is valid</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Also fliter_var() unlike ip2long()/long2ip() supports IPv6. For instance, there are four additional flags:</p>
<ul>
<li>FILTER_FLAG_IPV4 &#8211; allow only IPv4;</li>
<li>FILTER_FLAG_IPV6 &#8211; allow only IPv6;</li>
<li>FILTER_FLAG_NO_PRIV_RANGE &#8211; dot not allow IP from private range;</li>
<li>FILTER_FLAG_NO_RES_RANGE &#8211; do not allow IP from reserved range.</li>
</ul>
<p>For example, the code to allow only IPv6 not from reserved range should look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000088;">$ip</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;2a01:e35:aaa4:6860:a5e7:5ba9:965e:cc93&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ip</span> <span style="color: #339933;">=</span> <span style="color: #990000;">filter_var</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span><span style="color: #339933;">,</span> FILTER_VALIDATE_IP<span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                  FILTER_FLAG_IPV6<span style="color: #339933;">,</span> FILTER_FLAG_NO_RES_RANGE<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ip</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// IP address is valid</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.stetsenko.net/2011/05/php-ip-address-validation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: Array as the arguments part of the sprintf function</title>
		<link>http://www.stetsenko.net/2011/03/php-array-as-the-arguments-part-of-the-sprintf-function/</link>
		<comments>http://www.stetsenko.net/2011/03/php-array-as-the-arguments-part-of-the-sprintf-function/#comments</comments>
		<pubDate>Tue, 29 Mar 2011 02:21:06 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.stetsenko.net/?p=955</guid>
		<description><![CDATA[Using sprintf() function is a great way to integrate variables into string templates. It produces nice and readable code. But the drawback is that sprintf() does not support array as the argument part. As result the code may look less readable as number of parameters increases. In that case vsprintf() is your friend. It works [...]]]></description>
			<content:encoded><![CDATA[<p>Using <a href="http://www.stetsenko.net/2009/09/php-dont-repeat-parameters-using-sprintf/"><strong>sprintf()</strong></a> function is a great way to integrate variables into string templates. It produces nice and readable code. But the drawback is that <strong>sprintf()</strong> does not support array as the argument part. As result the code may look less readable as number of parameters increases.</p>
<p>In that case <strong>vsprintf()</strong> is your friend. It works exact the same way as <strong>sprintf()</strong> except it accepts array instead of separate arguments.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;a href=&quot;%s&quot;&gt;%s&lt;/a&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> <span style="color: #990000;">vsprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;a href=&quot;%s&quot;&gt;%s&lt;/a&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.stetsenko.net/2011/03/php-array-as-the-arguments-part-of-the-sprintf-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: Daylight Savings Time Support</title>
		<link>http://www.stetsenko.net/2011/03/php-daylight-savings-time-support/</link>
		<comments>http://www.stetsenko.net/2011/03/php-daylight-savings-time-support/#comments</comments>
		<pubDate>Sat, 26 Mar 2011 01:15:31 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.stetsenko.net/?p=937</guid>
		<description><![CDATA[The &#8216;old&#8217; way to support time zones in PHP did not have built-in full time zones support. Each time date(&#8216;I&#8217;) flag had to be checked to verify daylight savings time period. For example, to convert local time to GMT could be done the following way. // time zone difference in hours $tz = -5; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>The &#8216;old&#8217; way to support time zones in PHP did not have built-in full time zones support. Each time <strong>date(&#8216;I&#8217;)</strong> flag had to be checked to verify daylight savings time period.</p>
<p>For example, to convert local time to GMT could be done the following way.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// time zone difference in hours</span>
<span style="color: #000088;">$tz</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// local date and time</span>
<span style="color: #000088;">$local</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'2011-03-24 11:00:00'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// adjust time zone difference in hours</span>
<span style="color: #666666; font-style: italic;">// using daylight savings time flag and convert to seconds</span>
<span style="color: #000088;">$diff</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$tz</span> <span style="color: #339933;">+</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'I'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$local</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">3600</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// convert to GMT by subtracting $diff number of seconds</span>
<span style="color: #000088;">$gmt</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d H:i:s'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$local</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$diff</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// the output is 2011-03-24 15:00:00</span>
<span style="color: #b1b100;">print</span> <span style="color: #000088;">$gmt</span><span style="color: #339933;">;</span></pre></div></div>

<p>However utilizing <strong>DateTime class</strong> included in PHP 5 makes time zone related operations less stressful.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$local</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'2011-03-24 11:00:00'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// create and instance of DateTime</span>
<span style="color: #666666; font-style: italic;">// using local date/time and its time zone</span>
<span style="color: #000088;">$date</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DateTime<span style="color: #009900;">&#40;</span><span style="color: #000088;">$local</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">new</span> DateTimeZone<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'America/New_York'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// change time zone</span>
<span style="color: #000088;">$date</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setTimezone</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DateTimeZone<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GMT'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// the date is already converted to GMT</span>
<span style="color: #666666; font-style: italic;">// so the output is 2011-03-24 15:00:00</span>
<span style="color: #b1b100;">print</span> <span style="color: #000088;">$date</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d H:i:s'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So using time zone name instead of actual difference in hours or seconds allows to take care of daylight savings time automatically.</p>
<p>However unlike <a href="http://www.stetsenko.net/2009/05/mysql-daylight-savings-time-support/">MySQL CONVERT_TZ()</a> PHP uses different list of <a href="http://www.php.net/manual/en/timezones.php">time zone names</a>. For example, for Eastern standard time PHP offers &#8216;America/New_York&#8217; time zone name while MySQL prefers &#8216;US/Eastern&#8217;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stetsenko.net/2011/03/php-daylight-savings-time-support/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend Server JobQueue issue</title>
		<link>http://www.stetsenko.net/2011/03/zend-server-jobqueue-issue/</link>
		<comments>http://www.stetsenko.net/2011/03/zend-server-jobqueue-issue/#comments</comments>
		<pubDate>Thu, 24 Mar 2011 23:25:08 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.stetsenko.net/?p=921</guid>
		<description><![CDATA[Zend Server may be a really helpful product and only ZendJobQueue by itself is a great concept for multitasking, queuing or even regular cron replacement. But unfortunately it lacks a good documentation For instance I have not managed to find anything official about ZendJobQueue as nearly good as Zend Framework documentation. There are some good [...]]]></description>
			<content:encoded><![CDATA[<p>Zend Server may be a really helpful product and only ZendJobQueue by itself is a great concept for multitasking, queuing or even regular cron replacement. But unfortunately it lacks a good documentation</p>
<p>For instance I have not managed to find anything official about <strong>ZendJobQueue</strong> as nearly good as Zend Framework documentation. There are some good blog posts and presentations though which were great to start with. But they do not include in-depth details or troubleshooting guide.</p>
<p>In my case I ran into the following error message: <strong>&#8216;Bad response from Job Queue server to a createHttpJob request. QLocalSocket: Remote closed.&#8217;</strong> once I tried to go beyond simple test code.</p>
<p>Although Zend Server was up and running and test ZendJobQueue code was working, it looked like everything had crashed really bad&#8230;</p>
<p>A quick search showed that someone had already run into a similar <a href="http://forums.zend.com/viewtopic.php?f=8&#038;t=6548">Job Queue problem</a> about a year ago. No solution has been posted though. But the fact that my test code still was working helped to address the issue.</p>
<p>So here is my original call which caused the error message showed above.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'paramValue'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ZendJobQueue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$q</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createHttpJob</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/task.php'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And this is the call which works perfectly</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'paramName'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'paramValue'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$q</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ZendJobQueue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$q</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createHttpJob</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/task.php'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So apparently the <strong>$params array must have keys</strong>!<br />
Of course one can tell that simply by looking at the error message but it was not that obvious for the first hour.</p>
<p>P.S.: Zend Server 5.1 Development License.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stetsenko.net/2011/03/zend-server-jobqueue-issue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: Mac address validating and formatting</title>
		<link>http://www.stetsenko.net/2011/01/php-mac-address-validating-and-formatting/</link>
		<comments>http://www.stetsenko.net/2011/01/php-mac-address-validating-and-formatting/#comments</comments>
		<pubDate>Sat, 22 Jan 2011 02:44:28 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.stetsenko.net/?p=894</guid>
		<description><![CDATA[There are three common tasks related to storing mac address in the database as char(12) (it is feasible to use bigint instead but in our case we opted for a text type). 1. Validating mac address entered by user. In our case user is allowed to enter mac address the way s/he wants: 000CF15698AD 00:0C:F1:56:98:AD [...]]]></description>
			<content:encoded><![CDATA[<p>There are three common tasks related to storing mac address in the database as char(12) (it is feasible to use bigint instead but in our case we opted for a text type).</p>
<p><strong>1. Validating mac address entered by user.</strong><br />
In our case user is allowed to enter mac address the way s/he wants:<br />
000CF15698AD<br />
00:0C:F1:56:98:AD<br />
00-0C-F1-56-98-AD</p>
<p>So the perfect solution would be to apply regular expression like this one:<br />
<strong>/([a-fA-F0-9]{2}[:|\-]?){6}/</strong></p>
<p>For example, in plain PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> IsValid<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mac</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/([a-fA-F0-9]{2}[:|\-]?){6}/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mac</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>or using Zend Framework:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> IsValid<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mac</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$validator</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Validate_Regex<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/([a-fA-F0-9]{2}[:|\-]?){6}/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$validator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mac</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>2. Remove separating symbols &#8216;:&#8217; or &#8216;-&#8217; from the mac address to fit only 12 character storage.</strong><br />
This is really easy to implement using <strong>str_replace()</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> RemoveSeparator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mac</span><span style="color: #339933;">,</span> <span style="color: #000088;">$separator</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">':'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'-'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$separator</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mac</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>3. Adding separating symbol &#8216;:&#8217; back when the mac address is displayed to a user.</strong><br />
This could look a bit ugly if the straight forward solution is applied, e.g. something like below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> AddSeparator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mac</span><span style="color: #339933;">,</span> <span style="color: #000088;">$separator</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">':'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mac</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$sub</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mac</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$sub</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$separator</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$mac</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mac</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mac</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// remove trailing colon</span>
  <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>So I kept looking for more elegant solution. Ideally I was hoping for one-line solution. And the magic comes with function <strong>str_split()</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> AddSeparator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$mac</span><span style="color: #339933;">,</span> <span style="color: #000088;">$separator</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">':'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">join</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$separator</span><span style="color: #339933;">,</span> <span style="color: #990000;">str_split</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mac</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.stetsenko.net/2011/01/php-mac-address-validating-and-formatting/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP: split() alternative</title>
		<link>http://www.stetsenko.net/2010/09/php-split-alternative/</link>
		<comments>http://www.stetsenko.net/2010/09/php-split-alternative/#comments</comments>
		<pubDate>Fri, 10 Sep 2010 02:38:11 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.stetsenko.net/?p=819</guid>
		<description><![CDATA[Unfortunately (or fortunately?) split() function became deprecated as of PHP 5.3.0. However besides annoying warning Deprecated: Function split() is deprecated in&#8230; it also means that soon this function is going to be completely unavailable. So it&#8217;s better not to wait when the warning turns into the fatal error. Recommended alternative preg_split() has the same parameters, [...]]]></description>
			<content:encoded><![CDATA[<p>Unfortunately (or fortunately?) split() function became deprecated as of PHP 5.3.0.</p>
<p>However besides annoying warning <strong>Deprecated: Function split() is deprecated in&#8230;</strong> it also means that soon this function is going to be completely unavailable. So it&#8217;s better not to wait when the warning turns into the fatal error.</p>
<p>Recommended alternative <strong>preg_split()</strong> has the same parameters, i.e. the only change required is just adding prefix <em>preg_</em> to each call of <em>split()</em>.</p>
<p>Of course if regular expression is not required then it is better apply <strong>explode()</strong> to split a string by string.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stetsenko.net/2010/09/php-split-alternative/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: How to get microseconds</title>
		<link>http://www.stetsenko.net/2010/07/php-how-to-get-microseconds/</link>
		<comments>http://www.stetsenko.net/2010/07/php-how-to-get-microseconds/#comments</comments>
		<pubDate>Sat, 17 Jul 2010 18:56:04 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.stetsenko.net/?p=751</guid>
		<description><![CDATA[Sometimes it could be necessary to get time with microseconds part. Especially it is good for something like logging when multiple entries are recorded per second and preferably to know how they are spread out. At first glance everything looked very easy: print date&#40;'Y-m-d H:i:s.u'&#41; ; where u is microseconds. However it turns out PHP [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes it could be necessary to get time with microseconds part. Especially it is good for something like logging when multiple entries are recorded per second and preferably to know how they are spread out.</p>
<p>At first glance everything looked very easy:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">print</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d H:i:s.u'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span></pre></div></div>

<p>where <em>u</em> is microseconds.</p>
<p>However it turns out PHP manual has a tiny note which states</p>
<blockquote><p>Since this [date()] function only accepts integer timestamps the <em>u</em> format character is only useful when using the date_format() function with user based timestamps created with date_create().</p></blockquote>
<p>Which means that <em>u</em> kind of works but in case of calling within date() function the result is always <strong>.000000</strong>.</p>
<p>So the straight forward solution could be something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">print</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d H:i:s'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>But the drawback of the solution above is two calls to the time related functions which potentially may cause some accuracy issues.</p>
<p>Thus even better solution is doing just one call of time function:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$usec</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sec</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d H:i:s'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sec</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$usec</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.stetsenko.net/2010/07/php-how-to-get-microseconds/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: Don&#8217;t repeat parameters using sprintf()</title>
		<link>http://www.stetsenko.net/2009/09/php-dont-repeat-parameters-using-sprintf/</link>
		<comments>http://www.stetsenko.net/2009/09/php-dont-repeat-parameters-using-sprintf/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 19:17:16 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.stetsenko.net/?p=407</guid>
		<description><![CDATA[While missing a C# method String.Format() I&#8217;ve found a similar function in PHP, i.e. sprintf(). But since this function is slightly different from String.Format() it is not obvious how to avoid duplicating the same parameters. For instance how to re-write an example below without passing $url and $title twice. $link = sprintf&#40;'&#60;a href=&#34;%s&#34; title=&#34;%s&#34;&#62;%s&#60;/a&#62; (%s)&#60;/li&#62;', [...]]]></description>
			<content:encoded><![CDATA[<p>While missing a C# method <strong>String.Format()</strong> I&#8217;ve found a similar function in PHP, i.e. <strong>sprintf()</strong>. But since this function is slightly different from String.Format() it is not obvious how to avoid duplicating the same parameters.</p>
<p>For instance how to re-write an example below without passing $url and $title twice.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;a href=&quot;%s&quot; title=&quot;%s&quot;&gt;%s&lt;/a&gt; (%s)&lt;/li&gt;'</span><span style="color: #339933;">,</span>
              <span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$title</span><span style="color: #339933;">,</span> <span style="color: #000088;">$title</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The answer is less obvious compare to C# but still pretty handy:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;a href=&quot;%1$s&quot; title=&quot;%2$s&quot;&gt;%2$s&lt;/a&gt; (%1$s)'</span><span style="color: #339933;">,</span>
              <span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In both cases <strong>echo $link</strong> give the same output like:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;http://www.google.com/&quot; title=&quot;Google&quot;&gt;Google&lt;/a&gt; (http://www.google.com/)</pre></div></div>

<p>but the second way makes the code more elegant <img src='http://www.stetsenko.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stetsenko.net/2009/09/php-dont-repeat-parameters-using-sprintf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: json_encode before 5.2.0</title>
		<link>http://www.stetsenko.net/2009/09/php-json_encode-before-5-2-0/</link>
		<comments>http://www.stetsenko.net/2009/09/php-json_encode-before-5-2-0/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 18:53:48 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.stetsenko.net/?p=401</guid>
		<description><![CDATA[Even though it is a lot of talks about PHP 5.3 there are still many production environments which run PHP 5.1.*. As well as sometimes development servers are updated earlier than production ones for testing purposes. It may cause that some new PHP features will not work on production because of older version there. One [...]]]></description>
			<content:encoded><![CDATA[<p>Even though it is a lot of talks about PHP 5.3 there are still many production environments which run PHP 5.1.*. As well as sometimes development servers are updated earlier than production ones for testing purposes. It may cause that some new PHP features will not work on production because of older version there.</p>
<p>One of the most common examples is &#8216;missing&#8217; <strong>json_encode()</strong> function which became native with PHP 5.2.0 onward. Below is possible workaround while your production has not upgraded yet.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'json_encode'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_null</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'null'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'false'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'true'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_scalar</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_float</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Always use &quot;.&quot; for floats.</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">floatval</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        static <span style="color: #000088;">$jsonReplaces</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;\b&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\f</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'\\\\'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'\\/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'\\n'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'\\t'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'\\r'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'\\b'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'\\f'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'\&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$jsonReplaces</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$jsonReplaces</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;'</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">else</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$isList</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #990000;">reset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++,</span> <span style="color: #990000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">key</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$isList</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$isList</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'['</span> <span style="color: #339933;">.</span> <span style="color: #990000;">join</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">']'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$k</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">':'</span><span style="color: #339933;">.</span><span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'{'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">join</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span> <span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'}'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>Update:</strong> the same code is now available through <a href="https://github.com/stetsenko/code/blob/master/json_encode.php">GitHub</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stetsenko.net/2009/09/php-json_encode-before-5-2-0/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

