<?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>The Q &#187; PHP</title>
	<atom:link href="http://www.mc2design.com/blog/category/php/feed" rel="self" type="application/rss+xml" />
	<link>http://www.mc2design.com/blog</link>
	<description>Web development and marketing from the squares at MC² Design Group</description>
	<lastBuildDate>Tue, 16 Feb 2010 22:53:36 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Random Number in ExpressionEngine With &amp; Without&#160;PHP</title>
		<link>http://www.mc2design.com/blog/random-in-expressionengine-with-without-php</link>
		<comments>http://www.mc2design.com/blog/random-in-expressionengine-with-without-php#comments</comments>
		<pubDate>Wed, 08 Apr 2009 20:49:09 +0000</pubDate>
		<dc:creator>Jarrett M. Barnett</dc:creator>
				<category><![CDATA[ExpressionEngine]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[images]]></category>

		<guid isPermaLink="false">http://www.mc2design.com/blog/?p=256</guid>
		<description><![CDATA[The rand() function in PHP is a quick and easy way to have a nice random number generated. Without it, faking randomization can quickly become cumbersome. After all, a random number has so many uses. Maybe you want to display a random testimonial on each page load, a featured blog post, or even use a [...]]]></description>
			<content:encoded><![CDATA[<p>The rand() function in PHP is a quick and easy way to have a nice random number generated. Without it, faking randomization can quickly become cumbersome. After all, a random number has so many uses. Maybe you want to display a random testimonial on each page load, a featured blog post, or even use a different stylesheet.&nbsp;</p>
<p>To get a random number with ExpressionEngine, we can go the PHP route, or the non-PHP route. Going the PHP route is obviously easier to output and write the markup for (not to mention, is truely &#8220;random&#8221;), but enabling PHP in your templates may not be your answer; especially if you don&#8217;t want users [who have the ability to create/edit blog posts] have the power to do anything that PHP would allow them to do (such as outputting normally unobtainable data from the&nbsp;database).</p>
<p>Unfortunately, in ExpressionEngine, a &#8220;random number&#8221; expression has yet to exist; however, one of the nice things about ExpressionEngine is the ability to process PHP at the flip of a switch.&nbsp;</p>
<p>The PHP Solution:<br />
First, turn on PHP processing in: Templates (tab) > Template Preferences Manager (link) > Allow PHP (dropdown)</p>
<p>Then we simply assign the rand() to a variable, and check against that variable to output&nbsp;accordingly.</p>
<pre class="brush: php">
&lt;?php 

$random_number = rand(0,6); 

if ($random_number == 1) { echo &#039;&lt;img src=&quot;/assets/images/image1.png&quot; /&gt;&#039;; }
elseif ($random_number == 2) { echo &#039;&lt;img src=&quot;/assets/images/image2.png&quot; /&gt;&#039;; }
elseif ($random_number == 3) { echo &#039;&lt;img src=&quot;/assets/images/image3.png&quot; /&gt;&#039;; }
elseif ($random_number == 4) { echo &#039;&lt;img src=&quot;/assets/images/image4.png&quot; /&gt;&#039;; }
elseif ($random_number == 5) { echo &#039;&lt;img src=&quot;/assets/images/image5.png&quot; /&gt;&#039;; }
elseif ($random_number == 6) { echo &#039;&lt;img src=&quot;/assets/images/image6.png&quot; /&gt;&#039;; }

// ensures an image is displayed even when no other conditions are met (in this case, it really only displays when random_number = 0)
else { echo &#039;&lt;img src=&quot;\/assets\/images\/image0.png&quot; \/&gt;&#039;; } 

?&gt;
</pre>
<p>For those that prefer to avoid PHP, here&#8217;s a quick way to (in a way) fake a random number. I figured a good way to do this was using the {current_time}, but only the seconds portion. Granted, its not the most elegant solution, but it gets the job&nbsp;done.</p>
<p>The Non-PHP&nbsp;Solution:</p>
<pre class="brush: php">
{!-- Sets current time (seconds) to the variable &#039;current_time_seconds&#039; --}
{assign_variable:current_time_seconds=&quot;{current_time format=&quot;%s&quot;}&quot;}

{!-- %s = Seconds range: 00-59 --}
{!-- Check current time in seconds and displays a different image based on current time in seconds --}

{!-- displays if 1-9 --}
{if {current_time_seconds} &gt; 0 &amp;&amp; {current_time_seconds} &lt; 10}
&lt;img src=&quot;/assets/images/image1.png&quot; /&gt;

{!-- displays if 11-19 --}
{if:elseif {current_time_seconds} &gt; 10 &amp;&amp; {current_time_seconds} &lt; 20}
&lt;img src=&quot;/assets/images/image2.png&quot; /&gt;

{!-- displays if 21-29 --}
{if:elseif {current_time_seconds} &gt; 20 &amp;&amp; {current_time_seconds} &lt; 30}
&lt;img src=&quot;/assets/images/image3&quot; /&gt;

{!-- displays if 31-39 --}
{if:elseif {current_time_seconds} &gt; 30 &amp;&amp; {current_time_seconds} &lt; 40}
&lt;img src=&quot;/assets/images/image4.png&quot; /&gt;

{!-- displays if 41-49 --}
{if:elseif {current_time_seconds} &gt; 40 &amp;&amp; {current_time_seconds} &lt; 50}
&lt;img src=&quot;/assets/images/image5.png&quot; /&gt;

{!-- displays if 51-59 --}
{if:elseif {current_time_seconds} &gt; 50 &amp;&amp; {current_time_seconds} &lt; 60}
&lt;img src=&quot;/assets/images/image6.png&quot; /&gt;

{!-- displays if seconds don&#039;t match any of the conditions above (basically the following times in seconds: 00,10,20,30,40,50) --}
{if:else}
&lt;img src=&quot;/assets/images/image0.png&quot; /&gt;

{/if}
</pre>
<p>The non-PHP solution can be a great solution for someone who doesn&#8217;t want a random image &#8220;every single time&#8221; (for the visitors who are clicking through pages within seconds of&nbsp;eachother).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mc2design.com/blog/random-in-expressionengine-with-without-php/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Widont for the Zend Framework &#8211; automatically remedies text&#160;widows</title>
		<link>http://www.mc2design.com/blog/widont-for-the-zend-framework-automatically-remedies-text-widows</link>
		<comments>http://www.mc2design.com/blog/widont-for-the-zend-framework-automatically-remedies-text-widows#comments</comments>
		<pubDate>Wed, 10 Sep 2008 17:01:21 +0000</pubDate>
		<dc:creator>Luke Visinoni</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://www.mc2design.com/blog/?p=176</guid>
		<description><![CDATA[When I came across Shaun Inman&#8217;s clever Widon&#8217;t wordpress plug-in this morning, I thought to myself &#8220;What the heck is an unwanted browser window?&#8221;, as I read on I realized it said
&#8220;widow&#8221;. If you have never heard of a widow, you&#8217;ve likely never worked in the print industry. On the web, most of us have [...]]]></description>
			<content:encoded><![CDATA[<p>When I came across Shaun Inman&#8217;s clever <a href="http://www.shauninman.com/archive/2007/01/03/widont_2_1_wordpress_plugin">Widon&#8217;t</a> wordpress plug-in this morning, I thought to myself &#8220;What the heck is an unwanted browser window?&#8221;, as I read on I realized it said<br />
&#8220;widow&#8221;. If you have never heard of a widow, you&#8217;ve likely never worked in the print industry. On the web, most of us have gotten rather sloppy when it comes to good typography, but in our defense, our medium doesn&#8217;t really lend itself to good practices. A widow is a lone word on the end of a header or paragraph. The header of this post would have had a widow, had I not used Shaun&#8217;s plugin (for a better explanation of a widow, refer to <a href="http://www.shauninman.com/archive/2006/08/22/widont_wordpress_plugin">this article</a>).</p>
<p>Since I&#8217;ve been using the Zend Framework so much lately, I figured why not add this functionality into it? Here&#8217;s a view helper to do just that.&nbsp;Enjoy!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Zend_View_Helper_Widont <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$view</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> widont<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$escape</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>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$escape</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">escape</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'|([^\s])\s+([^\s]+)\s*$|'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'$1&amp;nbsp;$2'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setView<span style="color: #009900;">&#40;</span><span style="color: #000088;">$view</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$view</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Refer to the <a href="http://framework.zend.com/manual/en/zend.view.helpers.html">Zend Framework&#8217;s view helper documentation</a> if you don&#8217;t know how to install view&nbsp;helpers.</p>
<p>To use this helper, call $this->widont($str) from inside of a view. If you pass a true value as the second argument, the string will be escaped before being&nbsp;output.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mc2design.com/blog/widont-for-the-zend-framework-automatically-remedies-text-widows/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP Csv Utilities &#8211; new version coming&#160;soon!</title>
		<link>http://www.mc2design.com/blog/php-csv-utilities-new-version-coming-soon</link>
		<comments>http://www.mc2design.com/blog/php-csv-utilities-new-version-coming-soon#comments</comments>
		<pubDate>Tue, 29 Jul 2008 17:27:00 +0000</pubDate>
		<dc:creator>Luke Visinoni</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP CSV Utilities]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.mc2design.com/blog/?p=147</guid>
		<description><![CDATA[It has been months since there was any kind of update on PHP Csv Utilities (or even the blog for that matter). I released the last version (0.2) prematurely, which resulted in a poor release and a lot of bugs. I&#8217;d like to let anybody interested in the library know that I am working on [...]]]></description>
			<content:encoded><![CDATA[<p>It has been months since there was any kind of update on <a href="http://code.google.com/p/php-csv-utils/">PHP Csv Utilities</a> (or even the blog for that matter). I released the last version (0.2) prematurely, which resulted in a poor release and a lot of bugs. I&#8217;d like to let anybody interested in the library know that I am working on version 0.3 right now as well as a much improved build / release process. If there are any bugs or issues you have with the library please leave a comment or shoot me an email and I&#8217;ll do my best to make sure it&#8217;s addressed.&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mc2design.com/blog/php-csv-utilities-new-version-coming-soon/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Zend Framework version 1.5 officially&#160;released</title>
		<link>http://www.mc2design.com/blog/zend-framework-version-15-officially-released</link>
		<comments>http://www.mc2design.com/blog/zend-framework-version-15-officially-released#comments</comments>
		<pubDate>Fri, 21 Mar 2008 18:18:00 +0000</pubDate>
		<dc:creator>Luke Visinoni</dc:creator>
				<category><![CDATA[Industry News]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://www.mc2design.com/blog/zend-framework-version-15-officially-released</guid>
		<description><![CDATA[The Zend Framework has always been my favorite PHP framework. The thing I love about this framework is that its components are loosely coupled. That is to say that its components have well-defined, and well-thought out dependencies. If you don&#8217;t like certain components, you don&#8217;t have to use them. Other frameworks boast this kind of [...]]]></description>
			<content:encoded><![CDATA[<p>The Zend Framework has always been my favorite PHP framework. The thing I love about this framework is that its components are loosely coupled. That is to say that its components have well-defined, and well-thought out dependencies. If you don&#8217;t like certain components, you don&#8217;t have to use them. Other frameworks boast this kind of modularity, but honestly I haven&#8217;t seen any that really back it up. For our last two or three PHP projects, we gave CakePHP a try. At first I was really happy with cake because of how quickly I was able to wire frame an application, but the more I use it the more I realize that the components in cake are far too coupled and there is just too damn much &#8220;auto-magic&#8221; going on in cake. I really prefer explicit to implicit&nbsp;code.</p>
<p>The reason we decided to leave Zend and go to CakePHP was because it lacked two main components that made it very difficult to wire frame applications quickly and easily. Those components are a <a href="http://framework.zend.com/manual/en/zend.layout.html">layout system</a>, and a <a href="http://framework.zend.com/manual/en/zend.form.html">form manager</a>. I am happy to announce that Zend has tackled both of these problems in this release, and has actually done a decent job on them. Neither of them are 100% yet (at least not in my opinion), but both are very usable and helpful at the very least. Go grab a copy of <a href="http://framework.zend.com/download">the new release</a> and give it a go. I think you&#8217;ll be quite happy with&nbsp;it!</p>
<p>For a full list of the new features, check out <a href="http://devzone.zend.com/article/3270-Zend-Technologies-Releases-Zend-Framework-1.5">the official release page on zend developer&nbsp;zone</a>.0</p>
<p><strong>UPDATE!</strong> &#8211; Zend Developer Zone has published <a href="http://www.zend.com/webinar/Framework/webinar-ZF-Layout-20080319.flv">a webinar on Zend_View and Zend_Layout by Ralph Schindler</a>. Go check it&nbsp;out!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mc2design.com/blog/zend-framework-version-15-officially-released/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP CSV Utilities v0.2 released &#8211; now able to detect the format of a csv&#160;file</title>
		<link>http://www.mc2design.com/blog/php-csv-utilities-v02-has-been-released</link>
		<comments>http://www.mc2design.com/blog/php-csv-utilities-v02-has-been-released#comments</comments>
		<pubDate>Sun, 16 Mar 2008 07:30:52 +0000</pubDate>
		<dc:creator>Luke Visinoni</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP CSV Utilities]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.mc2design.com/blog/php-csv-utilities-v02-has-been-released</guid>
		<description><![CDATA[Download PHP CSV Utililties v0.2
Read Documentation for PHP CSV Utilities
I have just wrapped up version 0.2 of our csv library. It includes several new features. The most exciting of which is the new Csv_Sniffer&#160;class.

Csv_Sniffer
sniff(string $sample)
Csv_Sniffer&#8217;s sniff method accepts a sample of csv data and attempts to deduce its format. In this library, there is a [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://code.google.com/p/php-csv-utils/downloads/list">Download PHP CSV Utililties v0.2</a><br />
<a href="http://code.google.com/p/php-csv-utils/wiki/Documentation">Read Documentation for PHP CSV Utilities</a></p>
<p>I have just wrapped up version 0.2 of <a href="http://www.mc2design.com/blog/php-csv-utilities-a-php-library-similar-to-pythons-standard-csv-module">our csv library</a>. It includes several new features. The most exciting of which is the new Csv_Sniffer&nbsp;class.</p>
<p><span&nbsp;id="more-124"></span></p>
<h3>Csv_Sniffer</h3>
<p><strong>sniff(string $sample)</strong><br />
Csv_Sniffer&#8217;s sniff method accepts a sample of csv data and attempts to deduce its format. In this library, there is a class called Csv_Dialect, which tells Csv_Reader and Csv_Writer the format they should read and write in. If Csv_Sniffer::sniff is successful, it will return a Csv_Dialect object representing the format of the csv file (or at least it&#8217;s best guess). You can then pass this dialect to Csv_Reader and it will know how to read the file. You can also pass it to Csv_Writer if you need to append the file or write one in the same format.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$sample</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array_slice</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./data/products.csv'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 0<span style="color: #339933;">,</span> 20<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// grab 20 lines</span>
    <span style="color: #000088;">$sniffer</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Csv_Sniffer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$dialect</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$sniffer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sniff</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sample</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$reader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Csv_Reader<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./data/products.csv'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dialect</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Csv_Exception_CannotDetermineDialect <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;p&gt;<span style="color: #009933; font-weight: bold;">%s</span>&lt;/p&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><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: #009900;">&#125;</span></pre></div></div>

<p><strong>hasHeader(string $sample)</strong><br />
Csv_Sniffer&#8217;s hasHeader method accepts a sample of csv data and attempts to detect if the file has a header row or not. If so it will return true.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$sample</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array_slice</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./data/products.csv'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 0<span style="color: #339933;">,</span> 20<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// grab 20 lines</span>
<span style="color: #000088;">$sniffer</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Csv_Sniffer<span style="color: #009900;">&#40;</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;">$sniffer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasHeader</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sample</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The file probably has a header&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>
    <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;The file probably doesn't have a header&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Csv_Reader_String</h3>
<p>This new class is exactly the same as Csv_Reader, except instead of accepting a filename and reading from a file, it reads directly from a string. This could be useful if for some reason somebody had stored csv data in a database and you were retrieving it from there, or if you needed to collect submitted csv data directly from a web&nbsp;form.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'csv_data'</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>
    <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'csv_data'</span><span style="color: #009900;">&#93;</span>
    <span style="color: #000088;">$reader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Csv_Reader_String<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</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;">$reader</span> <span style="color: #b1b100;">as</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;">// now you could insert it into a database or whatever else you need to do with it</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Csv_Dialect::__construct([array&nbsp;$options])</strong></p>
<p>You may now pass an associative array to Csv_Dialect&#8217;s constructor to override any of it&#8217;s properties. While this doesn&#8217;t actually provide any new features, it definitely is a&nbsp;convenience.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dialect</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Csv_Dialect<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'quotechar'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'escapechar'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'quoting'</span> <span style="color: #339933;">=&gt;</span> Csv_Dialect<span style="color: #339933;">::</span><span style="color: #004000;">QUOTE_NONNUMERIC</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$reader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Csv_Reader<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./data/orders.csv'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dialect</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Plans for version&nbsp;0.3</h3>
<ul>
<li><strong>Csv_Writer will write immediately, rather than when you call close()</strong> &#8211; This won&#8217;t change the interface at all, but in the next version, instead of writing to disk when the user calls close(), it will write immediately when writeRow() or writeRows() is called.</li>
<li><strong>Interface changes for Csv_Sniffer</strong> &#8211; I don&#8217;t like how you have to pass the same sample data to both sniff() and hasHeader(). I will probably change it to accept $sample in its constructor instead. Another issue I have with it is that if there is a tie between delimiter characters in the sniff method, it just chooses by ascii order. I would like to allow the user to specify an array of characters in order of priority in case of a tie.</li>
<li><strong>A more advanced unit testing interface</strong> &#8211; The unit tests I have written are all run at once and since they are reading / writing actual csv files it is beginning to take a while to run them all. I&#8217;m putting together an interface that will allow me to run tests seperately or all together as well as a way to time some operations so that I can speed them up as much as possible.</li>
<li><strong>Csv_Dialect classes</strong> for any and all formats I can dig up (Open Office, Miva Merchant, Google Docs and Spreadsheets, <a href="http://en.wikipedia.org/wiki/Comma-separated_values">standard csv</a>?,  etc.)</li>
<li><strong>Csv_Mapper</strong> &#8211; A class that maps keys to columns so that you can access them like $row['first_name'].</li>
<li><strong>Even more documentation</strong> I have written <a href="http://code.google.com/p/php-csv-utils/wiki/Documentation">some documentation</a> on the google code wiki, but I am planning on writing more consistent docs. The ones I have now are all sort of willy-nilly.</li>
<li><strong>Csv_Reader_Zip</strong> &#8211; A csv reader that can read zipped files</li>
<li><strong>Character encoding</strong> &#8211; This will be the first time I have really had to deal with multiple character encodings, so this may take me a while. I will need to do some research on the subject.</li>
<li><strong>More to come</strong> &#8211; I will finish writing about the new features and complete the docs within the next week or so, for I am tired and I&#8217;m going to bed.</li>
</ul>
<p><a href="http://code.google.com/p/php-csv-utils/downloads/list">Download PHP CSV Utililties v0.2</a><br />
<a href="http://code.google.com/p/php-csv-utils/wiki/Documentation">Read Documentation for PHP CSV Utilities</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mc2design.com/blog/php-csv-utilities-v02-has-been-released/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Cut down on temporary variables in PHP with&#160;Fluidics</title>
		<link>http://www.mc2design.com/blog/cut-down-on-temporary-variables-in-php-with-fluidics</link>
		<comments>http://www.mc2design.com/blog/cut-down-on-temporary-variables-in-php-with-fluidics#comments</comments>
		<pubDate>Sat, 15 Mar 2008 03:43:24 +0000</pubDate>
		<dc:creator>Luke Visinoni</dc:creator>
				<category><![CDATA[Industry News]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.mc2design.com/blog/cut-down-on-temporary-variables-in-php-with-fluidics</guid>
		<description><![CDATA[Ollie Saunders, a colleague of mine and a regular at the DevNetwork forums has put together a very slick little set of functions he has collectively termed &#8220;PHP Fluidics&#8221;. If there is one thing that really sucks about PHP, it&#8217;s how often you have to use temporary variables to get to methods or array elements [...]]]></description>
			<content:encoded><![CDATA[<p>Ollie Saunders, a colleague of mine and a regular at the <a href="http://www.devnetwork.net">DevNetwork forums</a> has put together a very slick little set of functions he has collectively termed &#8220;PHP Fluidics&#8221;. If there is one thing that really sucks about PHP, it&#8217;s how often you have to use temporary variables to get to methods or array elements you need. Fluidics makes this process (and several others) much easier. We use this library in just about everything we code these days and I&#8217;d like to point out a few reasons&nbsp;why.</p>
<p><span&nbsp;id="more-97"></span></p>
<p>One common example is when you need to instantiate an object and call a method on it. Normally, you&#8217;d have to do something like the&nbsp;following.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$person</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MC2_Person<span style="color: #009900;">&#40;</span>23<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$person</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFullName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>While this isn&#8217;t all that painful when you only need to do it once, in even a moderate sized project, you&#8217;ll need to do something like this hundreds of times. Now let&#8217;s look at the fluidics way to do&nbsp;this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">with<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> MC2_person<span style="color: #009900;">&#40;</span>23<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFullName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Another extremely useful function in fluidics is the dindex function. After using python for the last few months I have really grown fond of the built-in dictionary method get(). What it does is allow you to request an index on an array (called a dictionary in python) and if it doesn&#8217;t exist, you can provide a default. In python the syntax is like dict.get(&#8216;key&#8217;, &#8216;defaultval&#8217;). In fluidics it&#8217;s like&nbsp;this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$lunch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'snack'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'cheetos'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'drink'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'mountain dew'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$drink</span> <span style="color: #339933;">=</span> dindex<span style="color: #009900;">&#40;</span><span style="color: #000088;">$lunch</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;drink&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;pepsi&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// returns &quot;mountain dew&quot;</span>
<span style="color: #000088;">$sandwich</span> <span style="color: #339933;">=</span> dindex<span style="color: #009900;">&#40;</span><span style="color: #000088;">$lunch</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;sandwich&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;ham&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// returns &quot;ham&quot;</span></pre></div></div>

<p>I could sit here and list all the functions that make this little library so useful, but Ollie does a better job of it anyway, so check out his googlecode project. I hope you like it as much as I did.&nbsp;</p>
<p><a href="http://code.google.com/p/fluidics/">Read more about fluidics here.</a><br />
<a href="http://olliesaunders.net/">Ollie Saunders&#8217; Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mc2design.com/blog/cut-down-on-temporary-variables-in-php-with-fluidics/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP CSV Utilities &#8211; a PHP library similar to python&#8217;s standard CSV&#160;module</title>
		<link>http://www.mc2design.com/blog/php-csv-utilities-a-php-library-similar-to-pythons-standard-csv-module</link>
		<comments>http://www.mc2design.com/blog/php-csv-utilities-a-php-library-similar-to-pythons-standard-csv-module#comments</comments>
		<pubDate>Wed, 20 Feb 2008 07:49:21 +0000</pubDate>
		<dc:creator>Luke Visinoni</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP CSV Utilities]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[csv library]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[parser]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.mc2design.com/blog/php-csv-utilities-a-php-library-similar-to-pythons-standard-csv-module</guid>
		<description><![CDATA[Download PHP Csv&#160;Utilities
PHP Csv Utilities Documentation (PhpDocumentor)
Since I began doing web development five years ago, I have been exclusively a PHP developer. Recently though, I have taken quite a liking to python. In fact many times while I&#8217;m writing PHP I find myself thinking, &#8220;It sure would be nice if I could do this the [...]]]></description>
			<content:encoded><![CDATA[<p><strong><a href="http://code.google.com/p/php-csv-utils/downloads/list" rel="external">Download PHP Csv&nbsp;Utilities</a></strong></p>
<p><strong><a href="http://phpcsvutils.mc2design.com/lib/docs/phpdoc/" rel="external">PHP Csv Utilities Documentation</a></strong> (<a href="http://www.phpdoc.org"&nbsp;rel="external">PhpDocumentor</a>)</p>
<p>Since I began doing web development five years ago, I have been exclusively a PHP developer. Recently though, I have taken quite a liking to python. In fact many times while I&#8217;m writing PHP I find myself thinking, &#8220;It sure would be nice if I could do this the python way&#8221;. It&#8217;s not that I don&#8217;t love PHP, it&#8217;s just that python is such an absolute joy to work with. Many features of PHP feel sort of tacked-on as an afterthought. For instance, many standard features available for object-oriented languages are rudimentary or missing completely. Although to PHP&#8217;s credit, <a href="http://devzone.zend.com/article/1714-Whats-New-in-PHP-5">PHP5 and the new Zend engine 2</a> have improved the situation&nbsp;considerably.</p>
<p>Several of the recent projects I&#8217;ve been working on have required an &#8220;import from / export to CSV&#8221; feature. PHP comes with a few functions for reading and writing csv files right out of the box: <a href="http://us.php.net/fgetcsv">fgetcsv</a> and <a href="http://us.php.net/fputcsv">fputcsv</a>. While these are good functions and they get the job done I find they just aren&#8217;t enough in many cases. It would be nice if PHP had an interface like python&#8217;s csv module. Enter PHP CSV Utilities. The library is still in its infancy and nothing about the interface is concrete yet, but here are some of the things that are already possible with the&nbsp;library:</p>
<p><span&nbsp;id="more-103"></span></p>
<p><strong>Open a csv file and parse it&nbsp;line-by-line:</strong></p>
<p>The simplest possible method of reading a csv file is to create a new Csv_Reader object and loop over it with&nbsp;foreach.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$reader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Csv_Reader<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/path/to/myfile.csv'</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;">$reader</span> <span style="color: #b1b100;">as</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;">// do something.. $row now contains an array of the current row</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>What if the file doesn&#8217;t&nbsp;exist?</strong></p>
<p>Csv_Reader will throw a Csv_Exception_FileNotFound exception if it can&#8217;t open the file you&nbsp;provide</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">try <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$reader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Csv_Reader<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/path/to/file'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Csv_Exception_FileNotFound <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Read the header line and then loop through the rest of the&nbsp;lines</strong></p>
<p>If you know the file you are reading has a header row, simply grab it and then loop through the reader with a while loop (foreach will not work here because it starts at the beginning and loops through everything in the&nbsp;reader).</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$reader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Csv_Reader<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/path/to/myfile.csv'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$reader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// grabs first row</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$reader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRow</span><span style="color: #009900;">&#40;</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;">// starts from second row</span>
    <span style="color: #666666; font-style: italic;">// do something</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Read a csv file that was created by&nbsp;Excel</strong></p>
<p>Excel has the ability to save as a csv file instead of its proprietary xls format. The library supports what are called Csv dialects (terminology borrowed from python). Csv dialects simply tell reader and writer objects how to do their job. For instance, Excel only quotes columns if there are special characters in them. Using the standard reader would inproperly parse the output of an Excel csv file. To read an Excel csv file properly, simply provide a Csv_Dialect_Excel object in the&nbsp;constructor.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dialect</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Csv_Dialect_Excel<span style="color: #339933;">;</span>
<span style="color: #000088;">$reader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Csv_Reader<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/path/to/file'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dialect</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// now reader will read excel csv file properly (this does not mean xls)</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$reader</span> <span style="color: #b1b100;">as</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;">// do something</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>But what if I have an excel file with a different delimiter, like a tab&nbsp;character?</strong></p>
<p>If you need to change any of the options in a dialect, you may either extend it via inheritance, or you may simply change said option at&nbsp;run-time.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dialect</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Csv_Dialect_Excel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dialect</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">delimiter</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$reader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDialect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dialect</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;">$reader</span> <span style="color: #b1b100;">as</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;">// do something with $row</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Csv_Dialects represent the format of a csv file</strong> &#8211; This means you can read or write just about any type of csv file. As of right now, Csv_Dialect has the following properties (most of which were borrowed from python&#8217;s csv&nbsp;module):</p>
<ul>
<li><strong>delimiter</strong> &#8211; The delimiter is the character that seperates column values from eachother in a row</li>
<li><strong>quotechar</strong> &#8211; The character used to quote columns</li>
<li><strong>escapechar</strong> &#8211; The character used to escape special characters such as the delimiter or the quotechar</li>
<li><strong>lineterminator</strong> &#8211; The character used to denote the end of a line</li>
<li><strong>quoting</strong> &#8211; This tells the reader which rows in the csv file are quoted, and the writer which columns to quote. Can be any of the following Csv_Dialect constants:
<ul>
<li>Csv_Dialect::QUOTE_MINIMAL &#8211; Quotes only items that contain special characters such as the delimiter or quotechar</li>
<li>Csv_Dialect::QUOTE_ALL &#8211; Quotes all items regardless of their content</li>
<li>Csv_Dialect::QUOTE_NONNUMERIC &#8211; Quotes only items with nonnumeric values</li>
<li>Csv_Dialect::QUOTE_NONE &#8211; Quotes no items regardless of their content</li>
</ul>
</li>
<li><strong>doublequote</strong> &#8211; Not yet implemented</li>
<li><strong>skipinitialspace</strong> &#8211; Not yet implemented</li>
<li><strong>skipblanklines</strong> &#8211; Set to true if you would like for the reader to ignore blank lines</li>
</ul>
<p><strong>Rewrite a csv file with Excel formatting (or any other&nbsp;format)</strong></p>
<p>The Csv_Writer object accepts a Csv_Reader object in its writeRows() method, so rewriting a csv file with a new dialect (new delimiter, line-ending, etc) is really&nbsp;simple.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$reader</span> <span style="color: #339933;">=</span> Csv_Reader<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/path/to/file.csv'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$writer</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Csv_Writer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/path/to/file.csv'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">new</span> Csv_Dialect_Excel<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: #000088;">$writer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">writeRows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$reader</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$writer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// writes csv file but now it's in Excel csv format</span></pre></div></div>

<p>I&#8217;d really like to get an idea of features that would be useful to people other than myself. I&#8217;m also interested in feedback on the interface, and ways I could improve the library. If you&#8217;re interested, download the library and play around with it a bit. When you find something you like or dislike or if you have any kind of input at all, leave a comment below or drop me an email.&nbsp;Enjoy!</p>
<p><strong><a href="http://phpcsvutils.mc2design.com/lib/docs/phpdoc/" rel="external">PHP Csv Utilities Documentation</a> (<a href="http://www.phpdoc.org"&nbsp;rel="external">PhpDocumentor</a>)</strong></p>
<p><a href="http://code.google.com/p/php-csv-utils/downloads/list" rel="external">Download PHP Csv&nbsp;Utilities</a></p>
<p><strong>Plans for version&nbsp;0.2</strong></p>
<ul>
<li><strong>Csv_Sniffer</strong> &#8211; a class that accepts a sample of csv and attempts to deduce its format. It will then return a dialect that fits the csv file as well as attempt to detect whether or not the file has a row of headers.</li>
<li><strong>Csv_Dialect classes</strong> for any and all formats I can dig up (Open Office, Miva Merchant, Google Docs and Spreadsheets, <a href="http://en.wikipedia.org/wiki/Comma-separated_values">standard csv</a>?,  etc.)</li>
<li><strong>An option parameter for Csv_Dialect</strong> &#8211; I was considering allowing an array of parameters in Csv_Dialect&#8217;s constructor. This way you could easily put together a dialect like $dialect = new Csv_Dialect(array(&#8216;quotechar&#8217; => &#8220;&#8216;&#8221;, &#8216;escapechar&#8217; => &#8216;&#8221;&#8216;));</li>
<li><strong>More comprehensive unit tests</strong> &#8211; Although I made my best effort to stick to test-driven development practices (since learning them properly has been on my priority list for a while), there are still some areas where I could use some more testing.</li>
<li><strong>More documentation</strong> &#8211; I&#8217;m going to strive to document every feature of this library. Poor documentation is a pet peeve of <em>everybody</em>.</li>
</ul>
<p><strong>Possible / Eventual&nbsp;Features</strong></p>
<ul>
<li><strong>Csv_Reader_Zip</strong> &#8211; A csv reader that can read zipped files</li>
<li><strong>Csv_Reader_String</strong> &#8211; A csv reader that accepts a string instead of a file</li>
<li><strong>Character encoding</strong> &#8211; This will be the first time I have really had to deal with multiple character encodings, so this may take me a while. I will need to do some research on the subject.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.mc2design.com/blog/php-csv-utilities-a-php-library-similar-to-pythons-standard-csv-module/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>HTML Purifier 3.0.0&#160;released</title>
		<link>http://www.mc2design.com/blog/html-purifier-300-released</link>
		<comments>http://www.mc2design.com/blog/html-purifier-300-released#comments</comments>
		<pubDate>Wed, 16 Jan 2008 16:38:03 +0000</pubDate>
		<dc:creator>Luke Visinoni</dc:creator>
				<category><![CDATA[Industry News]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.mc2design.com/blog/html-purifier-300-released</guid>
		<description><![CDATA[If you have ever used bbcode or any other non-html markup language in an attempt to avoid having to filter user-submitted HTML, those days are over. HTML Purifier is a standards-compliant html filter. This means that not only does it protect your website from security risks such as cross-site scripting attacks, but it also produces [...]]]></description>
			<content:encoded><![CDATA[<p>If you have ever used bbcode or any other non-html markup language in an attempt to avoid having to filter user-submitted HTML, those days are over. HTML Purifier is a standards-compliant html filter. This means that not only does it protect your website from security risks such as cross-site scripting attacks, but it also produces completely valid (x)html. It is also character-encoding aware. With this release, the author, Edward Z. Yang has decided to <a href="http://www.gophp5.org/">GoPHP5</a>, so don&#8217;t expect to see this version released for PHP4 (The 2.1.x branch will be maintained until PHP 4 is completely deprecated, but no new features will be added to&nbsp;it.).</p>
<blockquote><p>
This release a number of improvements in CSS handling, including the filter HTMLPurifier_Filter_ExtractStyleBlocks which integrates HTML Purifier with CSSTidy for cleaning style sheets (see the source code file for more information on usage), contains experimental support for proprietary CSS properties with %CSS.Proprietary, case-insensitive CSS properties, and more lenient hexadecimal color codes. Also, all code has been upgraded to full PHP 5 and is  E_STRICT clean for all versions of PHP 5 (including the 5.0 series, which previously had parse-time errors).</p></blockquote>
<p>For more details about the release, check out <a href="http://htmlpurifier.org/news.html">HTML Purifier&#8217;s website</a>.&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mc2design.com/blog/html-purifier-300-released/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>$_SERVER[&#039;PHP_SELF&#039;] can not be trusted, but there are safe&#160;alternatives</title>
		<link>http://www.mc2design.com/blog/php_self-safe-alternatives</link>
		<comments>http://www.mc2design.com/blog/php_self-safe-alternatives#comments</comments>
		<pubDate>Fri, 22 Jun 2007 18:09:38 +0000</pubDate>
		<dc:creator>Luke Visinoni</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.mc2design.com/blog/php_self-you-little-rascal</guid>
		<description><![CDATA[I spend a good deal of time reading about PHP security over at the PHP Developer&#8217;s Network forums. In one of the many discussions I have had over there, I recall one in particular that really opened my eyes to how easy it can be to overlook a very serious security issue. As you may [...]]]></description>
			<content:encoded><![CDATA[<p>I spend a good deal of time reading about PHP security over at the <a href="http://www.devnetwork.net">PHP Developer&#8217;s Network forums</a>. In one of the many discussions I have had over there, I recall one in particular that really opened my eyes to how easy it can be to overlook a very serious security issue. As you may know, there is a predefined array of server-related variables in PHP, aptly named $_SERVER. For years I used a certain element &#8220;PHP_SELF&#8221; within this array in instances where I needed to output what page I was currently working within. One of the most common of such instances is when you need to tell a form to post back to itself (a very common practice in PHP).<br />
<span id="more-34"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;form method=&quot;post&quot; action=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'PHP_SELF'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
  &lt;!-- form contents --&gt;
&lt;/form&gt;</pre></div></div>

<p>How could this possibly be a security issue? I mean this is an untainted value set by PHP itself, right?&nbsp;</p>
<p><strong>Wrong!</strong></p>
<p>Unfortunately, this is not the case at all. The PHP_SELF element can in fact be altered by the user to include any kind of malicious <abbr title="cross-site scripting">XSS</abbr> code he/she desires. They will be limited only by their imaginations, and the amount of data that is actually allowed though the url (I believe this depends upon the browser). Don&#8217;t feel too down on yourself if you weren&#8217;t aware. Using this element in an irresponsible manner has been a staple in online PHP tutorials for as long as I can&nbsp;remember.</p>
<h3>Where&#8217;s the&nbsp;beef?</h3>
<p>So where is this supposed user-polluted input? If you take a moment to examine the entire $_SERVER array (hint: <a href="http://us2.php.net/manual/en/function.phpinfo.php">phpinfo()</a>), you&#8217;ll notice another element called PATH_INFO. See, it is perfectly valid to tack on extra information after the page name in the <abbr title="Uniform Resource Locator">URL</abbr>. For example, the following is a valid <abbr title="Uniform Resource Locator">URL</abbr> in&nbsp;PHP.</p>
<p>http://www.example.com/index.php/news/category/javascript</p>
<p>In this case, PATH_INFO would be populated with the data appended to the end of the <abbr title="Uniform Resource Locator">URL</abbr>. <strong>PHP_SELF will also contain this data.</strong>. If you were to examine your $_SERVER array given this <abbr title="Uniform Resource Locator">URL</abbr>, you&#8217;d see something like the&nbsp;following:</p>
<p>[PATH_INFO] => /news/category/javascript<br />
[PHP_SELF] => /php/phpselftest.php/news/category/javascript</p>
<p>Now let&#8217;s revisit our form example from earlier. Using $_SERVER['PHP_SELF'] directly within the form action, you can see how this may present an <a href="http://en.wikipedia.org/wiki/Xss"><abbr title="Cross-site Scripting">XSS</abbr> vulnerability</a>. I realize how paranoid I have sounded about <abbr title="Cross-site Scripting">XSS</abbr> in the last several blog posts, but I assure you it&#8217;s for good reason. If you are unfamiliar with XSS, I encourage you to read up on it. <a href="http://en.wikipedia.org/wiki/Xss">Wikipedia&#8217;s article</a> on the subject is a good starting&nbsp;point.</p>
<p>Let&#8217;s say the user enters http://www.example.com/form.php/%22%3E%3Cscript%3Ealert(&#8216;xss&nbsp;attack&#8217;)%3C/script%3E%3Cbr%20class=%22irrelevant</p>
<p>Now let&#8217;s take a look at what effect this has on our form&nbsp;output:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>form method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;post&quot;</span> action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://www.example.com/form.php/&quot;</span><span style="color: #339933;">&gt;&lt;</span>script<span style="color: #339933;">&gt;</span>alert<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'xss attack'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;&lt;</span>br <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;irrelevant&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;!--</span> form contents <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span></pre></div></div>

<p>This is not good. While I can assure you that the simple javascript I injected is harmless, I can&#8217;t guarantee the same for your users. It would be a somewhat trivial task for an attacker to write a javascript capable of stealing sensitive data and tricking an unsuspected user into executing it by way of a link such as the one&nbsp;above.</p>
<h3>Don&#8217;t worry, there are many&nbsp;solutions</h3>
<p>Among the many solutions, I prefer the simplest. In many cases, you can get away with just using a pound sign instead of explicitly naming the&nbsp;page.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>form method<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;post&quot;</span> action<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;!--</span> form contents <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span></pre></div></div>

<p>However, should you require the use of the &lt;base&gt; HTML element, this solution will generally fail miserably. The browser will no longer assume it should post back to the current page, and instead post to whatever you have assigned to your base tag&#8217;s href&nbsp;attribute.</p>
<p>In this case, you can either sanitize the value of PHP_SELF, or you can get the <a href="http://php.net/manual/en/function.basename.php">basename()</a> of either the <a href="http://php.net/manual/en/language.constants.predefined.php">magic constant</a>, __FILE__ (available for php v4.0.2 or later) or the <a href="http://php.net/manual/en/function.basename.php">basename()</a> of&nbsp;$_SERVER['SCRIPT_FILENAME'].</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mc2design.com/blog/php_self-safe-alternatives/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
