<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>code-diesel</title>
	
	<link>http://www.codediesel.com</link>
	<description>/* PHP &amp; MySQL Blog */</description>
	<pubDate>Fri, 10 Jul 2009 15:34:53 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<creativeCommons:license>http://creativecommons.org/licenses/by/2.0/</creativeCommons:license><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/Code-diesel" type="application/rss+xml" /><feedburner:emailServiceId>Code-diesel</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>Filter your wordpress content by location</title>
		<link>http://feedproxy.google.com/~r/Code-diesel/~3/13MJcVccrTQ/</link>
		<comments>http://www.codediesel.com/plugins/wordpress-country-filter-plugin/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 06:33:48 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[plugins]]></category>

		<category><![CDATA[filter]]></category>

		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1195</guid>
		<description><![CDATA[Today I&#8217;ve released my first Wordpress plugin, Country Filter. The Country Filter plugin allows you to control the visibility of Wordpress elements depending on the users country of origin. You can hide or show certain elements of a page or some sections of a post from users browsing from a particular country. For example you [...]]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;ve released my first Wordpress plugin, Country Filter. The <em>Country Filter</em> plugin allows you to control the visibility of Wordpress elements depending on the users country of origin. You can hide or show certain elements of a page or some sections of a post from users browsing from a particular country. For example you could have a ‘donate’ button that displays only if the user is browsing from France, India or UK. </p>
<p>Plugin download and installation instructions can be found <a href="http://www.codediesel.com/plugins/country-filter/">here</a>.</p>
<img src="http://feeds.feedburner.com/~r/Code-diesel/~4/13MJcVccrTQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/plugins/wordpress-country-filter-plugin/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.codediesel.com/plugins/wordpress-country-filter-plugin/</feedburner:origLink></item>
		<item>
		<title>Accessing Wordpress plugins remotely</title>
		<link>http://feedproxy.google.com/~r/Code-diesel/~3/4AW0zpGGFTc/</link>
		<comments>http://www.codediesel.com/php/accessing-wordpress-plugin-data-across-domains/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 04:23:53 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[php]]></category>

		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1087</guid>
		<description><![CDATA[With a plethora of wordpress plugins available for every conceivable purpose, there is a huge amount of data that is stored on a typical wordpress installation. We could easily use that data across domains or display them using widgets on the desktop. Take for example the WP-UserOnline plugin that displays how many users are currently [...]]]></description>
			<content:encoded><![CDATA[<p>With a plethora of wordpress plugins available for every conceivable purpose, there is a huge amount of data that is stored on a typical wordpress installation. We could easily use that data across domains or display them using widgets on the desktop. Take for example the <a target="_blank" href="http://lesterchan.net/portfolio/programming/php/">WP-UserOnline</a> plugin that displays how many users are currently online on your blog. We could easily write a proxy to grab that information from the plugin and use it in a Yahoo Widget or in a AIR application on the desktop, so you can see the number of users online without having to visit your site. Or you could use the information on some other site.<br />
<span id="more-1087"></span><br />
<strong>Example proxy</strong><br />
A sample proxy code to access the <em>WP-UserOnline</em> plugin data is shown below. The code basically uses the <em>WP-UserOnline</em> plugins &#8216;<em>get_users_browsing_site()</em>&#8216; function to read the data and echo it as a JSON string. Now all you have to do is request the below file (useronline_proxy.php) from a remote server and you get a JSON string containing the useronline information.</p>

<div class="wp_codebox"><table width="100%" ><tr id="10875"><td class="code" id="1087code5"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* 
  useronline_proxy.php
&nbsp;
  On my server this file is located in the
  www.codediesel.com/remote/ directory, hence the
  location of the $wp_root variable. You will need
  to change it depending on where you save this file.
*/</span>
<span style="color: #0000ff;">$wp_root</span> = <span style="color: #ff0000;">'..'</span>;
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066;">file_exists</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$wp_root</span>.<span style="color: #ff0000;">'/wp-load.php'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$wp_root</span>.<span style="color: #ff0000;">'/wp-load.php'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$wp_root</span>.<span style="color: #ff0000;">'/wp-config.php'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*  The following file contains all the functions 
    we require to get to the wp-useronline data. 
*/</span> 
<span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$wp_root</span>.
             <span style="color: #ff0000;">'/wp-content/plugins/wp-useronline/wp-useronline.php'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/* Get the number of users browsing the site right now */</span>
<span style="color: #0000ff;">$users</span> = get_users_browsing_site<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$temp</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;member&quot;</span>  =&gt;  <span style="color: #0000ff;">$users</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>,
              <span style="color: #ff0000;">&quot;guest&quot;</span>   =&gt;  <span style="color: #0000ff;">$users</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>,
              <span style="color: #ff0000;">&quot;bot&quot;</span>     =&gt;  <span style="color: #0000ff;">$users</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>,
              <span style="color: #ff0000;">&quot;total&quot;</span>   =&gt;  <span style="color: #000066;">array_sum</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$users</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/* Encodes the array as a JSON string and print it.
  (Only available since PHP 5.2)
*/</span>
<span style="color: #000066;">echo</span> json_encode<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$temp</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>How the whole thing works is shown below:</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2009/06/drawing22.gif"><img src="http://www.codediesel.com/wp-content/uploads/2009/06/drawing22.gif" alt="wordpress remote" title="wordpress remote" width="458" height="267" class="aligncenter size-full wp-image-1129" /></a></p>
<p><strong>Accessing the proxy remotely</strong><br />
Assuming allow-url-fopen is true, you can now access the <em>useronline_proxy.php</em> file from another server as below. Note that you will have to use cURL if remote url fopen is not allowed on your php installation.</p>

<div class="wp_codebox"><table width="100%" ><tr id="10876"><td class="code" id="1087code6"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #0000ff;">$fp</span> = <span style="color: #000066;">fopen</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;http://www.domain.com/remote/useronline_proxy.php&quot;</span>, <span style="color: #ff0000;">&quot;r&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$json_string</span> = <span style="color: #ff0000;">''</span>;
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #000066;">feof</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fp</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #0000ff;">$json_string</span> .= <span style="color: #000066;">fread</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fp</span>, <span style="color: #cc66cc;">8192</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$json_data</span> = json_decode<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$json_string</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000066;">fclose</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fp</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Using Javascript to access the proxy</strong><br />
You can also use javascript to access the remote proxy. But since cross domain access is restricted by the browser, we will need to resort to jQuery&#8217;s  <em>getJSON</em> function to do the same. The javascript code is shown below.</p>

<div class="wp_codebox"><table width="100%" ><tr id="10877"><td class="code" id="1087code7"><pre class="javascript">&lt;!DOCTYPE HTML <span style="color: #003366; font-weight: bold;">PUBLIC</span> <span style="color: #3366CC;">&quot;-//W3C//DTD HTML 4.01//EN&quot;</span>&gt;
&lt;html lang=<span style="color: #3366CC;">&quot;en&quot;</span>&gt;
&lt;head&gt;
&lt;title&gt;&lt;/title&gt;
&lt;script src=<span style="color: #3366CC;">&quot;jquery-1.3.2.js&quot;</span>&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;script&gt;
$<span style="color: #66cc66;">&#40;</span>document<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">ready</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
&nbsp;
$proxy = <span style="color: #3366CC;">&quot;http://www.domain.com/remote/useronline_proxy.php?callback=?&quot;</span>;
    $.<span style="color: #006600;">getJSON</span><span style="color: #66cc66;">&#40;</span>$proxy, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>data<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        $info = <span style="color: #3366CC;">'Members: '</span> + data.<span style="color: #006600;">member</span> + <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span> +
                <span style="color: #3366CC;">'Guests: '</span> + data.<span style="color: #006600;">guest</span> + <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span> +
                <span style="color: #3366CC;">'Bots: '</span> + data.<span style="color: #006600;">bot</span> + <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span> +
                <span style="color: #3366CC;">'Total: '</span> + data.<span style="color: #006600;">total</span>;
&nbsp;
        <span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>$info<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>But to make the above javascript code work we will have to make a little change to the <em>useronline_proxy.php</em> file.</p>

<div class="wp_codebox"><table width="100%" ><tr id="10878"><td class="code" id="1087code8"><pre class="php">&nbsp;
<span style="color: #808080; font-style: italic;">/* Replace the 'echo json_encode($temp);' line with the following */</span>
&nbsp;
<span style="color: #000066;">echo</span> <span style="color: #0000ff;">$_GET</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'callback'</span><span style="color: #66cc66;">&#93;</span> . <span style="color: #ff0000;">'('</span> . json_encode<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$temp</span><span style="color: #66cc66;">&#41;</span> . <span style="color: #ff0000;">');'</span>;</pre></td></tr></table></div>

<p><strong>Security</strong><br />
To keep the proxy code simple, I&#8217;ve not added any authentication to the same. But you could pass a username/password as a GET parameter and check it in <em>useronline_proxy.php</em>.</p>
<img src="http://feeds.feedburner.com/~r/Code-diesel/~4/4AW0zpGGFTc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/accessing-wordpress-plugin-data-across-domains/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.codediesel.com/php/accessing-wordpress-plugin-data-across-domains/</feedburner:origLink></item>
		<item>
		<title>Opera Unite : A web server in your browser</title>
		<link>http://feedproxy.google.com/~r/Code-diesel/~3/7-7SWf5rqnA/</link>
		<comments>http://www.codediesel.com/browser/opera-unite-a-web-server-in-your-browser/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 06:32:07 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[browser]]></category>

		<category><![CDATA[opera]]></category>

		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1065</guid>
		<description><![CDATA[Rarely do I get excited about new software developments. The last I remember was when WolframAlpha was realeased. Now it is Opera Unite. I&#8217;ve forever been a Opera fan, and always loved the extra bit of functionality thoughtfully added to the product by the opera team. But Unite really takes the step forward.

In a nutshell, [...]]]></description>
			<content:encoded><![CDATA[<p>Rarely do I get excited about new software developments. The last I remember was when <a href="http://www.wolframalpha.com/index.html" target="_blank">WolframAlpha</a> was realeased. Now it is <a target="_blank" href="http://unite.opera.com/">Opera Unite</a>. I&#8217;ve forever been a Opera fan, and always loved the extra bit of functionality thoughtfully added to the product by the opera team. But Unite really takes the step forward.<br />
<span id="more-1065"></span><br />
In a nutshell, Opera Unite is a collaborative technology that uses a web server embedded inside the Opera browser to share data and services with the outside world. To share pictures, movies and other content right off your hard disk all you have to do is run the relevant Unite Service and then send the resulting link to your peers, with which they can than access your content. You can also write your own Opera applications in JavaScript, known as <em>Opera Unite Services</em>, which are then run by the Opera server. </p>
<p><strong>Basic Works</strong><br />
Opera Unite works by using a proxy server (operaunite.com) between the opera server and the various clients. This means besides your login credentials it takes zero configuration to connect to the outside world. There have been various attempts before for such collaborative tools, but the seamless integration provided by Unite makes it very  easier for people to collaborate and share content with other people.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2009/06/drawing1.gif"><img src="http://www.codediesel.com/wp-content/uploads/2009/06/drawing1.gif" alt="opera unite" title="opera unite" width="540" height="207" class="aligncenter size-full wp-image-1081" /></a></p>
<p><strong>So what can you do with Unite:</strong><br />
1. You could play interactive games with your friends.<br />
2. Share photos, music and videos with other people, straight off your hard drive.<br />
3. You can work collaboratively with others on documents with your peers.<br />
or create whatever collaborative application you like using the Unite API.</p>
<p>The default Unite services shipped with Opera are as follows:</p>
<p>a. File Sharing<br />
Securely share a file from your personal computer without waiting to upload it. First select the folder from which you would like to share files. Opera Unite then generates a direct URL to that folder. By giving that link to your friends, you can share files without routing through a third-party Web service.</p>
<p>b.Web Server<br />
Run entire Web sites from your local computer with the Opera Unite Web Server. After selecting the folder containing your Web site, you can share and host it from the given Opera Unite URL. Opera Unite will automatically recognize index files and create the Web site as you designed it.</p>
<p>c.Media Player<br />
Rock out wherever you are by accessing your MP3s and playlists from any machine. After selecting the folder containing your playlist, use the Opera Unite direct link to play your tracks directly in any modern Web browser.</p>
<p>d.Photo Sharing<br />
Share your photos direct from your PC, without uploading them online. Once you select your photo folder, the photo-sharing service will create a thumbnail image gallery of your photos. Clicking the thumbnail will present the photo in its original resolution.</p>
<p>e.The Lounge<br />
The Lounge is a self-contained chat service running on your computer. Your friends can access the chat room via the direct link, which will not require them to sign into any service. Depending on your privacy settings, you need only provide the generated password to your service in order for people to log in to your chatroom.</p>
<p>f.Fridge<br />
Post a note on your friends’ virtual refrigerators. By sharing the direct link to your refrigerator, you and your friends, family or colleagues can exchange notes securely and privately in real time.</p>
<p><strong>Security</strong><br />
Unite provides nice and simple security features when sharing your content with the outside world. Naturally there still will be debates on the security implications of Unite; but leaving that aside Opera, by embedding a webserver in the browser, has dared to change more then a decade old model of a web browser.</p>
<img src="http://feeds.feedburner.com/~r/Code-diesel/~4/7-7SWf5rqnA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/browser/opera-unite-a-web-server-in-your-browser/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.codediesel.com/browser/opera-unite-a-web-server-in-your-browser/</feedburner:origLink></item>
		<item>
		<title>Creating custom stream filters</title>
		<link>http://feedproxy.google.com/~r/Code-diesel/~3/kbJmjuPOFG8/</link>
		<comments>http://www.codediesel.com/php/creating-custom-stream-filters/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 13:49:47 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[php]]></category>

		<category><![CDATA[streams]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1001</guid>
		<description><![CDATA[In this post we will see how to create a custom stream filter. Streams, first introduced in PHP 4.3, provide an abstration layer for file access. A number of different resources besides files - like network connections, compression protocols etc. can be regarded as &#8220;streams&#8221; of data which can be serially read and written to.

By [...]]]></description>
			<content:encoded><![CDATA[<p>In this post we will see how to create a custom stream filter. <em>Streams</em>, first introduced in PHP 4.3, provide an abstration layer for file access. A number of different resources besides files - like network connections, compression protocols etc. can be regarded as &#8220;streams&#8221; of data which can be serially read and written to.<br />
<span id="more-1001"></span></p>
<p>By default there are a number of filters registered with PHP. You can get the list of filters registered on your system by the following call:</p>

<div class="wp_codebox"><table width="100%" ><tr id="100115"><td class="code" id="1001code15"><pre class="php"><span style="color: #000066;">print_r</span> <span style="color: #66cc66;">&#40;</span>stream_get_filters<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>On my system it returns the following registered filters.</p>

<div class="wp_codebox"><table width="100%" ><tr id="100116"><td class="code" id="1001code16"><pre class="php"><span style="color: #000066;">Array</span>
<span style="color: #66cc66;">&#40;</span>
    <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> =&gt; convert.<span style="color: #000066;">iconv</span>.*
    <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> =&gt; string.rot13
    <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span> =&gt; string.toupper
    <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span> =&gt; string.tolower
    <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#93;</span> =&gt; string.<span style="color: #000066;">strip_tags</span>
    <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#93;</span> =&gt; convert.*
    <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#93;</span> =&gt; consumed
    <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#93;</span> =&gt; zlib.*
    <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#93;</span> =&gt; bzip2.*
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>For example to strip html tags from a input string you add the &#8217;string.strip_tags&#8217; filter to the stream resource as below.</p>

<div class="wp_codebox"><table width="100%" ><tr id="100117"><td class="code" id="1001code17"><pre class="php"><span style="color: #0000ff;">$html</span> = <span style="color: #ff0000;">'The &lt;b&gt;World&lt;/b&gt; is safe &lt;i&gt;again&lt;/i&gt;.'</span>;
&nbsp;
<span style="color: #0000ff;">$fp</span> = <span style="color: #000066;">fopen</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;php://output&quot;</span>, <span style="color: #ff0000;">&quot;r&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">stream_filter_prepend</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fp</span>, <span style="color: #ff0000;">&quot;string.strip_tags&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">fwrite</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fp</span>, <span style="color: #0000ff;">$html</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">fclose</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$fp</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Which will than output:</p>

<div class="wp_codebox"><table width="100%" ><tr id="100118"><td class="code" id="1001code18"><pre>The World is safe again.</pre></td></tr></table></div>

<p><strong>Creating a custom filter</strong><br />
In this section we will create a filter to replace urls in the input stream with a<br />
 &#8216;[----URL----]&#8216; string. Now this may not look like much of a helpful filter, but this is just for illustrative purpose. You can use any replacement string you like. The complete source for the filter class is shown below.</p>

<div class="wp_codebox"><table width="100%" ><tr id="100119"><td class="code" id="1001code19"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> URLFilter <span style="color: #000000; font-weight: bold;">extends</span> PHP_User_Filter
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$_data</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Called when the filter is initialized */</span>
    <span style="color: #000000; font-weight: bold;">function</span> onCreate<span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #0000ff;">$this</span>-&gt;_data = <span style="color: #ff0000;">''</span>;
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">/* This is where the actual stream data conversion takes place */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> filter<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$in</span>, <span style="color: #0000ff;">$out</span>, &amp;<span style="color: #0000ff;">$consumed</span>, <span style="color: #0000ff;">$closing</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">/* We read all the stream data and store it in 
           the '$_data' variable 
        */</span>
        <span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$bucket</span> = stream_bucket_make_writeable<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$in</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$this</span>-&gt;_data .= <span style="color: #0000ff;">$bucket</span>-&gt;<span style="color: #006600;">data</span>;
            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">bucket</span> = <span style="color: #0000ff;">$bucket</span>;
            <span style="color: #0000ff;">$consumed</span> = <span style="color: #cc66cc;">0</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">/* Now that we have read all the data from the stream we process 
          it and save it again to the bucket.
        */</span>
        <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$closing</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0000ff;">$consumed</span> += <span style="color: #000066;">strlen</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;_data<span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #0000ff;">$pattern</span> = <span style="color: #ff0000;">&quot;#http(s)?://([<span style="color: #000099; font-weight: bold;">\w</span>-]+<span style="color: #000099; font-weight: bold;">\.</span>)+[<span style="color: #000099; font-weight: bold;">\w</span>-]+(/[<span style="color: #000099; font-weight: bold;">\w</span>-./?%&amp;=]*)?#&quot;</span>;
            <span style="color: #0000ff;">$str</span> = <span style="color: #000066;">preg_replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$pattern</span>,
                                <span style="color: #ff0000;">'[----URL----]'</span>,
                                <span style="color: #0000ff;">$this</span>-&gt;_data<span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">bucket</span>-&gt;<span style="color: #006600;">data</span> = <span style="color: #0000ff;">$str</span>;
            <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">bucket</span>-&gt;<span style="color: #006600;">datalen</span> = <span style="color: #000066;">strlen</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;_data<span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>!<span style="color: #000066;">empty</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">bucket</span>-&gt;<span style="color: #006600;">data</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                stream_bucket_append<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$out</span>, <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">bucket</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #b1b100;">return</span> PSFS_PASS_ON;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> PSFS_FEED_ME;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>All the action takes place in the <em>filter()</em> method, where we collect all the data from the buckets. The data is retrieved in the fashion of a <a target="_blank" href="http://en.wikipedia.org/wiki/Bucket_brigade">bucket brigade</a>. The <em>stream_bucket_make_writable()</em> function reads a portion of the data from the input bucket and converts it to a PHP bucket object. The data property of the bucket object is a string holding the bucket&#8217;s data, whereas datalen stores its length. We could convert the data in the filter() function itself, but rather we store all the data retrieved in the $_data variable and convert it at a single go. </p>
<p>Once all the data from the stream is read the $closing parameter is set to <em>true</em>. Now we can check for that and than process the data in any fashion we like. Once the data processing is done we add the processed data to the bucket and return a <em>PSFS_PASS_ON</em> to imply that we have successfully processed the stream data.</p>
<p><strong>An Example</strong><br />
Before we use the filter we have to register the filter and append it to the stream. A example using the above class is shown below, which gets the content from Google.com and than replaces all the urls in the source with the &#8216;[----URL----]&#8216; string.</p>

<div class="wp_codebox"><table width="100%" ><tr id="100120"><td class="code" id="1001code20"><pre class="php">&nbsp;
stream_filter_register<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'myFilter'</span>, <span style="color: #ff0000;">'URLFilter'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$handle</span> = <span style="color: #000066;">fopen</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;http://www.google.com/&quot;</span>, <span style="color: #ff0000;">&quot;r&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">stream_filter_append</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$handle</span>, <span style="color: #ff0000;">&quot;myFilter&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #000066;">feof</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$handle</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #0000ff;">$contents</span> .= <span style="color: #000066;">fread</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$handle</span>, <span style="color: #cc66cc;">8192</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000066;">fclose</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$handle</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">echo</span> <span style="color: #0000ff;">$contents</span>;</pre></td></tr></table></div>

<img src="http://feeds.feedburner.com/~r/Code-diesel/~4/kbJmjuPOFG8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/creating-custom-stream-filters/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.codediesel.com/php/creating-custom-stream-filters/</feedburner:origLink></item>
		<item>
		<title>Software Peter Principle</title>
		<link>http://feedproxy.google.com/~r/Code-diesel/~3/gLy5Ndnl1OI/</link>
		<comments>http://www.codediesel.com/software/software-peter-principle/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 00:00:35 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[software]]></category>

		<category><![CDATA[design]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1013</guid>
		<description><![CDATA[Having to scrap a project after working on it for more than a year is not really an interesting way to up your morale. But it is one the most common things to occur in software development. 
To take a real world example; A development company I know has been developing a shopping cart for [...]]]></description>
			<content:encoded><![CDATA[<p>Having to scrap a project after working on it for more than a year is not really an interesting way to up your morale. But it is one the most common things to occur in software development. </p>
<p>To take a real world example; A development company I know has been developing a shopping cart for the last 14 months ( beats me why anyone has to develop a shopping cart when so many free and excellent are already available ). But it is still nowhere near production ready. There is always <em>something left</em> to be added or cleaned. During the development period more than half a dozen programmers have worked on the project and left, producing a convoluted piece of software, which every new member dreads to touch in fear of breaking something somewhere. Salvaging the project is getting harder by the day.<br />
<span id="more-1013"></span><br />
The above episode is a perfect case of what is known in our field as the &#8216;Software Peter principle&#8217;.  I first came across this principle in chapter 3 of <em>C++ FAQs</em>. Originally borrowed from management, the principle basically describes:</p>
<blockquote><p>a dying project which has little by little become too complex to be understood even by its own developers.</p></blockquote>
<p>Some reasons usually cited being :</p>
<p><strong>Loss of Conceptual Integrity</strong><br />
Conceptual integrity is a measure of how well software conforms to a simple set of design principles. Conceptual integrity ensures that anywhere you look in your software system, you can tell that each small design is part of the same overall structure and not a mishmash of different designs.</p>
<p>Conceptual integrity is achieved when the software’s design proceeds from a small number of agreeing individuals, with a well defined but strong architectural team. In projects devoid of strong teams each individual favors personal interest over the common interest of the project. Adding his own coding style, altering earlier designs to suit his individual taste or deviating from the initial architectural design. Incidentally frameworks provide a means to achieve Conceptual Integrity</p>
<p><strong>Incompetent &#038; Inexperienced programmers</strong><br />
Not much can be done if the developers are not up to the project skills. All you can do is cross your fingers and pray, or at a little more price hire good developers. However hiring good programmers isn&#8217;t always enough. The experience of developing real world projects is way different than developing in an academic environment. Many times developers go about using complex language constructs in the code to showoff their proficiency, when a simple one could easily do.</p>
<p>Like diabetes, the <em>Software Peter Principle</em> is known as a silent killer; by the time symptoms show it is already too late. Salvaging becomes impossible and the only option left is to kill the project and move on. (Although not exactly the same with diabetes. Thank Heavens!)</p>
<img src="http://feeds.feedburner.com/~r/Code-diesel/~4/gLy5Ndnl1OI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/software/software-peter-principle/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.codediesel.com/software/software-peter-principle/</feedburner:origLink></item>
		<item>
		<title>Refactoring 3: Replace Temp with Query</title>
		<link>http://feedproxy.google.com/~r/Code-diesel/~3/OjOuDzlwKbU/</link>
		<comments>http://www.codediesel.com/refactoring/refactoring-3-replace-temp-with-query/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 10:52:52 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=963</guid>
		<description><![CDATA[Temporary variables are a integral part of any code. But a splattering of the same all over can make your code hard to understand or modify. Replace temp with query is a refactoring method where you replace temp variable expressions with methods. This method is often also required before you use the Extract Method refactoring.

A [...]]]></description>
			<content:encoded><![CDATA[<p>Temporary variables are a integral part of any code. But a splattering of the same all over can make your code hard to understand or modify. <em>Replace temp with query</em> is a refactoring method where you replace temp variable expressions with methods. This method is often also required before you use the <a target="_blank" href="http://www.codediesel.com/refactoring/refactoring-2-extract-method/">Extract Method</a> refactoring.<br />
<span id="more-963"></span><br />
<strong>A simple example</strong><br />
Take for example the following code with a <em>Item</em> class that exposes a single function <em>get_price</em>. &#8216;get_price&#8217; returns the total amount of the items. I&#8217;ve hard coded the quantity and price variables to simplify the code so that we can focus on the essential element. It contains two temp variables - $base_price and  $discount_factor - they are a perfect candidate for refactoring as they also do not modify any other objects.</p>

<div class="wp_codebox"><table width="100%" ><tr id="96325"><td class="code" id="963code25"><pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Item <span style="color: #66cc66;">&#123;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$_quantity</span> = <span style="color: #cc66cc;">100</span>;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$_item_price</span> = <span style="color: #cc66cc;">29</span>;
&nbsp;
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> get_price<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
          <span style="color: #0000ff;">$base_price</span> = <span style="color: #0000ff;">$this</span>-&gt;_quantity * <span style="color: #0000ff;">$this</span>-&gt;_item_price;
          <span style="color: #0000ff;">$discount_factor</span> = <span style="color: #cc66cc;">0</span>;
&nbsp;
          <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$base_price</span> &gt; <span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
               <span style="color: #0000ff;">$discount_factor</span> = <span style="color: #cc66cc;">0.95</span>;
          <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
               <span style="color: #0000ff;">$discount_factor</span> = <span style="color: #cc66cc;">0.98</span>;
          <span style="color: #66cc66;">&#125;</span>
&nbsp;
          <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$base_price</span> * <span style="color: #0000ff;">$discount_factor</span><span style="color: #66cc66;">&#41;</span>;
     <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">$item</span> = <span style="color: #000000; font-weight: bold;">new</span> Item<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000066;">echo</span> <span style="color: #0000ff;">$item</span>-&gt;<span style="color: #006600;">get_price</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Now all we have to do is convert the temp variables right side expression into methods and use the same in <em>get_price</em>. To avoid introducing errors during refactoring we convert one temp variable at a time and test it before proceeding further. First we convert <em>$base_price</em> temp variable to a function.</p>

<div class="wp_codebox"><table width="100%" ><tr id="96326"><td class="code" id="963code26"><pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Item <span style="color: #66cc66;">&#123;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$_quantity</span> = <span style="color: #cc66cc;">100</span>;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$_item_price</span> = <span style="color: #cc66cc;">29</span>;
&nbsp;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> base_price<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
          <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;_quantity * <span style="color: #0000ff;">$this</span>-&gt;_item_price<span style="color: #66cc66;">&#41;</span>;
     <span style="color: #66cc66;">&#125;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> get_price<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
          <span style="color: #0000ff;">$base_price</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">base_price</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #0000ff;">$discount_factor</span> = <span style="color: #cc66cc;">0</span>;
&nbsp;
          <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$base_price</span> &gt; <span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
               <span style="color: #0000ff;">$discount_factor</span> = <span style="color: #cc66cc;">0.95</span>;
          <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
               <span style="color: #0000ff;">$discount_factor</span> = <span style="color: #cc66cc;">0.98</span>;
          <span style="color: #66cc66;">&#125;</span>
&nbsp;
          <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$base_price</span> * <span style="color: #0000ff;">$discount_factor</span><span style="color: #66cc66;">&#41;</span>;
     <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Next we convert the <em>$discount_factor</em> variable expression to a function.</p>

<div class="wp_codebox"><table width="100%" ><tr id="96327"><td class="code" id="963code27"><pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Item <span style="color: #66cc66;">&#123;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$_quantity</span> = <span style="color: #cc66cc;">100</span>;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #0000ff;">$_item_price</span> = <span style="color: #cc66cc;">29</span>;
&nbsp;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> base_price<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
          <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;_quantity * <span style="color: #0000ff;">$this</span>-&gt;_item_price<span style="color: #66cc66;">&#41;</span>;
     <span style="color: #66cc66;">&#125;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> discount_factor<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
          <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">base_price</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">&#41;</span>
               <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0.95</span>;
          <span style="color: #b1b100;">else</span>
               <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0.98</span>;
     <span style="color: #66cc66;">&#125;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> get_price<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
          <span style="color: #0000ff;">$base_price</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">base_price</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #0000ff;">$discount_factor</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">discount_factor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
          <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$base_price</span> * <span style="color: #0000ff;">$discount_factor</span><span style="color: #66cc66;">&#41;</span>;
     <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Finally we eliminate the temp variables in the <em>get_price</em> function.</p>

<div class="wp_codebox"><table width="100%" ><tr id="96328"><td class="code" id="963code28"><pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> get_price<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
          <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">base_price</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> * <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">discount_factor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
     <span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Using <em>Replace Temp with Query</em> is easier when the temp variables are assigned only once in a block of code, and also if they do not cause any side effects i.e the temp variables statement does not modify any object.<br />
Note that in the above example before refactoring the $base_price and  $discount_factor variables were assigned to only once each. Multiple assignments can make it a little trickier to use the current refactoring method.</p>
<p><strong>Conclusion</strong><br />
This seems a long way to go to eliminate temporary variables, but besides clean code the other main advantage of using the above method is that now the <em>base_price</em> and the <em>discount_factor</em> methods are available to other members of the class to use.</p>
<img src="http://feeds.feedburner.com/~r/Code-diesel/~4/OjOuDzlwKbU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/refactoring/refactoring-3-replace-temp-with-query/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.codediesel.com/refactoring/refactoring-3-replace-temp-with-query/</feedburner:origLink></item>
		<item>
		<title>Codediesel Firefox toolbar</title>
		<link>http://feedproxy.google.com/~r/Code-diesel/~3/-nHcDpq2YFY/</link>
		<comments>http://www.codediesel.com/tools/codediesel-firefox-toolbar/#comments</comments>
		<pubDate>Tue, 19 May 2009 14:23:14 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[browser]]></category>

		<category><![CDATA[tools]]></category>

		<category><![CDATA[firefox]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=874</guid>
		<description><![CDATA[A couple of days back I created a Firefox toolbar for my blog as an experiment in learning XUL. The toolbar is shown below. You can install the same from here.

Now you can create a toolbar for your own blog or site. The source for the toolbar can be downloaded from below after which you [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of days back I created a Firefox toolbar for my blog as an experiment in learning XUL. The toolbar is shown below. You can install the same from </a><a title="Install" href="/data/code/codediesel_toolbar.php">here</a>.</p>
<p><a href="/data/code/codediesel_toolbar.php"><img src="http://www.codediesel.com/wp-content/uploads/2009/05/toolbar.gif" alt="Install" title="Install" width="546" height="24" class="aligncenter size-full wp-image-940" /></a></p>
<p>Now you can create a toolbar for your own blog or site. The source for the toolbar can be downloaded from below after which you can edit the code to suit your site; to help you there is a nice reference for XUL elements <a target="_blank" href="https://developer.mozilla.org/en/XUL">here</a>. The idea for the post was inspired by <a target="_blank" href="http://davidwalsh.name/">david</a>.</p>
<div  class="download2">
<a href="http://www.codediesel.com/downloads/codedieselToolbar">Download Source</a><br />
<span>Downloads : 97  / File size : 23.2 kB</span>
</div>
<img src="http://feeds.feedburner.com/~r/Code-diesel/~4/-nHcDpq2YFY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/tools/codediesel-firefox-toolbar/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.codediesel.com/tools/codediesel-firefox-toolbar/</feedburner:origLink></item>
		<item>
		<title>WolframAlpha - Dawn of a new search era</title>
		<link>http://feedproxy.google.com/~r/Code-diesel/~3/iiru-TTXkjc/</link>
		<comments>http://www.codediesel.com/tools/wolframalpha-dawn-of-a-new-search/#comments</comments>
		<pubDate>Fri, 15 May 2009 06:28:20 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[general]]></category>

		<category><![CDATA[tools]]></category>

		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=901</guid>
		<description><![CDATA[Today will be the launch of WolframAlpha, the new knowledge engine everyone is waiting for. WolframAlpha will truly revolutionize information search in that the information returned will be computed for your particular purpose from a gargantuan knowledge base. It rightly shows the power of intelligent computing. Even though I&#8217;m a developer myself and regularly keep [...]]]></description>
			<content:encoded><![CDATA[<p>Today will be the launch of <a target="_blank" href="http://www.wolframalpha.com/index.html">WolframAlpha</a>, the new knowledge engine everyone is waiting for. WolframAlpha will truly revolutionize information search in that the information returned will be computed for your particular purpose from a gargantuan knowledge base. It rightly shows the power of intelligent computing. Even though I&#8217;m a developer myself and regularly keep myself abreast in the software field, I couldn&#8217;t have imagined the amount of advances made in natural language processing, heuristics and knowledge representation.<br />
<span id="more-901"></span></p>
<p>According to the sites faq:</p>
<p><em>Wolfram|Alpha&#8217;s long-term goal is to make all systematic knowledge immediately computable and accessible to everyone. We aim to collect and curate all objective data; implement every known model, method, and algorithm; and make it possible to compute whatever can be computed about anything. Our goal is to build on the achievements of science and other systematization of knowledge to provide a single source that can be relied on by everyone for definitive answers to factual queries.</em></p>
<p>Conceived by the brilliant <a target="_blank" href="http://en.wikipedia.org/wiki/Stephen_Wolfram">Stephen Wolfram</a>, its another feather in his cap after his magnificent <a target="_blank" href="http://www.wolfram.com/products/mathematica/index.html">Mathematica</a> software.</p>
<p>As an example if you search for &#8220;life expectancy male age 40 Finland&#8221; the engine will return the following:<br />
<a href="http://www.codediesel.com/wp-content/uploads/2009/05/wolframalpha_example1.jpg"><img src="http://www.codediesel.com/wp-content/uploads/2009/05/wolframalpha_example1-448x1024.jpg" alt="wolframalpha_example1" title="wolframalpha_example1" width="448" height="1024" class="aligncenter size-large wp-image-905" /></a></p>
<p>Or if you search for &#8220;president of Brazil in 1922&#8243;:</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2009/05/wolframalpha_example2.jpg"><img src="http://www.codediesel.com/wp-content/uploads/2009/05/wolframalpha_example2.jpg" alt="wolframalpha_example2" title="wolframalpha_example2" width="448" height="407" class="aligncenter size-full wp-image-907" /></a></p>
<p>Or a mathematical query like &#8220;3,7,15,31,63,&#8230;&#8221;, and wolframalpha computes the next terms in the series and also gives the functions for the same:</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2009/05/wolframalpha_example3.jpg"><img src="http://www.codediesel.com/wp-content/uploads/2009/05/wolframalpha_example3.jpg" alt="wolframalpha_example3" title="wolframalpha_example3" width="448" height="585" class="aligncenter size-full wp-image-911" /></a></p>
<p>The other interesting feature of the software engine is that for many queries it computes the information from realtime data. For example if you were to search for &#8216;moon&#8217; than wolframalpha would give all the details of the same and also such information as the present position in the sky, distance from the earth, etc. You can watch the detailed examples <a target="_blank" href="http://www.wolframalpha.com/screencast/introducingwolframalpha.html">here</a>. Imagine what this kind of information can do in the hands of researchers and scientists. An exponential increase in the present knowledge in all areas of science.</p>
<p>The above example are just the tip of what the engine can do. Imagine a robot linked to the WolframAlpha engine and you can get the picture of where we are heading. We are on our way to building a true <a target="_blank" href="http://en.wikipedia.org/wiki/Encyclopedia_Galactica">Encyclopedia Galactica</a>. Knowledge search will never be the same again. Wish Asimov was here!</p>
<img src="http://feeds.feedburner.com/~r/Code-diesel/~4/iiru-TTXkjc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/tools/wolframalpha-dawn-of-a-new-search/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.codediesel.com/tools/wolframalpha-dawn-of-a-new-search/</feedburner:origLink></item>
		<item>
		<title>Pushing xpi mime content from php</title>
		<link>http://feedproxy.google.com/~r/Code-diesel/~3/sKyPY-d_OvA/</link>
		<comments>http://www.codediesel.com/php/sending-xpi-mime-content-from-php/#comments</comments>
		<pubDate>Fri, 15 May 2009 04:01:49 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[browser]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[tip]]></category>

		<category><![CDATA[mime]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=880</guid>
		<description><![CDATA[A couple of days back I created a Firefox toolbar for my blog as an experiment in learning XUL. Once installed on my blog I wanted Firefox to recognize it as an addon and install it rather than displaying a &#8217;save/open&#8217; dialog. For that I needed to add the following directive to the servers .htaccess [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of days back I created a Firefox toolbar for my blog as an experiment in learning XUL. Once installed on my blog I wanted Firefox to recognize it as an addon and install it rather than displaying a &#8217;save/open&#8217; dialog. For that I needed to add the following directive to the servers .htaccess file.<br />
<span id="more-880"></span></p>

<div class="wp_codebox"><table width="100%" ><tr id="88031"><td class="code" id="880code31"><pre class="apache"><span style="color: #00007f;">AddType</span> application/x-xpinstall .xpi</pre></td></tr></table></div>

<p>But the problem was that my server (godaddy) for some mysterious reasons was not recognizing the above directive. Searching around I found that godaddy has some restrictions on the various htaccess directives (yuck!). After fiddling for some time I finally had to try another solution. </p>
<p>What the Apache &#8216;AddType&#8217; directive does is that it maps the given filename extension onto the specified MIME content type, so that the browser can understand what it is supposed to do with the content. If we could replicate that in PHP than we are good. A possible solution that worked for me is shown below. You can also use the idea for other MIME types.</p>

<div class="wp_codebox"><table width="100%" ><tr id="88032"><td class="code" id="880code32"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/* File we want to send to the browser */</span>
<span style="color: #0000ff;">$filename</span> = <span style="color: #ff0000;">&quot;codediesel.xpi&quot;</span>;
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/**
 * The following header is required for browsers that do not
 * recognize the xpi extension. i.e all browsers other than Firefox.
 * This will display the familiar 'save/open' dialog if the xpi
 * extension is not supported.
 */</span>
<span style="color: #000066;">header</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Content-Disposition: filename={$filename}&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/* Tell the browser that the content that is coming is an xpinstall */</span>
<span style="color: #000066;">header</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Content-type: application/x-xpinstall'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/* Also send the content length */</span>
<span style="color: #000066;">header</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Content-Length: '</span> . <span style="color: #000066;">filesize</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filename</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/* readfile reads the file content and echos it to the output */</span>
<span style="color: #000066;">readfile</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$filename</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Of course if the filesize is large the <em>readfile</em> function could possibly incur a performance hit. But for small files it works good.</p>
<img src="http://feeds.feedburner.com/~r/Code-diesel/~4/sKyPY-d_OvA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/sending-xpi-mime-content-from-php/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.codediesel.com/php/sending-xpi-mime-content-from-php/</feedburner:origLink></item>
		<item>
		<title>Search &amp; replace in files using php</title>
		<link>http://feedproxy.google.com/~r/Code-diesel/~3/Xxgujop2JDc/</link>
		<comments>http://www.codediesel.com/php/search-replace-in-files-using-php/#comments</comments>
		<pubDate>Mon, 11 May 2009 13:57:51 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
		
		<category><![CDATA[libraries]]></category>

		<category><![CDATA[pear]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[files]]></category>

		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=819</guid>
		<description><![CDATA[Searching and replacing content in files is a common task all of us do regularly. Most programmers will implement it using Perl a shell script or through a editor. Perl offers itself as an excellent tool for the required purpose; we PHP programmers are not quite so lucky in that matter. Search/replace is easier from [...]]]></description>
			<content:encoded><![CDATA[<p>Searching and replacing content in files is a common task all of us do regularly. Most programmers will implement it using Perl a shell script or through a editor. Perl offers itself as an excellent tool for the required purpose; we PHP programmers are not quite so lucky in that matter. Search/replace is easier from a shell prompt or an editor, but what if you have to do the same programatically in php. <a target="_blank" href="http://pear.php.net/package/File_SearchReplace/download">File_SearchReplace</a> is a pear package that helps you search/replace in files through a nice object oriented interface.<br />
<span id="more-819"></span><br />
<strong>Installation</strong><br />
Pear installation as usual is simple.</p>

<div class="wp_codebox"><table width="100%" ><tr id="81939"><td class="code" id="819code39"><pre class="dos">c:/&gt; pear install File_SearchReplace</pre></td></tr></table></div>

<p><strong>Doing a simple search &#038; replace</strong><br />
The following is an simple example code that searches the file &#8216;fruits.txt&#8217; and replaces all occurrences of &#8216;apples&#8217; with &#8216;oranges&#8217;. The <em>getNumOccurences</em> function returns the total number of replaced strings in the file.</p>

<div class="wp_codebox"><table width="100%" ><tr id="81940"><td class="code" id="819code40"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #ff0000;">'File/SearchReplace.php'</span> ;
&nbsp;
<span style="color: #0000ff;">$files_to_search</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;fruits.txt&quot;</span><span style="color: #66cc66;">&#41;</span> ;
<span style="color: #0000ff;">$search_string</span>  = <span style="color: #ff0000;">&quot;apples&quot;</span>;
<span style="color: #0000ff;">$replace_string</span> = <span style="color: #ff0000;">&quot;oranges&quot;</span>;
&nbsp;
<span style="color: #0000ff;">$snr</span> = <span style="color: #000000; font-weight: bold;">new</span> File_SearchReplace<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$search_string</span>,
                              <span style="color: #0000ff;">$replace_string</span>,
                              <span style="color: #0000ff;">$files_to_search</span>,
                              <span style="color: #ff0000;">''</span>, <span style="color: #808080; font-style: italic;">// directorie(s) to search</span>
                              <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span> ;
&nbsp;
<span style="color: #0000ff;">$snr</span>-&gt;<span style="color: #006600;">doSearch</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;The number of replaces done : &quot;</span> . <span style="color: #0000ff;">$snr</span>-&gt;<span style="color: #006600;">getNumOccurences</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The fourth option in the <em>File_SearchReplace</em> specifies a optional directory name to search. If the directory name is empty than the files will be searched in the current directory or in the respective path if a path is also included with the filename. Following is an example if you want to search all files in the directory &#8216;nature/fruits&#8217;.</p>

<div class="wp_codebox"><table width="100%" ><tr id="81941"><td class="code" id="819code41"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #ff0000;">'File/SearchReplace.php'</span> ;
&nbsp;
<span style="color: #0000ff;">$files_to_search</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$direc_to_search</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'nature/fruits/'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$search_string</span>  = <span style="color: #ff0000;">&quot;apples&quot;</span>;
<span style="color: #0000ff;">$replace_string</span> = <span style="color: #ff0000;">&quot;oranges&quot;</span>;
&nbsp;
<span style="color: #0000ff;">$snr</span> = <span style="color: #000000; font-weight: bold;">new</span> File_SearchReplace<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$search_string</span>,
                              <span style="color: #0000ff;">$replace_string</span>,
                              <span style="color: #0000ff;">$files_to_search</span>,
                              <span style="color: #0000ff;">$direc_to_search</span>, <span style="color: #808080; font-style: italic;">// directory to search</span>
                              <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span> ; <span style="color: #808080; font-style: italic;">// 'true' to search subdirectories</span>
&nbsp;
<span style="color: #0000ff;">$snr</span>-&gt;<span style="color: #006600;">doSearch</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;The number of replaces done : &quot;</span> . <span style="color: #0000ff;">$snr</span>-&gt;<span style="color: #006600;">getNumOccurences</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>All the four starting options of <em>File_SearchReplace</em> are of mixed type; i.e they take a string or an array of strings as their options. For example in the following all occurrences of &#8216;apples&#8217; will be replaced by &#8216;oranges&#8217; and that of &#8216;pears&#8217; by &#8216;grapes&#8217;.</p>

<div class="wp_codebox"><table width="100%" ><tr id="81942"><td class="code" id="819code42"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #ff0000;">'File/SearchReplace.php'</span> ;
&nbsp;
<span style="color: #0000ff;">$files_to_search</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$direc_to_search</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'nature/fruits/'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$search_string</span>  = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'apples'</span>, <span style="color: #ff0000;">'pears'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$replace_string</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'oranges'</span>, <span style="color: #ff0000;">'grapes'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$snr</span> = <span style="color: #000000; font-weight: bold;">new</span> File_SearchReplace<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$search_string</span>,
                              <span style="color: #0000ff;">$replace_string</span>,
                              <span style="color: #0000ff;">$files_to_search</span>,
                              <span style="color: #0000ff;">$direc_to_search</span>,
                              <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span> ;
<span style="color: #0000ff;">$snr</span>-&gt;<span style="color: #006600;">doSearch</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;The number of replaces done : &quot;</span> . <span style="color: #0000ff;">$snr</span>-&gt;<span style="color: #006600;">getNumOccurences</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>You don&#8217;t have to create a new instance everytime you need a new search, you can set the various parameters through the interface provided by the <em>File_SearchReplace</em> class as shown below.</p>

<div class="wp_codebox"><table width="100%" ><tr id="81943"><td class="code" id="819code43"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #ff0000;">'File/SearchReplace.php'</span> ;
&nbsp;
<span style="color: #0000ff;">$files_to_search</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$direc_to_search</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'nature/fruits/'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$search_string</span>  = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'apples'</span>, <span style="color: #ff0000;">'pears'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$replace_string</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'oranges'</span>, <span style="color: #ff0000;">'grapes'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$snr</span> = <span style="color: #000000; font-weight: bold;">new</span> File_SearchReplace<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$search_string</span>,
                              <span style="color: #0000ff;">$replace_string</span>,
                              <span style="color: #0000ff;">$files_to_search</span>,
                              <span style="color: #0000ff;">$direc_to_search</span>,
                              <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span> ;
<span style="color: #0000ff;">$snr</span>-&gt;<span style="color: #006600;">doSearch</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;The number of replaces done : &quot;</span> . <span style="color: #0000ff;">$snr</span>-&gt;<span style="color: #006600;">getNumOccurences</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/* Start a new search */</span>
&nbsp;
<span style="color: #0000ff;">$snr</span>-&gt;<span style="color: #006600;">setFind</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;oranges&quot;</span><span style="color: #66cc66;">&#41;</span> ;
<span style="color: #0000ff;">$snr</span>-&gt;<span style="color: #006600;">setReplace</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;berries&quot;</span><span style="color: #66cc66;">&#41;</span> ;
<span style="color: #0000ff;">$snr</span>-&gt;<span style="color: #006600;">doSearch</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;The number of replaces done : &quot;</span> . <span style="color: #0000ff;">$snr</span>-&gt;<span style="color: #006600;">getNumOccurences</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>Regular Expression search</strong><br />
You can also use a regular expression in a search string, but before that we must specify what kind of search is required with the &#8217;setSearchFunction&#8217; as shown below. The following example replaces all occurrences of &#8216;color&#8217; or &#8216;colour&#8217; with the capital &#8216;COLOR&#8217;.</p>

<div class="wp_codebox"><table width="100%" ><tr id="81944"><td class="code" id="819code44"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #ff0000;">'File/SearchReplace.php'</span> ;
&nbsp;
<span style="color: #0000ff;">$files_to_search</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$direc_to_search</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'test/graphics/'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$search_string</span>  = <span style="color: #ff0000;">'/col(o|ou)r/'</span>;
<span style="color: #0000ff;">$replace_string</span> = <span style="color: #ff0000;">'COLOR'</span>;
&nbsp;
<span style="color: #0000ff;">$snr</span> = <span style="color: #000000; font-weight: bold;">new</span> File_SearchReplace<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$search_string</span>,
                              <span style="color: #0000ff;">$replace_string</span>,
                              <span style="color: #0000ff;">$files_to_search</span>,
                              <span style="color: #0000ff;">$direc_to_search</span>,
                              <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span> ;
&nbsp;
<span style="color: #0000ff;">$snr</span>-&gt;<span style="color: #006600;">setSearchFunction</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;preg&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$snr</span>-&gt;<span style="color: #006600;">doSearch</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000066;">echo</span> <span style="color: #ff0000;">&quot;The number of replaces done : &quot;</span> . <span style="color: #0000ff;">$snr</span>-&gt;<span style="color: #006600;">getNumOccurences</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The <em>setSearchFunction</em> takes one of the following four options:</p>
<p>normal - default<br />
quick - use str_replace()<br />
preg - use preg_replace()<br />
ereg - use ereg_replace()</p>
<p>More information on this options can be found <a target="_blank" href="http://pear.php.net/manual/en/package.filesystem.file-searchreplace.intro.php">here</a>.</p>
<p><strong>In conclusion</strong><br />
The package can be quite useful when you want to replace large quantities of text programatically. As it works on plain strings and regular expressions, its can be quite a handy tool in many occasions.</p>
<img src="http://feeds.feedburner.com/~r/Code-diesel/~4/Xxgujop2JDc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/search-replace-in-files-using-php/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.codediesel.com/php/search-replace-in-files-using-php/</feedburner:origLink></item>
	</channel>
</rss>
