<?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>Hot Koehls &#187; handy functions</title>
	<atom:link href="http://frankkoehl.com/tag/handy-functions/feed/" rel="self" type="application/rss+xml" />
	<link>http://frankkoehl.com</link>
	<description>The more you know, the more you don&#039;t know</description>
	<lastBuildDate>Thu, 10 May 2012 18:34:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>jQuery 1.4 released</title>
		<link>http://frankkoehl.com/2010/01/jquery-14-released/</link>
		<comments>http://frankkoehl.com/2010/01/jquery-14-released/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 16:33:14 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[For techies]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[handy functions]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[standards]]></category>
		<category><![CDATA[usability]]></category>

		<guid isPermaLink="false">http://frankkoehl.com/?p=1118</guid>
		<description><![CDATA[The latest and greatest version of jQuery, version 1.4, was released on January 14, the birthday of jQuery&#8217;s original launch. Bugfixes and improvements abound! The jQuery team has put together a site devoted to the new version, called the 14 days of jQuery, covering the major version changes as well as infrastructure updates coinciding with [...]]]></description>
			<content:encoded><![CDATA[<p>The latest and greatest version of jQuery, version 1.4, was released on January 14, the birthday of jQuery&#8217;s original launch. <a href="http://jquery14.com/day-01/jquery-14">Bugfixes and improvements abound!</a></p>
<p>The jQuery team has put together a site devoted to the new version, called the <a href="http://jquery14.com">14 days of jQuery</a>, covering the major version changes as well as infrastructure updates coinciding with the new release. For example, the documentation site has been completely redesigned, and been moved to it&#8217;s own subdomain home, <a href="http://api.jquery.com">api.jquery.com</a>. Links from the primary jquery.com site should be updated within the next week. With video demos of new features, Q&#038;A&#8217;s with the core team (including founder John Resig), it&#8217;s well-worth checking out for every jQuery developer.</p>
]]></content:encoded>
			<wfw:commentRss>http://frankkoehl.com/2010/01/jquery-14-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Change timezone to GMT in Debian</title>
		<link>http://frankkoehl.com/2009/10/change-timezone-gmt-debian/</link>
		<comments>http://frankkoehl.com/2009/10/change-timezone-gmt-debian/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 21:25:39 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[For techies]]></category>
		<category><![CDATA[handy functions]]></category>
		<category><![CDATA[servers]]></category>

		<guid isPermaLink="false">http://frankkoehl.com/?p=899</guid>
		<description><![CDATA[If you need to change the timezone of a Debian system, your searches will probably tell you to use the tzselect utility. That will only change the timezone for the server temporarily. Use that command and then run this: cat /etc/timezone See how the timezone didn&#8217;t change? Left alone, the system will revert to the [...]]]></description>
			<content:encoded><![CDATA[<p>If you need to change the timezone of a Debian system, your searches will probably tell you to use the <code>tzselect</code> utility. That will only change the timezone for the server temporarily. Use that command and then run this: </p>
<pre>cat /etc/timezone</pre>
<p>See how the timezone didn&#8217;t change? Left alone, the system will revert to the timezone stored in /etc/timezone at next reboot. </p>
<p>Plus, <code>tzselect</code> does not offer the full compliment of possible timezones. I recently needed to change my server to GMT, for example, and <code>tzselect</code> does not include an option for it.</p>
<p>Instead, Debian has a less-publicized tool that will solve both problems&#8230;</p>
<pre>dpkg-reconfigure tzdata</pre>
<p>Run that command and it will permanently change the timezone for the system, and includes options for all sort of obscure timezones. I wouldn&#8217;t consider GMT to be obscure, but maybe that&#8217;s just me.</p>
<p>Took me forever to find this searching earlier today. I don&#8217;t know why this isn&#8217;t more widely noted, but there you go.</p>
]]></content:encoded>
			<wfw:commentRss>http://frankkoehl.com/2009/10/change-timezone-gmt-debian/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Circumvent PHP errors with define_once()</title>
		<link>http://frankkoehl.com/2009/04/define_once/</link>
		<comments>http://frankkoehl.com/2009/04/define_once/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 17:48:24 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[For techies]]></category>
		<category><![CDATA[coding theory]]></category>
		<category><![CDATA[handy functions]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://frankkoehl.com/?p=459</guid>
		<description><![CDATA[Core PHP does not include a define_once() function to complement functions like require_once() and include_once(), which is pretty silly in my opinion. While I am generally not a fan of using *_once statements due to the performance penalty (and incurred laziness), define_once is the exception. There are ways to look for a loaded/missing file, but [...]]]></description>
			<content:encoded><![CDATA[<p>Core PHP does not include a <code>define_once()</code> function to complement functions like <a href="http://php.net/require_once"><code>require_once()</code></a> and <a href="http://www.php.net/manual/en/function.include-once.php"><code>include_once()</code></a>, which is pretty silly in my opinion. While I am generally <a href="http://frankkoehl.com/2008/11/why-include_once-and-require_once-may-make-you-a-crappy-coder/">not a fan of using <code>*_once</code> statements</a> due to the performance penalty (and incurred laziness), <code>define_once</code> is the exception. There are ways to look for a loaded/missing file, but a define is not a define until you define it, so you really have no choice.</p>
<p>So in situations where you have to blindly load defines &mdash; I do it to build language defines in a cascading templating system &mdash; use this function to achieve the proper results:</p>
<pre lang="php">
function define_once($define, $value) {
  if (!defined((string)$define)) {
    define($define, $value);
    return true;
  }
  return false;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://frankkoehl.com/2009/04/define_once/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Find the second (or third, or fourth) occurence in a string</title>
		<link>http://frankkoehl.com/2009/03/second-third-fourth-occurence-string/</link>
		<comments>http://frankkoehl.com/2009/03/second-third-fourth-occurence-string/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 13:53:22 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[For techies]]></category>
		<category><![CDATA[handy functions]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://frankkoehl.com/?p=436</guid>
		<description><![CDATA[PHP includes some handy functions to find the first or last occurrence of a given string token in a string: strpos and strrpos. However these functions are limited to just the first occurrence; what if I want to know the location of the second token&#8217;s position, or the third? These problems usually result in some [...]]]></description>
			<content:encoded><![CDATA[<p>PHP includes some handy functions to find the first or last occurrence of a given string token in a string: <code><a href="http://www.php.net/strpos">strpos</a></code> and <code><a href="http://www.php.net/strrpos">strrpos</a></code>. However these functions are limited to just the first occurrence; what if I want to know the location of the second token&#8217;s position, or the third? These problems usually result in some serious coding acrobatics.</p>
<p>Well no need for code-jitsu anymore. Based almost completely on a post I found at another blog &mdash; which is now down, how&#8217;s that for timing? &mdash; here are two functions which allow you to search for any occurrence of a specific token in a string&#8230; </p>
<pre lang="php">
/**
 * Find position of Nth $occurrence of $needle in $haystack
 * Starts from the beginning of the string
**/
function strpos_offset($needle, $haystack, $occurrence) {
  // explode the haystack
  $arr = explode($needle, $haystack);
  // check the needle is not out of bounds
  switch( $occurrence ) {
    case $occurrence == 0:
      return false;
    case $occurrence > max(array_keys($arr)):
      return false;
    default:
      return strlen(implode($needle, array_slice($arr, 0, $occurrence)));
  }
}

/**
 * Find position of Nth $occurrence of $needle in $haystack
 * Starts from the end of the string
**/
function strrpos_offset($needle, $haystack, $occurrence) {
  // explode the haystack
  $arr = array_reverse(explode($needle, $haystack));
  // check the needle is not out of bounds
  switch( $occurrence ) {
    case $occurrence == 0:
      return false;
    case $occurrence > max(array_keys($arr)):
      return false;
    default:
      $inverted = strlen(implode($needle, array_slice($arr, 0, $occurrence)));
      $actual = (strlen($haystack) - 1) - $inverted;
      return $actual;
  }
}

// look for second occurrence of letter 'a' from the start of string
echo strpos_offset('a', 'abracadabra', 2);
// returns 3

// look for second occurrence of letter 'a' from the end of string
echo strrpos_offset('a', 'abracadabra', 2);
// returns 7
</pre>
<p>In terms of use, we&#8217;ve essentially added an extra argument to <code>strpos</code> and <code>strrpos</code> that specifies which occurrence you&#8217;re looking for. In other words, you can make both of these functions work like the PHP standards by setting the third <code>$occurrence</code> variable to 1.</p>
]]></content:encoded>
			<wfw:commentRss>http://frankkoehl.com/2009/03/second-third-fourth-occurence-string/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

