<?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>Zenovation's Blog &#187; php</title>
	<atom:link href="http://zenovations.com/blog/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://zenovations.com/blog</link>
	<description>A blog about web design, programming, hosting, and virtualmin hacks</description>
	<lastBuildDate>Mon, 16 Jan 2012 15:01:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Regular expressions tester for your regex fiddling</title>
		<link>http://zenovations.com/blog/2012/01/regular-expressions-tester-for-your-regex-fiddling/</link>
		<comments>http://zenovations.com/blog/2012/01/regular-expressions-tester-for-your-regex-fiddling/#comments</comments>
		<pubDate>Fri, 06 Jan 2012 20:48:43 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://zenovations.com/blog/?p=265</guid>
		<description><![CDATA[I was teaching a good friend a bit about regular expressions and wrote up a regular expression tester. It was a blast to write and simple to implement. It&#8217;s all in one HTML page so you can download it and fiddle away. Hope it helps someone else as well: http://www.zenovations.com/blog/misc/regex.html]]></description>
			<content:encoded><![CDATA[<p>I was teaching a good friend a bit about regular expressions and wrote up a regular expression tester. It was a blast to write and simple to implement. It&#8217;s all in one HTML page so you can download it and fiddle away. Hope it helps someone else as well:</p>
<p>http://www.zenovations.com/blog/misc/regex.html</p>
]]></content:encoded>
			<wfw:commentRss>http://zenovations.com/blog/2012/01/regular-expressions-tester-for-your-regex-fiddling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>creating response headers</title>
		<link>http://zenovations.com/blog/2011/10/creating-response-headers/</link>
		<comments>http://zenovations.com/blog/2011/10/creating-response-headers/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 16:04:41 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[response headers]]></category>

		<guid isPermaLink="false">http://zenovations.com/blog/?p=259</guid>
		<description><![CDATA[PHPWeby has a great article on how to create response headers for various means, such as 503, file download dialog, apache authentication, and so on. http://phpweby.com/tutorials/php/35 &#160; &#160;]]></description>
			<content:encoded><![CDATA[<p>PHPWeby has a great article on how to create response headers for various means, such as 503, file download dialog, apache authentication, and so on.</p>
<p><a href="http://phpweby.com/tutorials/php/35">http://phpweby.com/tutorials/php/35</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://zenovations.com/blog/2011/10/creating-response-headers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>remote debugging PHP for a single virtualhost with PhpStorm and xdebug</title>
		<link>http://zenovations.com/blog/2011/07/remote-debugging-php-for-a-single-virtualhost-with-phpstorm-and-xdebug/</link>
		<comments>http://zenovations.com/blog/2011/07/remote-debugging-php-for-a-single-virtualhost-with-phpstorm-and-xdebug/#comments</comments>
		<pubDate>Sun, 10 Jul 2011 01:16:30 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Web Dev]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[phpstorm]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://zenovations.com/blog/?p=231</guid>
		<description><![CDATA[I struggled for several days to set up remote debugging. I wanted to be able to remotely debug a single virtualhost on my CentOS server using my local IDE (PhpStorm) and xdebug. Since this took me quite a while to figure out, I thought I&#8217;d include the steps here for others. Installing Xdebug Over SSH, [...]]]></description>
			<content:encoded><![CDATA[<p>I struggled for several days to set up remote debugging. I wanted to be able to remotely debug a single virtualhost on my CentOS server using my local IDE (<a href="http://www.jetbrains.com/phpstorm/">PhpStorm</a>) and <a href="http://xdebug.org/">xdebug</a>.</p>
<p>Since this took me quite a while to figure out, I thought I&#8217;d include the steps here for others.</p>
<h2>Installing Xdebug</h2>
<p>Over SSH, I ran the following:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p231code8'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2318"><td class="code" id="p231code8"><pre class="bash" style="font-family:monospace;">$ pecl <span style="color: #c20cb9; font-weight: bold;">install</span> xdebug</pre></td></tr></table></div>

<h2>Configuring Xdebug in php.ini</h2>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p231code9'); return false;">View Code</a> INI</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2319"><td class="code" id="p231code9"><pre class="ini" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">;;;;;;;;;;;;;;;;;;;</span>
<span style="color: #666666; font-style: italic;">; Module Settings ;</span>
<span style="color: #666666; font-style: italic;">;;;;;;;;;;;;;;;;;;;</span>
<span style="color: #000099;">zend_extension</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">/usr/lib/php/modules/xdebug.so</span>
&nbsp;
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>xdebug<span style="">&#93;</span></span>
xdebug.remote_enable<span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">1</span>
xdebug.remote_port<span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">9000</span>
xdebug.idekey<span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">PHPSTORM-XDEBUG</span>
xdebug.remote_connect_back<span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">1</span>
&nbsp;
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>Date<span style="">&#93;</span></span></pre></td></tr></table></div>

<p>Note the use of zend_extension; as of PHP 5.3, this is the only one that works (extension=&#8230;) will appear to function, but never connect!</p>
<p>Also, I used xdebug.remote_connect_back=1 &#8211; this allows connections from anywhere. But since I&#8217;ve configured my server to load a separate php.ini for each site (sorry, this is outside the scope of this discussion), and the entire site is under development and protected by .htaccess, I didn&#8217;t have to worry about other people debugging remotely.</p>
<p>An alternative to using remote_connect_back is to specify the remote_host to your <a href="http://www.whatsmyip.org/">local ip address</a>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p231code10'); return false;">View Code</a> INI</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23110"><td class="code" id="p231code10"><pre class="ini" style="font-family:monospace;">xdebug.remote_host<span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">0.0.0.0 </span><span style="color: #666666; font-style: italic;">;your ip address</span>
<span style="color: #666666; font-style: italic;">;xdebug.remote_connect_back=0</span></pre></td></tr></table></div>

<p><strong> Alternative: Loading via .htaccess instead of php.ini</strong></p>
<p>Alternately, I could have dropped a .htaccess file into my root directory for the virtualhost, if the php.ini for each site were too much trouble:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p231code11'); return false;">View Code</a> INI</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23111"><td class="code" id="p231code11"><pre class="ini" style="font-family:monospace;">php_value  zend_extension<span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">/usr/lib/php/modules/xdebug.so</span>
php_flag   xdebug.remote_enable on
php_value  xdebug.remote_port <span style="">9000</span>
php_value  xdebug.idekey PHPSTORM-XDEBUG
php_flag   xdebug.remote_connect_back on</pre></td></tr></table></div>

<h2>Configuring PhpStorm</h2>
<p>I followed <a href="http://blogs.jetbrains.com/webide/2011/03/configure-php-debugging-in-phpstorm-2-0/">these instructions</a> to the letter. Make sure you set up the &#8220;PHP Remote Debug&#8221; and not the &#8220;PHP Server&#8221; option if you&#8217;re using my list here as a guide (both work, I just chose the remote)</p>
<p>Remember to get those IDEKEY values to match up in the editor and on the server!</p>
<h2>Configure Firewalls</h2>
<p>On CentOS, I use apf, so I configured my firewall by adding port 9000 into the following settings: IG_TCP_PORTS, EG_TCP_PORTS</p>
<p>Then I restarted apf:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p231code12'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23112"><td class="code" id="p231code12"><pre class="bash" style="font-family:monospace;">$ <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apf restart</pre></td></tr></table></div>

<p>At home, I opened a connection in my firewall by adding a redirect for port 9000 to my personal PC.</p>
<h2>Starting it Up</h2>
<p>Remember to reload Apache&#8217;s config!</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p231code13'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23113"><td class="code" id="p231code13"><pre class="bash" style="font-family:monospace;">$ <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>httpd reload</pre></td></tr></table></div>

<p>Make sure PhpStorm is set to break on first line, or you won&#8217;t know if it worked! (you need the debugger to get involved):</p>
<p><a href="http://zenovations.com/blog/wp-content/uploads/2011/07/2011-07-09-1800_03.png"><img class="alignnone size-full wp-image-234" title="2011-07-09-1800_03" src="http://zenovations.com/blog/wp-content/uploads/2011/07/2011-07-09-1800_03.png" alt="" width="554" height="259" /></a></p>
<p>&nbsp;</p>
<p>Start the PhpStorm Remote Debug:<br />
<a href="http://zenovations.com/blog/wp-content/uploads/2011/07/2011-07-09-1752_01.png"><img class="alignnone size-full wp-image-232" title="2011-07-09-1752_01" src="http://zenovations.com/blog/wp-content/uploads/2011/07/2011-07-09-1752_01.png" alt="" width="400" height="132" /></a></p>
<p>&nbsp;</p>
<p>Make sure to click the &#8220;listen for connections&#8221; icon!</p>
<p><a href="http://zenovations.com/blog/wp-content/uploads/2011/07/2011-07-09-1755_02.png"><img class="alignnone size-full wp-image-233" title="2011-07-09-1755_02" src="http://zenovations.com/blog/wp-content/uploads/2011/07/2011-07-09-1755_02.png" alt="" width="502" height="131" /></a></p>
<p>A quick note on semantics here: It is not technically necessary to have the debug server running and &#8220;listen for connections&#8221; clicked. Listen for connections will, in fact, start the debugger when an incoming connection is received. However, for initial testing, having the debugger actually ON was very helpful in being sure PhpStorm wasn&#8217;t failing to start incoming connections.</p>
<p>&nbsp;</p>
<p>Connect to your IP address from the web server to make sure it can talk to your IDE:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p231code14'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23114"><td class="code" id="p231code14"><pre class="bash" style="font-family:monospace;">$ telnet 0.0.0.0 <span style="color: #000000;">9000</span></pre></td></tr></table></div>

<p>&nbsp;</p>
<p>Add <strong>XDEBUG_SESSION_START=PHPSTORM-XDEBUG</strong> in the URL of the site to initiate a connection:</p>
<p><a href="http://zenovations.com/blog/wp-content/uploads/2011/07/2011-07-09-1804_05.png"><img class="alignnone size-full wp-image-236" title="2011-07-09-1804_05" src="http://zenovations.com/blog/wp-content/uploads/2011/07/2011-07-09-1804_05.png" alt="" width="396" height="164" /></a></p>
<p>&nbsp;</p>
<p>Witness debugging in all its glory!</p>
<p><a href="http://zenovations.com/blog/wp-content/uploads/2011/07/2011-07-09-1807_06.png"><img class="alignnone size-full wp-image-237" title="2011-07-09-1807_06" src="http://zenovations.com/blog/wp-content/uploads/2011/07/2011-07-09-1807_06.png" alt="" width="625" height="162" /></a></p>
<p>&nbsp;</p>
<h2>Browser Plugins</h2>
<p>There are browser plugins for <a href="https://chrome.google.com/webstore/detail/eadndfjplgieldjbigjakmdgkmoaaaoc" target="_blank">Chrome </a>and <a href="https://addons.mozilla.org/en-US/firefox/addon/easy-xdebug/" target="_blank">Firefox </a>that make remote connections a breeze; you no longer have to type the parms into the URL. I&#8217;ve used both of these with success and highly recommend this simplification.</p>
]]></content:encoded>
			<wfw:commentRss>http://zenovations.com/blog/2011/07/remote-debugging-php-for-a-single-virtualhost-with-phpstorm-and-xdebug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Find previous occurrence of string using PHP&#8217;s strrev() and preg_match()</title>
		<link>http://zenovations.com/blog/2010/01/find-previous-occurrence-of-string-using-phps-strrev-and-preg_match/</link>
		<comments>http://zenovations.com/blog/2010/01/find-previous-occurrence-of-string-using-phps-strrev-and-preg_match/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 00:59:15 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[Technical]]></category>

		<guid isPermaLink="false">http://zenovations.com/blog/?p=161</guid>
		<description><![CDATA[Today I wrote a class to iterate words in a string. One challenge was finding my way backwards in a string. Specifically, given a starting position inside the string, I wanted to find the previous &#8220;word&#8221; and return it. However, since this needs to work localized (not just a-z), and the definition of a &#8220;word&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>Today I wrote a class to iterate words in a string. One challenge was finding my way backwards in a string. Specifically, given a starting position inside the string, I wanted to find the previous &#8220;word&#8221; and return it. However, since this needs to work localized (not just a-z), and the definition of a &#8220;word&#8221; is configurable, it was no simple matter of looking back for the previous space character.</p>
<p>So here is what I came up with; a method that finds the next or previous word given a starting position in the string:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p161code17'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p16117"><td class="code" id="p161code17"><pre class="php" style="font-family:monospace;">   <span style="color: #009933; font-style: italic;">/**
    * Abstracted method for finding the next/prev word. This method assumes that 
    * $pos is greater than zero and less than the length of $text (check before calling)
    *
    * @param string $text the string of text to find next/prev word in
    * @param int $pos the position of first character in current word
    * @param string $wordPattern the regex definition of a word without any matching parens
    * @param string $reverse looks backward instead of forward (finds last word in string)
    * @return mixed false if no more words or array( &quot;the word matched with junk&quot;, &quot;the word only&quot;)
    */</span>
   <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000000; font-weight: bold;">function</span> nextWordMatch<span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pos</span><span style="color: #339933;">,</span> <span style="color: #000088;">$wordPattern</span><span style="color: #339933;">,</span> <span style="color: #000088;">$reverse</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;">// we get the substring of text, starting at the current position</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$reverse</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #666666; font-style: italic;">// in this case, we look at everything before $pos; we reverse it so that</span>
         <span style="color: #666666; font-style: italic;">// we can run a simple regex on it rather than trying to deal with craziness</span>
         <span style="color: #666666; font-style: italic;">// of looking backwards in string</span>
         <span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/substr"><span style="color: #990000;">substr</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pos</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>
      <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #666666; font-style: italic;">// in this case, we look at everything after $pos</span>
         <span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/substr"><span style="color: #990000;">substr</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pos</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;">// we escape the preg character just in case</span>
      <span style="color: #666666; font-style: italic;">// we add in two sets of match parens, one for the word and one for the whole match</span>
      <span style="color: #666666; font-style: italic;">// when looking backwards, we need to look from the end rather than the start</span>
      <span style="color: #000088;">$wordPattern</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/str_replace"><span style="color: #990000;">str_replace</span></a><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: #000088;">$wordPattern</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;((<span style="color: #006699; font-weight: bold;">{$wordPattern}</span>)&quot;</span><span style="color: #339933;">.</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">NON_WORD_CHARS</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$reverse</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;@<span style="color: #006699; font-weight: bold;">{$pattern}</span><span style="color: #000099; font-weight: bold;">\$</span>@&quot;</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: #000088;">$pattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;@^<span style="color: #006699; font-weight: bold;">{$pattern}</span>@&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// perform the match now and figure out what to do with it</span>
      <a href="http://www.php.net/preg_match"><span style="color: #990000;">preg_match</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pattern</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">3</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// remember that the first match is the raw text, so we add one</span>
         <span style="color: #666666; font-style: italic;">// we didn't find any words, so return false</span>
         <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// strip off the raw text, leaving our two matches</span>
      <span style="color: #b1b100;">return</span> <a href="http://www.php.net/array_slice"><span style="color: #990000;">array_slice</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</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></td></tr></table></div>

<p>Here is the default value for $wordPattern and the constant NON_WORD_CHARS used in the example:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p161code18'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p16118"><td class="code" id="p161code18"><pre class="php" style="font-family:monospace;">   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$wordPattern</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'\b[\w]+(?:[-\']\w+)*\b'</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">const</span> NON_WORD_CHARS <span style="color: #339933;">=</span> <span style="color: #0000ff;">'\W*'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://zenovations.com/blog/2010/01/find-previous-occurrence-of-string-using-phps-strrev-and-preg_match/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick and Dirty Database Conversion Tool</title>
		<link>http://zenovations.com/blog/2009/04/quick-and-dirty-database-conversion-tool/</link>
		<comments>http://zenovations.com/blog/2009/04/quick-and-dirty-database-conversion-tool/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 19:11:29 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://zenovations.com/blog/?p=16</guid>
		<description><![CDATA[I created a quick tool for importing database data and modifying structure and data as it goes. It populates a single table at a time, offers decent flexibility, and allows you to specify your own callback functions to deal with data conversion. It can handle inserts and allows you to specify customized queries if desired. [...]]]></description>
			<content:encoded><![CDATA[<p>I created a quick tool for importing database data and modifying structure and data as it goes. It populates a single table at a time, offers decent flexibility, and allows you to specify your own callback functions to deal with data conversion.  It can handle inserts and allows you to specify customized queries if desired. It allows any complexity of joins on the source data. Alas, it only works in mysql because that&#8217;s all I needed it for.  Use an adapt for any non-evil purpose you like &#8211; Creative Commons.  Usage:</p>
<ol>
<li>Open configure.php</li>
<li>Set $fake_inserts = true (for testing)</li>
<li>Probably want to set $max_rows (for testing)</li>
<li>Set up your table conversions using the $tablemap arrays</li>
<li>Call the script to test results: php ./converter.php</li>
<li>When satisfied, set $fake_inserts = false (got a backup??)</li>
<li>Whammo!</li>
</ol>
<p>Here is the <b>config.php</b> file you&#8217;ll need to tweak:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p16code21'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1621"><td class="code" id="p16code21"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*************************************************************
   CONFIGURABLE PARAMETERS
**********************************************************/</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * These can also be configured per-query below, if more granularity is needed
 */</span>
<span style="color: #000088;">$from_db_connection</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'db-instance'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$to_db_connection</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'db-instance'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * We either output by printing insert statements to the screen for testing(true),
 * or by running the insert queries directly on the target db(false-default). Set this
 * to false when you're ready to import for real
 */</span>
<span style="color: #000088;">$fake_inserts</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Max rows allows you to only run a certain number of queries per table, for testing. Leave
 * this set to 0 for the real run
 */</span>
<span style="color: #000088;">$max_rows</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * If true, script will exist on any failed insert query.
 * If false, will continue to run even if queries fail and insert the other records.
 */</span>
<span style="color: #000088;">$stop_on_failed_query</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * $tablemap is an array containing one entry for each table to be converted.
 * Note that tables don't necessarily have to be 1:1 to make this work, since
 * the source query can use whatever sort of joins needed to set up the source
 * fields. There must, of course, be exactly one output table.
 *
 * &lt;p&gt;Here are the fields for each conversion element:
 * &lt;ul&gt;
 *   &lt;li&gt;connection info - an array containin the source database and target database. It contains two
 *       elements, each is an array containing array( host, username, password, db_instance )
 *   &lt;li&gt;source_query - the query to be run against source db, set it up however you like, the field names should correspond with the 'fields' array's keys
 *   &lt;li&gt;fields - see below
 *   &lt;li&gt;defaults - default values to apply to any fields in target table, overwritten by values in 'fields'
 * &lt;/ul&gt;
 *
 * &lt;p&gt;If you just want to create insert statements with the data, use dest_table. If you
 * need custom queries or updates to run, you can specify dest_query. Values for any output fields
 * can be substituted using {field_name} in the query.
 *
 * &lt;p&gt;The 'fields' element is an indexed array. The key is the source table's column name. The value is either a string indicating the name of hte column in the destination table, or an array containing exactly two elements: 1) a function to call, which will be passed the value from source table, 2) the destination column name
 *
 * &lt;p&gt;The function is passed a host of arguments, in order:
 * &lt;ol&gt;
 *  &lt;li&gt;value from originating column
 *  &lt;li&gt;entire data row
 *  &lt;li&gt;original column name
 *  &lt;li&gt;destination column name
 * &lt;/ol&gt;
 *
 * &lt;p&gt;To list the same field twice (since the keys would be identical) you can use an array index which will be stripped from the column name before querying, such as &quot;field_id[1]&quot;, &quot;field_id[2]&quot;, which both refer to a column named &quot;field_id&quot;
 */</span>
<span style="color: #000088;">$tablemap</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
* This example is a simple, straight copy from one table to another
*/</span>
<span style="color: #000088;">$tablemap</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
	 <span style="color: #0000ff;">&quot;connection_info&quot;</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
				    <span style="color: #0000ff;">&quot;source&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$from_db_connection</span><span style="color: #339933;">,</span>
				    <span style="color: #0000ff;">&quot;dest&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$to_db_connection</span>
	 <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	 <span style="color: #0000ff;">&quot;source_query&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'select * from source_table'</span><span style="color: #339933;">,</span>
	 <span style="color: #0000ff;">&quot;dest_table&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;destination_table&quot;</span><span style="color: #339933;">,</span>
	 <span style="color: #0000ff;">'fields'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'source1'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'dest1'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'source2'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'dest2'</span><span style="color: #339933;">,</span>
			   <span style="color: #0000ff;">'posted'</span>  <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'toSqlDate'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'date'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// convert unix timestamp to sql date</span>
			   <span style="color: #0000ff;">'id[1]'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// here we set two dest fields from &quot;id&quot;, using array index</span>
			   <span style="color: #0000ff;">'id[2]'</span>   <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'getParentId'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'parent'</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// another custom function</span>
	 <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	 <span style="color: #0000ff;">'defaults'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
			   <span style="color: #0000ff;">'dest3'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'0'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'dest4'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</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;">/*
 * This example runs a customized query instead of a normal insert
 */</span>
&nbsp;
<span style="color: #000088;">$tablemap</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
	 <span style="color: #0000ff;">&quot;connection_info&quot;</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
          <span style="color: #0000ff;">&quot;source&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$from_db_connection</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">&quot;dest&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$to_db_connection</span>
	 <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	 <span style="color: #0000ff;">&quot;source_query&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'
         select 
            count(*) as src_total, category
         from 
            source_table_1 s1, source_table_2 s2
         where
            s1.id = s2.id AND s2.id &gt; 100
         group by
            category'</span><span style="color: #339933;">,</span>
	 <span style="color: #0000ff;">&quot;dest_query&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'update dest_table set total = {dest_total} where catid = {cat}'</span><span style="color: #339933;">,</span>
	 <span style="color: #0000ff;">'fields'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
			   <span style="color: #0000ff;">'category'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'cat'</span><span style="color: #339933;">,</span>
			   <span style="color: #0000ff;">'src_total'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'dest_total'</span>
	 <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
	 <span style="color: #0000ff;">'defaults'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// these are still applicable, just didn't need any</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;">/*****************************************************************************
  USER FUNCTIONS (methods used by the 'fields' array to convert values)
***************************************************************************/</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Just an example function that creates a value by combining two cols from source table
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> getParentId<span style="color: #009900;">&#40;</span><span style="color: #000088;">$src_id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data_row</span><span style="color: #339933;">,</span> <span style="color: #000088;">$src_col_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dest_col_name</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: #000088;">$src_id</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: #666666; font-style: italic;">// if it exists, use it</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$src_id</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #666666; font-style: italic;">// otherwise, create one</span>
   <span style="color: #b1b100;">return</span> <span style="color: #000088;">$data_row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;number_1&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$data_row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;number_2&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Example function to create an incremental id for the result data
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> getIncrementalId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$currentIncId</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #339933;">++</span><span style="color: #000088;">$currentIncId</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$currentIncId</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Converts a unix timestamp to a sql datetime
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> toSqlDate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$utime</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <a href="http://www.php.net/date"><span style="color: #990000;">date</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y-m-d H:i:s&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$utime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>And here is the <b>convert.php</b> script you&#8217;ll be running:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p16code22'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1622"><td class="code" id="p16code22"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #666666; font-style: italic;">/*********************************
 GO TO THIS FILE TO CONFIGURE
 ********************************/</span>
  <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;config.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*********************************
 YOU WON'T NEED TO CHANGE ANYTHING BELOW HERE
 ********************************/</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Prints out contents and adds html chars if this isn't cli
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> printIt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$txt</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$e</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/php_sapi_name"><span style="color: #990000;">php_sapi_name</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;cli&quot;</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;&lt;br&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">print</span> <span style="color: #000088;">$txt</span><span style="color: #339933;">.</span><span style="color: #000088;">$e</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Generates a list of insertable values, escaped for sql
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> make_vals<span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$s</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</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: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$s</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$s</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;, &quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$s</span> <span style="color: #339933;">.=</span> fixMySquirrelVal<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: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$s</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Adds a data row to the dest table
 * @param resource $dbin the db connection
 * @param string $table dest table
 * @param array $row indexed array of (string)col =&gt; (mixed)value - do not escape these
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> addRow<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbin</span><span style="color: #339933;">,</span> <span style="color: #000088;">$table</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$vals</span> <span style="color: #339933;">=</span> make_vals<span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$cols</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'`'</span><span style="color: #339933;">.</span><a href="http://www.php.net/join"><span style="color: #990000;">join</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'`,`'</span><span style="color: #339933;">,</span><a href="http://www.php.net/array_keys"><span style="color: #990000;">array_keys</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</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: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO `<span style="color: #006699; font-weight: bold;">$table</span>` (<span style="color: #006699; font-weight: bold;">$cols</span>) VALUES(<span style="color: #006699; font-weight: bold;">$vals</span>)&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> runMySquirrelQuery<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbin</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Runs a custom query rather than a simple insert
 * @param resource $dbin the db connection
 * @param string $query the custom query, with {field_name} anywhere the field vals will be substituted
 * @param array $row indexed array of (string)col =&gt; (mixed)value - do not escape these
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> customQuery<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbin</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// substitute our new vals</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</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: #009900;">&#123;</span>
    <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/str_replace"><span style="color: #990000;">str_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;{<span style="color: #006699; font-weight: bold;">{$k}</span>}&quot;</span><span style="color: #339933;">,</span> fixMySquirrelVal<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: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #666666; font-style: italic;">// run query with custom vals</span>
  <span style="color: #b1b100;">return</span> runMySquirrelQuery<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbin</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> fixMySquirrelVal<span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</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><a href="http://www.php.net/is_null"><span style="color: #990000;">is_null</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</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: #0000ff;">&quot;NULL&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <a href="http://www.php.net/sprintf"><span style="color: #990000;">sprintf</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; '<span style="color: #009933; font-weight: bold;">%s</span>' &quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/mysql_real_escape_string"><span style="color: #990000;">mysql_real_escape_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</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: #000000; font-weight: bold;">function</span> runMySquirrelQuery<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbin</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$fake_inserts</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$stop_on_failed_query</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$fake_inserts</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    printIt<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbin</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;!!ERROR!! Unable to insert record (<span style="color: #006699; font-weight: bold;">$query</span>): &quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><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;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$stop_on_failed_query</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Given a row from the source table, converts field names and values
 * to be compatible for dest table
 * @param array $row indexed array of (string)col =&gt; (mixed)value - do not escape these
 * @param array $fields indexed array of (string)source_field =&gt; (mixed)dest_field
 * @param array $defaults indexed array of (string)dest_field =&gt; (mixed)value -- can be null
 * @return array indexed by column names for dest table
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> convertRow<span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fields</span><span style="color: #339933;">,</span> <span style="color: #000088;">$defaults</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// start by applying default values, these can be overwritten if fields contains same value</span>
  <span style="color: #000088;">$newrow</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/is_array"><span style="color: #990000;">is_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$defaults</span><span style="color: #009900;">&#41;</span>? <span style="color: #000088;">$defaults</span> <span style="color: #339933;">:</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fields</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: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// for multiple fields with same key, strips off the [n] from end</span>
    <span style="color: #000088;">$k</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_replace"><span style="color: #990000;">preg_replace</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;@\[[0-9]+\]$@&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #666666; font-style: italic;">// run user functions as needed to modify values</span>
    <span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/is_array"><span style="color: #990000;">is_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span>? <span style="color: #000088;">$v</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;">$v</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$val</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/is_array"><span style="color: #990000;">is_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span>? <a href="http://www.php.net/call_user_func"><span style="color: #990000;">call_user_func</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$v</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;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #339933;">,</span> <span style="color: #000088;">$k</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/is_null"><span style="color: #990000;">is_null</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</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;">// create the new column in data row</span>
      <span style="color: #000088;">$newrow</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$val</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$newrow</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> connectToDb<span style="color: #009900;">&#40;</span><span style="color: #000088;">$connection_info</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <a href="http://www.php.net/list"><span style="color: #990000;">list</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pass</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbname</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$connection_info</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$dbh</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_connect"><span style="color: #990000;">mysql_connect</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pass</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$dbh</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Unable to connect to DB: &quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><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: #339933;">!</span><a href="http://www.php.net/mysql_select_db"><span style="color: #990000;">mysql_select_db</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbname</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbh</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Unable to select <span style="color: #006699; font-weight: bold;">$dbname</span>: &quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$dbh</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> convertTable<span style="color: #009900;">&#40;</span><span style="color: #000088;">$map</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$max_rows</span><span style="color: #339933;">;</span>
&nbsp;
  printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;-------------------------------------------------&quot;</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: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$map</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dest_query'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Running custom query: <span style="color: #006699; font-weight: bold;">{$map['dest_query']}</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Migrating values to <span style="color: #006699; font-weight: bold;">{$map['dest_table']}</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;-------------------------------------------------&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// we call this each time because we can't just store a separate connection for the from instance</span>
  <span style="color: #666666; font-style: italic;">// and to instance... chances are they point to the same server, so php/mysql will re-use the connection</span>
  <span style="color: #666666; font-style: italic;">// and just switch the instance being operated on... this will cause our queries to go on the fritz</span>
  <span style="color: #666666; font-style: italic;">// the upside is that it doesn't have to reconnect since it's reused, so there's no big cost to this approach</span>
  <span style="color: #000088;">$dbout</span> <span style="color: #339933;">=</span> connectToDb<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$map</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'connection_info'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'source'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// select the old data</span>
  <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$map</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'source_query'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbout</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: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Unable to load source data (<span style="color: #006699; font-weight: bold;">{$map['source_query']}</span>): &quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/mysql_num_rows"><span style="color: #990000;">mysql_num_rows</span></a><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;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No rows found, that's weird so I'm exiting&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://www.php.net/exit"><span style="color: #990000;">exit</span></a><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// here again, we call this right before use, because we're going to switch instances, but</span>
  <span style="color: #666666; font-style: italic;">// it's probably to the same mysql server, so connection will get reused</span>
  <span style="color: #000088;">$dbin</span> <span style="color: #339933;">=</span> connectToDb<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$map</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'connection_info'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dest'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// While a row of data exists, put that row in $row as an associative array</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: #000088;">$j</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_fetch_assoc"><span style="color: #990000;">mysql_fetch_assoc</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$vals</span> <span style="color: #339933;">=</span> convertRow<span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #339933;">,</span> <span style="color: #000088;">$map</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'fields'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$map</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'defaults'</span><span style="color: #009900;">&#93;</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: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$map</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dest_query'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      customQuery<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbin</span><span style="color: #339933;">,</span> <span style="color: #000088;">$map</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dest_query'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$vals</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$j</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      addRow<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbin</span><span style="color: #339933;">,</span> <span style="color: #000088;">$map</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'dest_table'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$vals</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$j</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$i</span><span style="color: #339933;">++;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$max_rows</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$max_rows</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// recover query memory</span>
  <a href="http://www.php.net/mysql_free_result"><span style="color: #990000;">mysql_free_result</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// show the user what we did</span>
  printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Converted &quot;</span><span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">-</span><span style="color: #000088;">$j</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; rows&quot;</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;">$j</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;!!!! <span style="color: #006699; font-weight: bold;">$j</span> errors !!!!!!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// actually run the conversions</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tablemap</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$t</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  convertTable<span style="color: #009900;">&#40;</span><span style="color: #000088;">$t</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
printIt<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Finished without blowing up the planet&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://zenovations.com/blog/2009/04/quick-and-dirty-database-conversion-tool/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>phpize: command not found</title>
		<link>http://zenovations.com/blog/2009/04/phpize-command-not-found-2/</link>
		<comments>http://zenovations.com/blog/2009/04/phpize-command-not-found-2/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 18:25:37 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[centos 5]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[pecl]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://zenovations.com/blog/?p=15</guid>
		<description><![CDATA[Ran into this error (phpize: command not found) while trying to install json extension for PHP. Here is the solution on CentOS 5/RHEL: ?View Code BASHyum install php-devel On Debian/Ubuntu: ?View Code BASHapt-get install php5-dev Viola! Why do you have to install dev tools to use extensions? Dunno. Very irritating. But it solves it with [...]]]></description>
			<content:encoded><![CDATA[<p>Ran into this error (phpize: command not found) while trying to install json extension for PHP.</p>
<p>Here is the solution on CentOS 5/RHEL:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p15code25'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1525"><td class="code" id="p15code25"><pre class="bash" style="font-family:monospace;">yum <span style="color: #c20cb9; font-weight: bold;">install</span> php-devel</pre></td></tr></table></div>

<p>On Debian/Ubuntu:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p15code26'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1526"><td class="code" id="p15code26"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">apt-get</span> <span style="color: #c20cb9; font-weight: bold;">install</span> php5-dev</pre></td></tr></table></div>

<p>Viola! Why do you have to install dev tools to use extensions? Dunno. Very irritating. But it solves it with no side effects.</p>
]]></content:encoded>
			<wfw:commentRss>http://zenovations.com/blog/2009/04/phpize-command-not-found-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8220;undefined function mysql_connect()&#8221; While Migrating Virtualmin</title>
		<link>http://zenovations.com/blog/2009/03/undefined-function-mysql_connect-while-migrating-virtualmin/</link>
		<comments>http://zenovations.com/blog/2009/03/undefined-function-mysql_connect-while-migrating-virtualmin/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 05:55:32 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Virtualmin]]></category>

		<guid isPermaLink="false">http://zenovations.com/blog/?p=3</guid>
		<description><![CDATA[While migrating virtualmin to our shiney new CentOS 5  box, I ran into this error on all our PHP sites: If I look at php info, it shows &#8211;with-mysql, but there is no mysql section farther down. It looked like PHP&#8217;s Apache module was having trouble finding the mysql.so file. I immediately thought to check [...]]]></description>
			<content:encoded><![CDATA[<p>While migrating virtualmin to our shiney new CentOS 5  box, I ran into this error on all our PHP sites:</p>
<p>If I look at php info, it shows &#8211;with-mysql, but there is no mysql section farther down. It looked like PHP&#8217;s Apache module was having trouble finding the mysql.so file. I immediately thought to check for the php-mysql module (there is no php5-mysql for CentOS 5, I noted) and found it installed:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p3code29'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p329"><td class="code" id="p3code29"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server apf<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># yum install php-mysql</span>
...
Parsing package <span style="color: #c20cb9; font-weight: bold;">install</span> arguments
Package php-mysql - 5.1.6-<span style="color: #000000;">20</span>.el5_2.1.i386 is already installed.
Nothing to <span style="color: #000000; font-weight: bold;">do</span></pre></td></tr></table></div>

<p>After a short dive into the php.ini settings, I noted that all the restored sites had this in their php.ini file:</p>
<pre>extension_dir=/usr/lib/<span style="color: #ff0000;">php</span><span style="color: #ff0000;">5</span>/modules</pre>
</p>
<p>But that dir doesn&#8217;t exist. Connecting the dots, I realized that CentOS 5 has moved all the PHP 5 data to the default. So the dir is now at <code>/usr/lib/<span style="color: #ff0000;">php</span>/modules</code>. And this solved the rest:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p3code30'); return false;">View Code</a> BASH</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p330"><td class="code" id="p3code30"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server lib<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># ln -s /usr/lib/php /usr/lib/php5</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server lib<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># ls -ld /usr/lib/php*</span>
drwxr-xr-x <span style="color: #000000;">4</span> root root <span style="color: #000000;">4096</span> Jul <span style="color: #000000;">16</span>  <span style="color: #000000;">2008</span> php
lrwxrwxrwx <span style="color: #000000;">1</span> root root    <span style="color: #000000;">3</span> Mar <span style="color: #000000;">30</span> <span style="color: #000000;">21</span>:01 php5 -<span style="color: #000000; font-weight: bold;">&amp;</span>gt; php
<span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>server lib<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># /etc/init.d/httpd restart</span>
Stopping httpd:                                            <span style="color: #7a0874; font-weight: bold;">&#91;</span>  OK  <span style="color: #7a0874; font-weight: bold;">&#93;</span>
Starting httpd:                                            <span style="color: #7a0874; font-weight: bold;">&#91;</span>  OK  <span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></td></tr></table></div>

<p>Yay!</p>
]]></content:encoded>
			<wfw:commentRss>http://zenovations.com/blog/2009/03/undefined-function-mysql_connect-while-migrating-virtualmin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Class for Accessing Twitter API</title>
		<link>http://zenovations.com/blog/2008/09/php-class-for-accessing-twitter-api/</link>
		<comments>http://zenovations.com/blog/2008/09/php-class-for-accessing-twitter-api/#comments</comments>
		<pubDate>Fri, 12 Sep 2008 13:57:01 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://zenovations.com/blog/?p=63</guid>
		<description><![CDATA[I wrote up a quick class for interacting with the twitter API. Right now it returns everything as json, but that would be a five minute project to change out for anyone with m4d sk1&#124;&#124;z0rz. Example usage: Here&#8217;s the function api, which implements most of the commands in the twitter public api: static updateStatus() - update your [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote up a quick class for interacting with the twitter API. Right now it returns everything as json, but that would be a five minute project to change out for anyone with m4d sk1||z0rz.</p>
<p><span id="more-63"></span></p>
<p>Example usage:</p>
<p>Here&#8217;s the function api, which implements most of the commands in the <a href="http://apiwiki.twitter.com/REST+API+Documentation">twitter public api</a>:</p>
<ul>
<li><code>static updateStatus()</code> - update your status on twitter</li>
<li>getUpdates() &#8211; get the last <code>n</code> updates from twitter</li>
<li>getFollowers() &#8211; list your twitter friends (people following you)</li>
<li>getReplies() &#8211; return replies to your posts</li>
<li>getFriends() &#8211; list friends you follow on twitter</li>
<li>sendDirectMessage() &#8211; direct message a friend</li>
<li>etDirectMessages() &#8211; download your direct messages</li>
</ul>
<p>You&#8217;ll want to be sure to set $url_referer to something useful, so you can <a href="http://twitter.com/help/request_source">set the from text</a> for twitter (login required on twitter).</p>
<p>Here&#8217;s the whole <a href="http://zenovations.com/blog/TwitterUtils.phps">TwitterUtils.php</a> class, enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://zenovations.com/blog/2008/09/php-class-for-accessing-twitter-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

