<?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>My Quick Fix &#187; PHP</title>
	<atom:link href="http://myquickfix.co.uk/category/computers/web-development/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://myquickfix.co.uk</link>
	<description>Quick fixes for little problems...</description>
	<lastBuildDate>Sat, 04 Feb 2012 16:11:06 +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>What is Notice: unserialize() [function.unserialize]: Error at offset then?</title>
		<link>http://myquickfix.co.uk/2011/11/what-is-notice-unserialize-function-unserialize-error-at-offset-then/</link>
		<comments>http://myquickfix.co.uk/2011/11/what-is-notice-unserialize-function-unserialize-error-at-offset-then/#comments</comments>
		<pubDate>Sat, 26 Nov 2011 19:59:22 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://myquickfix.co.uk/?p=188</guid>
		<description><![CDATA[Problem: While doing some Shopp Plugin support work earlier, one customer was getting this error&#8230; Notice: unserialize() [function.unserialize]: Error at offset 111 of 118 bytes in /home/maxwell4/public_html/main/wp-content/plugins/shopp/core/model/Settings.php on line 228 &#8230;she had done a &#8216;find and replace&#8217; in a SQL DB dump to change her domain name, boogered it up accidentally, and needed an explanation. [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Problem:</strong> While doing some <a href="http://shopplugin.net" title="Shopp WordPress Plugin homepage " target="_blank">Shopp Plugin</a> support work earlier, one customer was getting this error&#8230;</p>
<p><em>Notice: unserialize() [function.unserialize]: Error at offset 111 of 118 bytes in /home/maxwell4/public_html/main/wp-content/plugins/shopp/core/model/Settings.php on line 228</em></p>
<p>&#8230;she had done a &#8216;find and replace&#8217; in a SQL DB dump to change her domain name, boogered it up accidentally, and needed an explanation. The answer I gave applies any time that error occurs in PHP, so i thought it was worth posting here!</p>
<p><strong>Reason:</strong> Arrays are often <a href="http://php.net/manual/en/function.serialize.php" title="PHP Serialize function docs" target="_blank">serialized</a> so that they can be stored in a database text field. If you change the data within a serialized array, without changing the character count, you will get the error above.</p>
<p>For example, try this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$thing</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'one'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'two'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$thing</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This will echo out the following: <em>a:2:{i:0;s:3:&#8221;one&#8221;;i:1;s:3:&#8221;two&#8221;;}</em><br />
Now&#8230; if you stored that in a database, you could then retrieve it later and <a href="http://www.php.net/manual/en/function.unserialize.php" title="PHP Unserialize function reference " target="_blank">unserialize()</a> it to get your array back. wohoo!</p>
<p>In that serialized data, s:3:&#8221;one&#8221; means that the first element in the array is a string, and that it&#8217;s 3 characters long. Now.. if you manually changed &#8220;one&#8221; to &#8220;otherone&#8221; like this a:2:{i:0;s:3:&#8221;otherone&#8221;;i:1;s:3:&#8221;two&#8221;;} then when you ran it through unserialize() to retrieve your array, you would get the error were talking about.</p>
<p><strong>Fix:</strong> To correct this you would need to update the string length also like so: a:2:{i:0;s:8:&#8221;otherone&#8221;;i:1;s:3:&#8221;two&#8221;;} because &#8220;otherone&#8221; is actually 8 characters long. See? Good!</p>
<p>Any questions, please leave a message below.</p>
]]></content:encoded>
			<wfw:commentRss>http://myquickfix.co.uk/2011/11/what-is-notice-unserialize-function-unserialize-error-at-offset-then/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Set date.timezone in PHP without using php.ini</title>
		<link>http://myquickfix.co.uk/2011/10/set-date-timezone-without-using-php-in/</link>
		<comments>http://myquickfix.co.uk/2011/10/set-date-timezone-without-using-php-in/#comments</comments>
		<pubDate>Tue, 18 Oct 2011 09:44:27 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[htaccess]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://myquickfix.co.uk/?p=161</guid>
		<description><![CDATA[Problem: I needed to set default timezone for a site to get Zen Cart installed. The usual way would be to call date_default_timezone_set from a globally included script, or to use the date.timezone option in a php.ini file. The problem is that I don&#8217;t have access to the php.ini file and there is no script that [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Problem:</strong> I needed to set default timezone for a site to get Zen Cart installed. The usual way would be to call <a title="date default timezone set php function" href="http://php.net/manual/en/function.date-default-timezone-set.php" target="_blank">date_default_timezone_set</a> from a globally included script, or to use the <a title="date.timezone option manual" href="http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone" target="_blank">date.timezone</a> option in a php.ini file. The problem is that I don&#8217;t have access to the php.ini file and there is no script that will be called across the board.</p>
<p><strong>Fix:</strong> Turns out PHP flags can be called from .htaccess file. So assuming you are using Unix type hosting (or another server that can interpret distributed configuration files), you can add this to your .htaccess file to set the timezone:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">php_value date.timezone Europe<span style="color: #000000; font-weight: bold;">/</span>London</pre></div></div>

<p>Save that, upload it, and if you then look at the output of <a title="Info on the phpinfo PHP function" href="http://php.net/manual/en/function.phpinfo.php" target="_blank">phpinfo()</a> (create a php file with this in and upload it), you should see:</p>
<div id="attachment_162" class="wp-caption alignnone" style="width: 618px"><a href="http://myquickfix.co.uk/wp-content/uploads/2011/10/php.timezone.gif"><img class="size-full wp-image-162" title="php.timezone" src="http://myquickfix.co.uk/wp-content/uploads/2011/10/php.timezone.gif" alt="" width="608" height="137" /></a><p class="wp-caption-text">The Local Value should show what you set in the .htaccess file</p></div>
]]></content:encoded>
			<wfw:commentRss>http://myquickfix.co.uk/2011/10/set-date-timezone-without-using-php-in/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Convert eregi_replace to preg_replace in old class.phpmailer.php scripts</title>
		<link>http://myquickfix.co.uk/2011/09/convert-eregi_replace-to-preg_replace/</link>
		<comments>http://myquickfix.co.uk/2011/09/convert-eregi_replace-to-preg_replace/#comments</comments>
		<pubDate>Fri, 23 Sep 2011 10:39:05 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Regular Expressions]]></category>

		<guid isPermaLink="false">http://myquickfix.co.uk/?p=96</guid>
		<description><![CDATA[Problem: I have lots of legacy php code on old sites that uses eregi_replace to format up an HTML email body. As eregi_replace is now depreciated it can be replaced with preg_replace. This is tricky sometimes because the formatting is quite different. Fix: Here&#8217;s a common line from class.phpmailer.php scripts: $emailBody = eregi_replace&#40;&#34;[\]&#34;,'',$emailBody&#41;; To convert that you&#8217;ll need to do this: [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Problem:</strong> I have lots of legacy php code on old sites that uses eregi_replace to format up an HTML email body. As <a title="The depreciated eregi_replace PHP codex article" href="http://www.php.net/manual/en/function.eregi-replace.php" target="_blank">eregi_replace</a> is now depreciated it can be replaced with <a href="http://uk3.php.net/manual/en/function.preg-replace.php" target="_blank">preg_replace</a>. This is tricky sometimes because the formatting is quite different.</p>
<p><strong>Fix:</strong> Here&#8217;s a common line from class.phpmailer.php scripts:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$emailBody</span> <span style="color: #339933;">=</span> <span style="color: #990000;">eregi_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;[\]&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #000088;">$emailBody</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To convert that you&#8217;ll need to do this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$emailBody</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/<span style="color: #000099; font-weight: bold;">\\</span><span style="color: #000099; font-weight: bold;">\\</span>/&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$emailBody</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Wondering why there are so MANY escaping backslashes there? I asked that question and got this answer:</p>
<blockquote><p>&#8220;..the backslash needs to be escaped once for the php string&#8217;s benefit, then again for the interpretation of the regular expression engine, as escaped characters like \d indicate a digit in regular expressions. So a pattern of \\d would match a digit, but \\\\d would match a backslash then a d character. PHP strings are lenient on backslashing when it isn&#8217;t necessary, so:<br />
- setting a string to &#8220;\d&#8221; will give it a value of \d (the same as setting it to &#8220;\\d&#8221;).<br />
- but setting it to &#8220;\&#8221;" will give a value of &#8220;.<br />
- and setting it to &#8220;\&#8221; will return a syntax error.</p>
<p>- PHP double quoted strings will consume the (first) backslash for \\ \n \t \r and \&#8221;.<br />
- PHP single quoted strings will only consume the first backslash for \\ and \&#8217; &#8220;</p></blockquote>
<p>There.. clear as mud!</p>
]]></content:encoded>
			<wfw:commentRss>http://myquickfix.co.uk/2011/09/convert-eregi_replace-to-preg_replace/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Swap days and months in PHP – quick and dirty MM/DD/YY to DD/MM/YY</title>
		<link>http://myquickfix.co.uk/2009/09/swap-days-and-months-in-php-%e2%80%93-quick-and-dirty-mmddyy-to-ddmmyy/</link>
		<comments>http://myquickfix.co.uk/2009/09/swap-days-and-months-in-php-%e2%80%93-quick-and-dirty-mmddyy-to-ddmmyy/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 16:59:38 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://myquickfix.co.uk/?p=5</guid>
		<description><![CDATA[Problem I have a ton of date strings in the stupid US style dd/mm/yy format. i.e. 12/24/09 being 24th September 2009. I need to convert them to the UK style dd/mm/yy. Solution 1 As a string in PHP is really just an array of characters, the simplest fix was to address the individual number characters [...]]]></description>
			<content:encoded><![CDATA[<h2>Problem</h2>
<p>I have a ton of date strings in the stupid US style dd/mm/yy format. i.e. 12/24/09 being 24th September 2009. I need to convert them to the UK style dd/mm/yy.</p>
<h2>Solution 1</h2>
<p>As a string in PHP is really just an array of characters, the simplest fix was to address the individual number characters by their positions within the sting, and rearrange them into the format I wanted:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$d</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;12/24/09&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$d</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$d</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #000088;">$d</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$d</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;">$d</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: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$d</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #000088;">$d</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$d</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// displays &quot;24/12/19&quot;.</span></pre></div></div>

<h2>Solution 2</h2>
<p>I posted my solution on twitter (@hutchings) and asked for comments and @londonhackspace organiser Jonty came back with the following elegant (and much cleverer) suggestion:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$d</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;12/24/09&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$d</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'|(\d+)/(\d+)/(\d+)|'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'$2/$1/$3'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$d</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$d</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// displays &quot;24/12/19&quot;.</span></pre></div></div>

<p>So provided you know that your original string is in US format, both these solutions can be used. In my testing I found solution 1 executed more quickly, but isn’t as pleasing to look at <img src='http://myquickfix.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Cheers Jonty.</p>
]]></content:encoded>
			<wfw:commentRss>http://myquickfix.co.uk/2009/09/swap-days-and-months-in-php-%e2%80%93-quick-and-dirty-mmddyy-to-ddmmyy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

