<?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>Steve Taylor &#187; miscellaneous</title>
	<atom:link href="http://sltaylor.co.uk/blog/category/miscellaneous/feed/" rel="self" type="application/rss+xml" />
	<link>http://sltaylor.co.uk</link>
	<description>Freelance WordPress developer in London - XHTML, CSS &#38; design</description>
	<lastBuildDate>Mon, 19 Jul 2010 09:39:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Formatting a string with optional elements</title>
		<link>http://sltaylor.co.uk/blog/formatting-a-string-with-optional-elements/</link>
		<comments>http://sltaylor.co.uk/blog/formatting-a-string-with-optional-elements/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 15:30:17 +0000</pubDate>
		<dc:creator>Steve Taylor</dc:creator>
				<category><![CDATA[miscellaneous]]></category>

		<guid isPermaLink="false">http://sltaylor.co.uk/?p=281</guid>
		<description><![CDATA[I&#8217;ve often found myself writing little bits of code to format strings with optional elements. The classic example is outputting the company someone works for, together with their position in that company&#8212;if the position is given. The format if both are given is: [position], [company] If the position isn&#8217;t given, you just output the company. [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve often found myself writing little bits of code to format strings with optional elements. The classic example is outputting the company someone works for, together with their position in that company&#8212;if the position is given. The format if both are given is:</p>
<p><i>[position], [company]</i></p>
<p>If the position isn&#8217;t given, you just output the company. Without, obviously, the comma. It&#8217;s a trivial thing to code, but on a current project I&#8217;ve found myself doing it in several different places. Of course, this suggests there should be a function written for it.</p>
<p>Ideally the function should be able to cope with any number of optional elements. With PHP&#8217;s <a href="http://www.php.net/manual/en/function.func-get-args.php"><code>func_get_args()</code></a>, it&#8217;s easy:</p>
<pre name="code" class="php">function formatOptionalText( $delimiter ) {
	$elements = array_slice( func_get_args(), 1 );
	$text = "";
	$i = 0;
	foreach ( $elements as $element ) {
		if ( trim ( $element ) ) {
			if ( $i &#038;&#038; $text ) $text .= $delimiter;
			$text .= $element;
		}
		$i++;
	}
	return $text;
}</pre>
<p>Note that you have to pass a delimiter first, and you should include a space if necessary (e.g. <code>", "</code>). After that, pass any number of strings and they&#8217;ll be output without gaps.</p>
]]></content:encoded>
			<wfw:commentRss>http://sltaylor.co.uk/blog/formatting-a-string-with-optional-elements/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Welcome</title>
		<link>http://sltaylor.co.uk/blog/welcome/</link>
		<comments>http://sltaylor.co.uk/blog/welcome/#comments</comments>
		<pubDate>Tue, 16 Jan 2007 13:18:12 +0000</pubDate>
		<dc:creator>Steve Taylor</dc:creator>
				<category><![CDATA[miscellaneous]]></category>

		<guid isPermaLink="false">http://sltaylor.co.uk/?p=4</guid>
		<description><![CDATA[This is my work blog, which I&#8217;ll mostly be using (initially at least) to document various tricks, tips, workarounds, hacks and other sundry discoveries I make doing web work. I don&#8217;t expect it to be too busy. But when I solve a particularly tricky coding problem, or discover something tremendously useful, I find it a [...]]]></description>
			<content:encoded><![CDATA[<p>This is my work blog, which I&#8217;ll mostly be using (initially at least) to document various tricks, tips, workarounds, hacks and other sundry discoveries I make doing web work.</p>
<p>I don&#8217;t expect it to be too busy. But when I solve a particularly tricky coding problem, or discover something tremendously useful, I find it a good practice to document it on the webâ€”both for my own future reference and for other poor souls struggling with the same thing.</p>
<p>Comments will be closed because I don&#8217;t expect much here apart from spam! There&#8217;s always the <a href="/contact/">contact form</a> if you want to respond to something&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://sltaylor.co.uk/blog/welcome/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
