<?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>Jon Fox &#187; php</title>
	<atom:link href="http://www.jonefox.com/blog/category/code/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jonefox.com/blog</link>
	<description>My rants, ramblings, and random thoughts</description>
	<lastBuildDate>Mon, 16 Jan 2012 14:48:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='www.jonefox.com' port='80' path='/blog/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>PHP str_replace_last function</title>
		<link>http://www.jonefox.com/blog/2011/09/26/php-str_replace_last-function/</link>
		<comments>http://www.jonefox.com/blog/2011/09/26/php-str_replace_last-function/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 06:40:44 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[str_replace last]]></category>

		<guid isPermaLink="false">http://www.jonefox.com/blog/?p=595</guid>
		<description><![CDATA[I recently found myself in need of a way to replace the last (and only the last) occurrence of a substring within a string. Basically exactly str_replace, but only on the last occurrence of the needle within the haystack. I &#8230; <a href="http://www.jonefox.com/blog/2011/09/26/php-str_replace_last-function/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I recently found myself in need of a way to replace the last (and only the last) occurrence of a substring within a string.  Basically exactly str_replace, but only on the last occurrence of the needle within the haystack.  I came up with this quick solution.  Feedback welcome.</p>
<pre>
function str_replace_last( $search, $replace, $subject ) {
    if ( !$search || !$replace || !$subject )
        return false;

    $index = strrpos( $subject, $search );
    if ( $index === false )
        return $subject;

    // Grab everything before occurence
    $pre = substr( $subject, 0, $index );

    // Grab everything after occurence
    $post = substr( $subject, $index );

    // Do the string replacement
    $post = str_replace( $search, $replace, $post );

    // Recombine and return result
    return $pre . $post;
}
</pre>
<p>Hope someone else finds this useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jonefox.com/blog/2011/09/26/php-str_replace_last-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to programmatically find the continent from a given city or state</title>
		<link>http://www.jonefox.com/blog/2011/08/24/how-to-programmatically-find-the-continent-from-a-given-city-or-state/</link>
		<comments>http://www.jonefox.com/blog/2011/08/24/how-to-programmatically-find-the-continent-from-a-given-city-or-state/#comments</comments>
		<pubDate>Thu, 25 Aug 2011 03:23:37 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[continent lookup]]></category>

		<guid isPermaLink="false">http://www.jonefox.com/blog/?p=555</guid>
		<description><![CDATA[I recently ran across a somewhat unusual problem. I needed to find a programatic way to convert a string that could be a city or a state name into the continent that the city or state belongs to. I Googled &#8230; <a href="http://www.jonefox.com/blog/2011/08/24/how-to-programmatically-find-the-continent-from-a-given-city-or-state/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I recently ran across a somewhat unusual problem. I needed to find a programatic way to convert a string that could be a city or a state name into the continent that the city or state belongs to. I Googled around a bit and couldn&#8217;t find an obvious solution, so I thought I&#8217;d write this up in hope of helping the next guy.</p>
<p>What I ended up doing is using the <a title="Yahoo GeoPlanet API" href="http://developer.yahoo.com/geo/geoplanet/guide/api-reference.html#api-summary">Yahoo! GeoPlanet API</a>. This allowed me to do a query of my city or state name and get a place resource, then use that to find the continent that the place resource belongs to.</p>
<p>So first things first, you&#8217;re going to need an app id from Yahoo!, so if you don&#8217;t already have one you&#8217;ll need to <a href="https://developer.apps.yahoo.com/projects/">get one</a>. Then you just need to make a request like the following:<br />
<code>http://where.yahooapis.com/v1/places.q(CITY_OR_STATE)?appid=YOUR_APP_ID</code><br />
Where <code>CITY_OR_STATE</code> is the query term you&#8217;re searching for and <code>YOUR_APP_ID</code> is the Yahoo! app id from above. Then you&#8217;ll get back something like the following:</p>
<pre></pre>
<pre>&lt;places xmlns="http://where.yahooapis.com/v1/schema.rng" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:start="0" yahoo:count="1" yahoo:total="300"&gt;
&lt;place yahoo:uri="http://where.yahooapis.com/v1/place/2488042" xml:lang="en-US"&gt;
&lt;woeid&gt;2488042&lt;/woeid&gt;
&lt;placeTypeName code="7"&gt;Town&lt;/placeTypeName&gt;
&lt;name&gt;San Jose&lt;/name&gt;
&lt;country type="Country" code="US"&gt;United States&lt;/country&gt;
&lt;admin1 type="State" code="US-CA"&gt;California&lt;/admin1&gt;
&lt;admin2 type="County" code=""&gt;Santa Clara&lt;/admin2&gt;
&lt;admin3/&gt;
&lt;locality1 type="Town"&gt;San Jose&lt;/locality1&gt;
&lt;locality2/&gt;
&lt;postal/&gt;
&lt;centroid&gt;
&lt;latitude&gt;37.338470&lt;/latitude&gt;
&lt;longitude&gt;-121.885788&lt;/longitude&gt;
&lt;/centroid&gt;
&lt;boundingBox&gt;
&lt;southWest&gt;
&lt;latitude&gt;37.117840&lt;/latitude&gt;
&lt;longitude&gt;-122.160027&lt;/longitude&gt;
&lt;/southWest&gt;
&lt;northEast&gt;
&lt;latitude&gt;37.555859&lt;/latitude&gt;
&lt;longitude&gt;-121.535393&lt;/longitude&gt;
&lt;/northEast&gt;
&lt;/boundingBox&gt;
&lt;areaRank&gt;6&lt;/areaRank&gt;
&lt;popRank&gt;12&lt;/popRank&gt;
&lt;/place&gt;
&lt;/places&gt;</pre>
<p>You want to grab the <code>woeid</code> (in this case 2488042) for the next part. Next, we need to lookup the continent this place resource belongs to. To do this we make a request like so:<br />
<code>http://where.yahooapis.com/v1/place/THE_WOEID/belongtos.type(29)?appid=YOUR_APP_ID</code><br />
Where <code>THE_WOEID</code> is the woeid we found in the last step (2488042 in this example) and <code>YOUR_APP_ID</code> is your app id again. This should return something like the following:</p>
<pre>
&lt;places xmlns="http://where.yahooapis.com/v1/schema.rng" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:start="0" yahoo:count="1" yahoo:total="1"&gt;
&lt;place yahoo:uri="http://where.yahooapis.com/v1/place/24865672" xml:lang="en-US"&gt;
&lt;woeid&gt;24865672&lt;/woeid&gt;
&lt;placeTypeName code="29"&gt;Continent&lt;/placeTypeName&gt;
&lt;name&gt;North America&lt;/name&gt;
&lt;/place&gt;
&lt;/places&gt;
</pre>
<p>Where <code><name>North America</name></code> is clearly what we want to pull out of it.</p>
<p>I was doing this in PHP, so here&#8217;s a quick and dirty PHP example:</p>
<pre>
function continent_lookup( $search ) {
    if ( strlen( $search ) == 0 )
        return false;

    $search = urlencode( $search );

    $app_id = 'YOUR_APP_ID';

    $res = file_get_contents( "http://where.yahooapis.com/v1/places.q($search)?appid=$app_id" );
    preg_match( "/&lt;woeid&gt;([0-9]+?)&lt;\/woeid&gt;/", $res, $match );

    if ( !$match )
        return false;

    $woeid = $match[1];

    $res = file_get_contents( "http://where.yahooapis.com/v1/place/$woeid/belongtos.type(29)?appid=$app_id" );
    preg_match( "/&lt;name&gt;(.+?)&lt;\/name&gt;/", $res, $match );

    return $match[1];
}
</pre>
<p>Hope this helps someone else out there. Let me know if you have any feedback.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jonefox.com/blog/2011/08/24/how-to-programmatically-find-the-continent-from-a-given-city-or-state/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How To Detect Transparency In PNG Images</title>
		<link>http://www.jonefox.com/blog/2011/04/15/how-to-detect-transparency-in-png-images/</link>
		<comments>http://www.jonefox.com/blog/2011/04/15/how-to-detect-transparency-in-png-images/#comments</comments>
		<pubDate>Fri, 15 Apr 2011 14:29:52 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[detect]]></category>
		<category><![CDATA[detection]]></category>
		<category><![CDATA[png]]></category>
		<category><![CDATA[transparency]]></category>

		<guid isPermaLink="false">http://www.jonefox.com/blog/?p=484</guid>
		<description><![CDATA[I recently came across a problem where I needed to check for transparency in PNG images using PHP. After some digging with the help of my partner Josh, we came up with the following php function: function png_has_transparency( $filename ) &#8230; <a href="http://www.jonefox.com/blog/2011/04/15/how-to-detect-transparency-in-png-images/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I recently came across a problem where I needed to check for transparency in PNG images using PHP.  After some digging with the help of my partner Josh, we came up with the following php function:</p>
<pre>
function png_has_transparency( $filename ) {
    if ( strlen( $filename ) == 0 || !file_exists( $filename ) )
        return false;

    if ( ord ( file_get_contents( $filename, false, null, 25, 1 ) ) &amp; 4 )
        return true;

    $contents = file_get_contents( $filename );
    if ( stripos( $contents, 'PLTE' ) !== false &amp;&amp; stripos( $contents, 'tRNS' ) !== false )
        return true;

    return false;
}</pre>
<p>The first file check:<br />
<code>ord ( file_get_contents( $filename, false, null, 25, 1 ) ) &amp; 4</code><br />
will check for transparency in a 32-bit PNG.  The second check<br />
<code>stripos( $contents, 'PLTE' ) !== false &amp;&amp; stripos( $contents, 'tRNS' ) !== false</code><br />
 works to detect transparency in 8-bit PNG&#8217;s.  Big thanks to <a href="http://camendesign.com/code/uth1_is-png-32bit">this article</a> which expains how these parts work in a bit more detail.</p>
<p>Depending on how likely you are to have transparency (or 32-bit png&#8217;s) you might want to modify this function to only do one file access, but the advantage here is that if it is a 32-bit png with transparency we only need to read in part of the file.  This was faster in my early testing of a sample of our images, but your results may vary.</p>
<p>Hope this helps someone else out there.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jonefox.com/blog/2011/04/15/how-to-detect-transparency-in-png-images/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>One time use email addresses</title>
		<link>http://www.jonefox.com/blog/2011/01/02/one-time-use-email-addresses/</link>
		<comments>http://www.jonefox.com/blog/2011/01/02/one-time-use-email-addresses/#comments</comments>
		<pubDate>Sun, 02 Jan 2011 23:22:21 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Random Ideas]]></category>
		<category><![CDATA[email]]></category>

		<guid isPermaLink="false">http://jonefox.com/blog/?p=463</guid>
		<description><![CDATA[Recently I threw together a tool to simplify my sign up on other services.  It&#8217;s a script that allows me to use an email address one time, and then ignore all future emails to that address.  I often use it &#8230; <a href="http://www.jonefox.com/blog/2011/01/02/one-time-use-email-addresses/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Recently I threw together a tool to simplify my sign up on other services.  It&#8217;s a script that allows me to use an email address one time, and then ignore all future emails to that address.  I often use it for signing up to new services so that I can get the confirmation email, but not get a bunch of spam / bacon I don&#8217;t really care about.  I set it up with a custom domain so that I can use anything at that domain as an email address.  For example, test1@mycustomdomain.com, test2@mycustomdomain.com, and anotherdumbemail@mycustomdomain.com.</p>
<p>The interesting part of it, though, is that the emails all dump to a PHP script.  This script looks at the email address it was sent to, and checks a database to see if that email address has been seen before or not.  If it hasn&#8217;t, it will forward that email to my &#8220;real&#8221; email address.  If it has, it will ignore it.</p>
<p>This has been super helpful when making test accounts on services too as it&#8217;s easy to get another email address, but I don&#8217;t have to worry about checking a weird email account or getting a bunch of spam afterwards.  Just thought I&#8217;d mention it in case anyone else out there does a lot of this stuff.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jonefox.com/blog/2011/01/02/one-time-use-email-addresses/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Lorem Ipsum Post Generator WordPress Plugin</title>
		<link>http://www.jonefox.com/blog/2009/08/30/lorem-ipsum-post-generator-wordpress-plugin/</link>
		<comments>http://www.jonefox.com/blog/2009/08/30/lorem-ipsum-post-generator-wordpress-plugin/#comments</comments>
		<pubDate>Sun, 30 Aug 2009 21:39:59 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[lorem ipsum]]></category>
		<category><![CDATA[test posts]]></category>
		<category><![CDATA[wordpress plugin]]></category>

		<guid isPermaLink="false">http://jonefox.com/blog/?p=194</guid>
		<description><![CDATA[I&#8217;m constantly working with test blogs for IntenseDebate. Often, I need new posts and/or comments to give me some data to play with while testing new features, debugging old ones, or just to show something off. Instead of creating endless &#8230; <a href="http://www.jonefox.com/blog/2009/08/30/lorem-ipsum-post-generator-wordpress-plugin/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m constantly working with test blogs for <a href="http://intensedebate.com">IntenseDebate</a>.  Often, I need new posts and/or comments to give me some data to play with while testing new features, debugging old ones, or just to show something off.  Instead of creating endless posts and comments of &#8220;testing 123&#8243; and &#8220;another random test post&#8221;, I decided to throw together a quick WordPress plugin that will generate the posts for me using <a href="http://lipsum.com/">Lorem Ipsum</a> text.  It has a couple options, mainly the number of posts to create, a min and max number of paragraphs per post, and a min and max number of comments per post.  I find it really useful for generating some test content quickly.  I&#8217;ve posted a screenshot below.  It&#8217;s nothing beautiful, but I thought it might be useful to someone else out there.  I&#8217;ve posted the plugin <a href="http://downloads.wordpress.org/plugin/lorem-ipsum-post-generator.zip">here</a> if you want to check it out.<br />
<img class="size-medium wp-image-195" title="Lorem Ipsum Post Generator Plugin Screenshot" src="http://jonefox.com/blog/wp-content/uploads/2009/08/loremipsum-screen-300x192.jpg" alt="Screenshot of the Lorem Ipsum Post Generate Plugin" width="300" height="192" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jonefox.com/blog/2009/08/30/lorem-ipsum-post-generator-wordpress-plugin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ubiquity: PHP function lookup</title>
		<link>http://www.jonefox.com/blog/2008/09/22/ubiquity-php-function-lookup/</link>
		<comments>http://www.jonefox.com/blog/2008/09/22/ubiquity-php-function-lookup/#comments</comments>
		<pubDate>Tue, 23 Sep 2008 03:17:15 +0000</pubDate>
		<dc:creator>jon</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Ubiquity]]></category>

		<guid isPermaLink="false">http://jonefox.com/blog/?p=72</guid>
		<description><![CDATA[So I guess I&#8217;m getting into this Ubiquity thing a bit. I&#8217;ve created another one to lookup php functions on php.net. Simply subscribe to the command here to add it to your command list. Once you&#8217;ve got the command installed &#8230; <a href="http://www.jonefox.com/blog/2008/09/22/ubiquity-php-function-lookup/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>So I guess I&#8217;m getting into this Ubiquity thing a bit.  I&#8217;ve created another one to lookup php functions on <a href="http://www.php.net">php.net</a>.  Simply subscribe to the command <a href="http://jonefox.com/ubiquity-php.htm">here</a> to add it to your command list.</p>
<p>Once you&#8217;ve got the command installed you can lookup PHP functions by simply opening Ubiquity (CTRL+SPACE) and typing &#8220;php [function]&#8220;.  This one was pretty simple to do, but very useful already.  I look up syntax for PHP functions several times a day and saving my &#8220;Google step&#8221; is really nice.</p>
<p>Let me know what you think.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jonefox.com/blog/2008/09/22/ubiquity-php-function-lookup/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

