<?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>Kerkness.ca &#187; parse_str</title>
	<atom:link href="http://www.kerkness.ca/tagged/parse_str/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kerkness.ca</link>
	<description>flexing my kerkness, among other things</description>
	<lastBuildDate>Wed, 14 Oct 2009 15:12:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.3</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Forgotten PHP : Some useful and simple PHP features that I often forget about.</title>
		<link>http://www.kerkness.ca/forgotten-php-some-useful-and-simple-php-features-that-i-often-forget-about/</link>
		<comments>http://www.kerkness.ca/forgotten-php-some-useful-and-simple-php-features-that-i-often-forget-about/#comments</comments>
		<pubDate>Fri, 01 May 2009 22:06:49 +0000</pubDate>
		<dc:creator>Kerk</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[number_format]]></category>
		<category><![CDATA[parse_str]]></category>
		<category><![CDATA[sprintf]]></category>
		<category><![CDATA[substr_count]]></category>
		<category><![CDATA[urldecode]]></category>
		<category><![CDATA[urlencode]]></category>
		<category><![CDATA[vsprintf]]></category>

		<guid isPermaLink="false">http://www.kerkness.ca/?p=425</guid>
		<description><![CDATA[I&#8217;ve been programming with PHP for many years but regardless how familiar I am with the language I am constantly looking at reference documentation for reminders on how to do things. Here&#8217;s a list of some really handy PHP functions that I often forget about and then will stumble across them in documentation and am [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been programming with PHP for many years but regardless how familiar I am with the language I am constantly looking at <a href="http://ca.php.net/manual/en/funcref.php" target="_blank">reference documentation</a> for reminders on how to do things. Here&#8217;s a list of some really handy PHP functions that I often forget about and then will stumble across them in documentation and am reminded why I shouldn&#8217;t forget about them.</p>
<h2>How to format text to be inserted into a url</h2>
<p>If you need to pass some text or a dynamic string in a URL use <strong><em>urlencode()</em></strong> and <em><strong>urldecode()</strong></em></p>
<pre>// encoding the string
$str = "Jerry's Bedding Plants &amp; Ice Cream";
$url = 'http://www.example.com/page.php?args=' . urlencode( $str );

// decoding the string in page.php
$str = urldecode( $_GET['args'] );</pre>
<h2>Parsing a query string</h2>
<p>Want to automatically take the query string and dump the values into an array ? Use <em><strong>parse_str()</strong></em> . You can easily get the query string from <strong><em>$_SERVER['QUERY_STRING']</em></strong></p>
<pre name="code" class"php">$query = $_SERVER['QUERY_STRING'];  // foo=bar&amp;bar=foo&amp;cool=me&amp;you=ok
$vars = array();
parse_str( $query, $vars );
print_r( $vars );

/* output
Array
(
  [foo] =&gt; bar
  [bar] =&gt; foo
  [cool] =&gt; me
  [you] =&gt; ok
)
*/</pre>
<h2>Creating a formatted string</h2>
<p>Most people are very familiar with <strong><em>printf()</em></strong> in php but if you&#8217;re like me you often forget about <em><strong>sprintf()</strong></em> which returns your formatted string instead of printing it to the screen.</p>
<pre>$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);</pre>
<p>Another one you might forget about is <em><strong>vsprintf()</strong></em> which is similar to <em><strong>sprintf()</strong></em> but works with an array</p>
<pre name="code" class"php">$data = array( $year, $month, $day );
$isodate = vsprintf("%04d-%02d-%02d", $data );</pre>
<h2>Formatting numbers</h2>
<p>For some reason I always forget the arguments for the PHP <em><strong>number_format()</strong></em> function. The function takes one, two or 4 arguments ( will not accept just 3 arguments ).</p>
<pre name="code" class"php">// string number_format  ( float $number  , int $decimals  , string $dec_point  , string $thousands_sep  )
$number = number_format( '32456.82', 2, '.', ',' );</pre>
<h2>Count the number of times a small string occurs in a large string</h2>
<pre name="code" class"php">// how many times do I say rad;
$str = "I've got some rad moves, super rad moves, I've got rad mad bad not sad rad moves.";
$rad_count = substr_count( $str, 'rad' );</pre>
<p>Happy coding nerds !</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerkness.ca/forgotten-php-some-useful-and-simple-php-features-that-i-often-forget-about/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
