<?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>code-diesel &#187; tip</title>
	<atom:link href="http://www.codediesel.com/category/tip/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Read the version of a PDF in PHP</title>
		<link>http://www.codediesel.com/php/read-the-version-of-a-pdf-in-php/</link>
		<comments>http://www.codediesel.com/php/read-the-version-of-a-pdf-in-php/#comments</comments>
		<pubDate>Thu, 21 Jul 2011 13:09:01 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[pdf]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2862</guid>
		<description><![CDATA[The following is a very short code to read the version number of a PDF document using PHP. I needed this recently during a PDF processing app developed in PHP. As Adobe uses different compression methods in various versions, it becomes necessary to be able to identify the version of the PDF under work. &#60;?php [...]]]></description>
			<content:encoded><![CDATA[<p>The following is a very short code to read the version number of a PDF document using PHP. I needed this recently during a PDF processing app developed in PHP. As Adobe uses different compression methods in various versions, it becomes necessary to be able to identify the version of the PDF under work.<br />
<span id="more-2862"></span></p>

<div class="wp_codebox"><table><tr id="p28622"><td class="code" id="p2862code2"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> pdfVersion<span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span> 
    <span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rb'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Reset file pointer to the start */</span>
    <span style="color: #990000;">fseek</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Read 20 bytes from the start of the PDF */</span>
    <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/\d\.\d/'</span><span style="color: #339933;">,</span><span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$match</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$match</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</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: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> 
&nbsp;
<span style="color: #000088;">$version</span> <span style="color: #339933;">=</span> pdfVersion<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;example.pdf&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/read-the-version-of-a-pdf-in-php/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Tail functionality in PHP</title>
		<link>http://www.codediesel.com/php/tail-functionality-in-php/</link>
		<comments>http://www.codediesel.com/php/tail-functionality-in-php/#comments</comments>
		<pubDate>Tue, 19 Jul 2011 03:38:06 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2860</guid>
		<description><![CDATA[Frequently one needs to get the last few lines of some log files, whether php error logs or Apache logs. Most of these file sizes run into megabytes, which makes it difficult and time consuming to remotely open them using ftp. If you have shell access then you can easily use the *nix &#8216;tail&#8217; command [...]]]></description>
			<content:encoded><![CDATA[<p>Frequently one needs to get the last few lines of some log files, whether php error logs or Apache logs. Most of these file sizes run into megabytes, which makes it difficult and time consuming to remotely open them using ftp. If you have shell access then you can easily use the *nix &#8216;tail&#8217; command to return the last &#8216;n&#8217; lines; or one can use the following if shell access is not enabled on your hosting.</p>

<div class="wp_codebox"><table><tr id="p28606"><td class="code" id="p2860code6"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> Tail<span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #990000;">system</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tail -n 20 '</span><span style="color: #339933;">.</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-2860"></span><br />
Sometimes even the &#8216;system&#8217; command is disabled for security reasons, which brings us to the last solution &#8211; coding the tail command in PHP. The following is a simple but useful &#8216;tail&#8217; implementation in PHP. I&#8217;ve encapsulated the tail function in a &#8216;LogRead&#8217; class, which can be further enlarged by adding other useful log functions.</p>

<div class="wp_codebox"><table><tr id="p28607"><td class="code" id="p2860code7"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> LogRead
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filename</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$filename</span><span style="color: #339933;">;</span>
    <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> tail<span style="color: #009900;">&#40;</span><span style="color: #000088;">$lines</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</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: #0000ff;">''</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filename</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$block</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">4096</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$max</span> <span style="color: #339933;">=</span> <span style="color: #990000;">filesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$len</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$len</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$max</span><span style="color: #339933;">;</span> <span style="color: #000088;">$len</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$block</span><span style="color: #009900;">&#41;</span> 
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$seekSize</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$max</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$len</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$block</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$block</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$max</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$len</span><span style="color: #339933;">;</span>
            <span style="color: #990000;">fseek</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$len</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$seekSize</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> SEEK_END<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$seekSize</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr_count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$lines</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> 
            <span style="color: #009900;">&#123;</span>
                <span style="color: #009933; font-style: italic;">/* Make sure that the last line ends with a '\n' */</span>
                <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><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: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</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: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;!(.*?<span style="color: #000099; font-weight: bold;">\n</span>){&quot;</span><span style="color: #339933;">.</span> <span style="color: #000088;">$lines</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;}$!&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Which you can use as following:</p>

<div class="wp_codebox"><table><tr id="p28608"><td class="code" id="p2860code8"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$log</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LogRead<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;errors.log&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Get the last 6 lines */</span>
<span style="color: #000088;">$lines</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$log</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tail</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$lines</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><em>adapted from &#8216;steve at studio831 dot com&#8217;</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/tail-functionality-in-php/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Grabbing website Favicons using curl and php</title>
		<link>http://www.codediesel.com/php/grabbing-website-favicons-with-curl-php/</link>
		<comments>http://www.codediesel.com/php/grabbing-website-favicons-with-curl-php/#comments</comments>
		<pubDate>Fri, 27 May 2011 10:26:41 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[favicons]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2823</guid>
		<description><![CDATA[Google Shared Stuff allows one to easily get the Favicon of any website with a single line. For instance to get the Favicon of yahoo.com we can use the following in a browser. http://www.google.com/s2/favicons?domain=yahoo.com We can do do the same using PHP and curl using the following code. &#60;?php &#160; function getSiteFavicon&#40;$url&#41; &#123; $ch = [...]]]></description>
			<content:encoded><![CDATA[<p>Google Shared Stuff allows one to easily get the Favicon of any website with a single line. For instance to get the Favicon of yahoo.com we can use the following in a browser.</p>

<div class="wp_codebox"><table><tr id="p282313"><td class="code" id="p2823code13"><pre class="html" style="font-family:monospace;">http://www.google.com/s2/favicons?domain=yahoo.com</pre></td></tr></table></div>

<p>We can do do the same using PHP and curl using the following code.<br />
<span id="more-2823"></span></p>

<div class="wp_codebox"><table><tr id="p282314"><td class="code" id="p2823code14"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> getSiteFavicon<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.google.com/s2/favicons?domain='</span><span style="color: #339933;">.</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_TIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_FOLLOWLOCATION<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
    <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: image/png; charset=utf-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  getSiteFavicon<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;yahoo.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This will display the Favicon in the browser. An even more interesting thing we can do is to save the same to a PNG file. A little modification to the above code will yield the following.</p>

<div class="wp_codebox"><table><tr id="p282315"><td class="code" id="p2823code15"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> saveSiteFavicon<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/* Drop the TLD from the url */</span>
    <span style="color: #000088;">$saveFileName</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$saveFileName</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.png'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w+'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.google.com/s2/favicons?domain='</span><span style="color: #339933;">.</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_TIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Save the returned data to a file */</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_FILE<span style="color: #339933;">,</span> <span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_FOLLOWLOCATION<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
  saveSiteFavicon<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;yahoo.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Below is a more elaborate code to grab Favicons from a list of urls and combine them into a single image.</p>

<div class="wp_codebox"><table><tr id="p282316"><td class="code" id="p2823code16"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> getSiteFavicon<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.google.com/s2/favicons?domain='</span><span style="color: #339933;">.</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_TIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_FOLLOWLOCATION<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009933; font-style: italic;">/* Urls to grab the Favicon of */</span>
<span style="color: #000088;">$urls</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;yahoo.com&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;microsoft.com&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;wired.com&quot;</span><span style="color: #339933;">,</span>
              <span style="color: #0000ff;">&quot;cakephp.org&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;wordpress.com&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;msn.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Create a 180px by 40px blank image */</span>
<span style="color: #000088;">$dest</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreate</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">180</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$urls</span> <span style="color: #000000; font-weight: bold;">as</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/* Get the Favicon data for a url */</span>
    <span style="color: #000088;">$imageData</span> <span style="color: #339933;">=</span> getSiteFavicon<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Create a GD image resource ffrom the data */</span>
    <span style="color: #000088;">$src</span> <span style="color: #339933;">=</span> <span style="color: #990000;">imagecreatefromstring</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$imageData</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* Copy each Favicon image to the blank canvas */</span>
    <span style="color: #990000;">imagecopy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dest</span><span style="color: #339933;">,</span> <span style="color: #000088;">$src</span><span style="color: #339933;">,</span> <span style="color: #000088;">$x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* change the x,y offset for the next favicon */</span>
    <span style="color: #000088;">$x</span> <span style="color: #339933;">+=</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">60</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$y</span> <span style="color: #339933;">+=</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Output the newly created image */</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: image/gif'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagegif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dest</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dest</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The above code will create the following image in your browser.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2011/05/favicons.gif"><img src="http://www.codediesel.com/wp-content/uploads/2011/05/favicons.gif" alt="" title="favicons" width="180" height="40" class="aligncenter size-full wp-image-2825" /></a></p>
<p>A little more code change and you can save the image to a file.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/grabbing-website-favicons-with-curl-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Searching server files using GREP</title>
		<link>http://www.codediesel.com/php/searching-server-files-using-grep/</link>
		<comments>http://www.codediesel.com/php/searching-server-files-using-grep/#comments</comments>
		<pubDate>Thu, 26 May 2011 05:38:52 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2821</guid>
		<description><![CDATA[During debugging the majority of my time is spent in searching for specific text. Searching on the local machine is a breeze via a IDE, but if I&#8217;ve an application hosted on a remote server then it can get difficult and slow, unless of course you have access to SSH when you can use good-old [...]]]></description>
			<content:encoded><![CDATA[<p>During debugging the majority of my time is spent in searching for specific text. Searching on the local machine is a breeze via a IDE, but if I&#8217;ve an application hosted on a remote server then it can get difficult and slow, unless of course you have access to SSH when you can use good-old Grep. But many times the server does not have SSH access at which time I have to rely on using Linux commands though PHP.</p>
<p>So for example the following will allow me to recursively search all the files in a directory for the string &#8221; mail&#8221;.  I just upload the file to the server directory from where I need to make the search. Here I&#8217;m using the PHP <a href="http://php.net/manual/en/language.operators.execution.php" rel="nofollow" target="_blank" >backtick</a> operator that allows me to escape to the shell, execute the specified command, and return the result in the variable $output.<br />
<span id="more-2821"></span></p>

<div class="wp_codebox"><table><tr id="p282121"><td class="code" id="p2821code21"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> `grep <span style="color: #339933;">-</span>r <span style="color: #0000ff;">&quot; mail&quot;</span> <span style="color: #339933;">.</span>`<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The equivalent of the above using the <strong>shell_exec</strong> function is shown below.</p>

<div class="wp_codebox"><table><tr id="p282122"><td class="code" id="p2821code22"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #990000;">shell_exec</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'grep -r &quot; mail&quot; .'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Note that the backtick operator and <strong>shell_exec</strong> are disabled if php is running in safe_mode so you will need to temporarily enable it to run the above code.</p>
<p>The above code searches for the specified string in all the files, but if you need to limit the search for a specific file type than you have to use <strong>find</strong> in combination with <strong>grep</strong>. So, the following will search for the string &#8216; mail&#8217; only in files with the .php extension.</p>

<div class="wp_codebox"><table><tr id="p282123"><td class="code" id="p2821code23"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> `find <span style="color: #339933;">.</span> <span style="color: #339933;">-</span>iname <span style="color: #0000ff;">'*php'</span> <span style="color: #339933;">|</span> xargs grep <span style="color: #0000ff;">' mail'</span> `<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>By default grep lists the matched filename and also the matched line, but if you need to only list the filenames that contain the search term, you can use the &#8216;-l&#8217; option with grep.</p>

<div class="wp_codebox"><table><tr id="p282124"><td class="code" id="p2821code24"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> `find <span style="color: #339933;">.</span> <span style="color: #339933;">-</span>iname <span style="color: #0000ff;">'*php'</span> <span style="color: #339933;">|</span> xargs grep <span style="color: #0000ff;">' mail'</span> <span style="color: #339933;">-</span>l `<span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/searching-server-files-using-grep/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Fixing aspect-ratio of images in PHP</title>
		<link>http://www.codediesel.com/php/fixing-aspect-ratio-of-images-in-php/</link>
		<comments>http://www.codediesel.com/php/fixing-aspect-ratio-of-images-in-php/#comments</comments>
		<pubDate>Wed, 15 Sep 2010 14:29:28 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[image]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2701</guid>
		<description><![CDATA[Frequently when you are displaying images on a page using a fixed width and height , the images come out stretched or squeezed. This is because the aspect ratio of the images have not been maintained. In such a case you can use the following script to correctly display the images according to the aspect-ratio [...]]]></description>
			<content:encoded><![CDATA[<p>Frequently when you are displaying images on a page using a fixed width and height , the images come out stretched or squeezed. This is because the aspect ratio of the images have not been maintained. In such a case you can use the following script to correctly display the images according to the aspect-ratio of the image without actually resizing the image in a editor. This can be handy when you need to quickly correct image distortions. Here you need to keep either the width or the height fixed, so the other dimension can be calculated. Below the width is kept fixed at 200px, and the height is varied depending on the aspect ratio of the image, thus displaying the image without any distortion.</p>

<div class="wp_codebox"><table><tr id="p270126"><td class="code" id="p2701code26"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$imageFile</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'path/to/image'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Set the width fixed at 200px; */</span>
<span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">200</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Get the image info */</span>
<span style="color: #000088;">$info</span> <span style="color: #339933;">=</span> <span style="color: #990000;">getimagesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$imageFile</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Calculate aspect ratio by dividing height by width */</span>
<span style="color: #000088;">$aspectRatio</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$info</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$info</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Keep the width fix at 100px, 
   but change the height according to the aspect ratio */</span>
<span style="color: #000088;">$newHeight</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$aspectRatio</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$width</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;px&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Use the 'newHeight' in the CSS height property below. */</span>
<span style="color: #000088;">$width</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;px&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;&lt;img style=<span style="color: #000099; font-weight: bold;">\&quot;</span>width: <span style="color: #006699; font-weight: bold;">$width</span>; height: <span style="color: #006699; font-weight: bold;">$newHeight</span>;<span style="color: #000099; font-weight: bold;">\&quot;</span> 
       src=<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$imageFile</span><span style="color: #000099; font-weight: bold;">\&quot;</span> /&gt;&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/fixing-aspect-ratio-of-images-in-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Deleting pesky blank lines from source files</title>
		<link>http://www.codediesel.com/tip/deleting-pesky-blank-lines-in-source-files/</link>
		<comments>http://www.codediesel.com/tip/deleting-pesky-blank-lines-in-source-files/#comments</comments>
		<pubDate>Thu, 25 Mar 2010 12:46:24 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[tip]]></category>
		<category><![CDATA[editing]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/tip/getting-rid-of-those-extra-blank-lines/</guid>
		<description><![CDATA[Removing blank lines from html and other source files]]></description>
			<content:encoded><![CDATA[<p>Blank lines are an eye sore for source files. During the last few weeks I&#8217;ve been shuttling php and html files from various host servers and my PC. During the whole ordeal the source files kept adding a huge number of blank lines. At one point an &#8216;if&#8217; construct in one of my PHP code was about 60 lines away from the next starting brace. This was all caused by the non standard <a href="http://en.wikipedia.org/wiki/Newline" rel="nofollow" target="_blank" >newline character</a>, also known as a line break or end-of-line (EOL) character. As we all know, it is a special character or sequence of characters signifying the end of a line of text. Unfortunately the actual codes representing the newline differ across operating systems, which creates problems such as the above when exchanging data between systems. If you think you don&#8217;t use *nix systems and are therefore free from these problems then you are wrong. Even though you may be using Windows, most host servers are Linux based, and so transferring files back and forth from these servers can create these kind of problems.<br />
<span id="more-2620"></span></p>
<h4>Viewing EOL markers</h4>
<p>I regularly use <a href="http://notepad-plus.sourceforge.net/uk/site.htm" rel="nofollow" target="_blank" >Notepad++</a> for editing purposes. In Notepad++ we can see the EOL markers with the &#8216;View->Show Symbol->Show End of Line&#8217; menu.</p>
<p>The following images shows the EOL markers for Windows and Unix.</p>
<p><strong>Windows</strong><br />
<a href="http://www.codediesel.com/wp-content/uploads/2010/03/eol-markers1.png"><img style="padding:5px; border: 1px solid #c0c0c0;" src="http://www.codediesel.com/wp-content/uploads/2010/03/eol-markers1.png" alt="eol-markers windows" title="eol-markers windows" width="473" height="322" class="aligncenter size-full wp-image-2623" /></a></p>
<p><strong>Unix</strong><br />
<a href="http://www.codediesel.com/wp-content/uploads/2010/03/eol-markers2.png"><img style="padding:5px; border: 1px solid #c0c0c0;" src="http://www.codediesel.com/wp-content/uploads/2010/03/eol-markers2.png" alt="eol-markers windows" title="eol-markers windows" width="473" height="322" class="aligncenter size-full wp-image-2623" /></a></p>
<h4>Line terminations</h4>
<p>As you can see Windows files are terminated by the sequence CR+LF (\r\n) and Unix by LF(\n) character. There is a wonderful history behind the newline character, which <a href="http://en.wikipedia.org/wiki/Newline" rel="nofollow" target="_blank" >Wikipedia</a> explains quite nicely. Our main job here is to know how to quickly delete those annoying blank lines.</p>
<h4>Deleting blank lines</h4>
<p>A quick way to delete blank lines from a file using Notepad++ is to use the &#8216;Search-Replace&#8217; functionality of the editor as shown below. Note that we are using the &#8216;Extended&#8217; search mode for the replacements. Using &#8216;Plain Text&#8217; search mode will not work. If you are using another text editor use the &#8216;Regular expression&#8217; search mode if it has one.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2010/03/delete1.png"><img src="http://www.codediesel.com/wp-content/uploads/2010/03/delete1.png" alt="deleting new lines" title="deleting new lines" width="478" height="315" class="aligncenter size-full wp-image-2632" /></a></p>
<p>For deleting blank lines from a Windows file use the string &#8216;\r\n\r\n&#8217; in the search field, and a blank string for the replacement field. For files with Unix EOL markers use the &#8216;\n\n&#8217; string for the search field. Note the double &#8216;\r\n&#8217; and &#8216;\n&#8217;. Specifying a single &#8216;\n&#8217; or &#8216;\r\n&#8217; will delete all the EOL markers and convert the text to a single loooong string. Of course if you are using Notepad++ then you have at your disposal the wonderful TextFx plugin; besides a ton of functions, it also lets you delete blank lines from a file. But if you are using any other editor without the functionality then this tip can come handy.</p>
<h4>Converting hard tabs to soft tabs</h4>
<p>While editing source files I prefer using soft tabs rather than hard tabs, this helps me preserve my code formatting when viewed in another editor which has a different tab setting. Incidentally you can convert hard tabs in your code to soft tabs by replacing &#8216;\t&#8217; with a <em>n</em> number of spaces as given above.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/tip/deleting-pesky-blank-lines-in-source-files/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>7 essential php command-line options</title>
		<link>http://www.codediesel.com/php/7-essential-php-commandline-options/</link>
		<comments>http://www.codediesel.com/php/7-essential-php-commandline-options/#comments</comments>
		<pubDate>Sat, 12 Dec 2009 05:30:24 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[addins]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[php cli]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2163</guid>
		<description><![CDATA[some essential php command-line options for your use]]></description>
			<content:encoded><![CDATA[<p>Most of us use PHP from a IDE or using a simple text editor with a browser, rarely dropping down to the command-line for running php programs. But php provides some interesting and quick options you can use to perform various common tasks or to debug some nasty installation problems. Below is a list of some useful options you should be familiar with.<br />
<span id="more-2163"></span><br />
There are two ways to specify options on the command line &#8211; <em>Short Option</em>, where you specify a option by a single character and a <em>Long Option</em>, where you use a more readable string to specify the option, such as &#8216;&#8211;php-ini&#8217;. In this post we will be using the <em>Short Option</em>.</p>
<h3>PHP Command-line options</h3>
<h4>1 : Using a different php.ini file</h4>
<p>The following option can be useful when you need to test various ini configurations, or you need to check if all the modules in a particular ini are loading correctly.</p>

<div class="wp_codebox"><table><tr id="p216327"><td class="code" id="p2163code27"><pre class="text" style="font-family:monospace;">php –c  PATH/to/PHP.INI example.php</pre></td></tr></table></div>

<h4>2 : Check to see for any syntax errors in a php file.</h4>
<p>Need to quickly check for php syntax errors if you do not have access to an IDE, then this option can be quite helpful. Combine it with some shell code and you can easily check for syntax errors for multiple files in a directory.</p>

<div class="wp_codebox"><table><tr id="p216328"><td class="code" id="p2163code28"><pre class="text" style="font-family:monospace;">php –l example.php</pre></td></tr></table></div>

<p>You can for example use the windows command line script to scan all the php files in the current directory for syntax errors.</p>

<div class="wp_codebox"><table><tr id="p216329"><td class="code" id="p2163code29"><pre class="dos" style="font-family:monospace;">c:\localhost&gt;for <span style="color: #33cc33;">%%</span><span style="color: #448888;">X</span> <span style="color: #00b100; font-weight: bold;">in</span> <span style="color: #66cc66;">&#40;</span>*.php<span style="color: #66cc66;">&#41;</span> <span style="color: #00b100; font-weight: bold;">do</span> php -l <span style="color: #33cc33;">%%</span><span style="color: #448888;">X</span></pre></td></tr></table></div>

<h4>3 : Display php builtin and loaded modules</h4>

<div class="wp_codebox"><table><tr id="p216330"><td class="code" id="p2163code30"><pre class="text" style="font-family:monospace;">php –m</pre></td></tr></table></div>

<p>Combine it with php –c to debug any module loading errors for different php.ini files</p>

<div class="wp_codebox"><table><tr id="p216331"><td class="code" id="p2163code31"><pre class="text" style="font-family:monospace;">php –c PATH/to/PHP.INI -m</pre></td></tr></table></div>

<p>This came in handy when I was installing php 5.3 and for some reasons the xdebug extension was not loading. With the help of the above command I was able to narrow down the problem to the following reason:</p>

<div class="wp_codebox"><table><tr id="p216332"><td class="code" id="p2163code32"><pre class="text" style="font-family:monospace;">Xdebug requires Zend Engine API version 220090115.
The Zend Engine API version 220090626 which is installed, is newer.
Contact Derick Rethans at http://xdebug.org for a later 
version of Xdebug.</pre></td></tr></table></div>

<h4>4 : Syntax Highlighting</h4>
<p>The following option will display syntax highlighted text using php’s internal mechanism.</p>

<div class="wp_codebox"><table><tr id="p216333"><td class="code" id="p2163code33"><pre class="text" style="font-family:monospace;">php  –s  example.php</pre></td></tr></table></div>

<p>You can then write the output to another file using a redirect.</p>

<div class="wp_codebox"><table><tr id="p216334"><td class="code" id="p2163code34"><pre class="text" style="font-family:monospace;">php  –s  example.php  &gt;  syntax.html</pre></td></tr></table></div>

<h4>5 : Strip the source code of comments and whitespace.</h4>
<p>Can be useful if you need to reduce the file size.</p>

<div class="wp_codebox"><table><tr id="p216335"><td class="code" id="p2163code35"><pre class="text" style="font-family:monospace;">php –w example.php</pre></td></tr></table></div>

<h4>6 : Run php code from the command line</h4>
<p>Directly run php code from the command line without using the php start and end tags.</p>

<div class="wp_codebox"><table><tr id="p216336"><td class="code" id="p2163code36"><pre class="text" style="font-family:monospace;">php -r &quot;$foo = 'hello!'; $foo = strtoupper($foo); echo $foo;&quot;</pre></td></tr></table></div>

<h4>7 : Display information related to a internal function, class or extension</h4>
<p><i>(Available as of PHP 5.1.2)</i><br />
These options can be quite handy when a PHP manual is not within reach.</p>

<div class="wp_codebox"><table><tr id="p216337"><td class="code" id="p2163code37"><pre class="text" style="font-family:monospace;">php --r[fcei]</pre></td></tr></table></div>

<p>Show information about a function:</p>

<div class="wp_codebox"><table><tr id="p216338"><td class="code" id="p2163code38"><pre class="text" style="font-family:monospace;">php --rf usort</pre></td></tr></table></div>

<p>Example output for the above:</p>

<div class="wp_codebox"><table><tr id="p216339"><td class="code" id="p2163code39"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">Function</span> <span style="color: #009900;">&#91;</span> <span style="color: #339933;">&lt;</span>internal<span style="color: #339933;">:</span>standard<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">usort</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #339933;">-</span> Parameters <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
    Parameter <span style="color: #009933; font-style: italic;">#0 [ &lt;required&gt; &amp;$arg ]
</span>    Parameter <span style="color: #009933; font-style: italic;">#1 [ &lt;required&gt; $cmp_function ]
</span>  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Show information about a class:</p>

<div class="wp_codebox"><table><tr id="p216340"><td class="code" id="p2163code40"><pre class="text" style="font-family:monospace;">php --rc EmptyIterator</pre></td></tr></table></div>

<p>Example output for the above:</p>

<div class="wp_codebox"><table><tr id="p216341"><td class="code" id="p2163code41"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">Class</span> <span style="color: #009900;">&#91;</span> <span style="color: #339933;">&lt;</span>internal<span style="color: #339933;">:</span>SPL<span style="color: #339933;">&gt;</span> <span style="color: #339933;">&lt;</span>iterateable<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">class</span> EmptyIterator 
          implements Iterator<span style="color: #339933;">,</span> Traversable <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #339933;">-</span> Constants <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #339933;">-</span> Static properties <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #339933;">-</span> Static methods <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #339933;">-</span> Properties <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #339933;">-</span> Methods <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
    Method <span style="color: #009900;">&#91;</span> <span style="color: #339933;">&lt;</span>internal<span style="color: #339933;">:</span>SPL<span style="color: #339933;">,</span> prototype Iterator<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">public</span> method <span style="color: #990000;">rewind</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
    Method <span style="color: #009900;">&#91;</span> <span style="color: #339933;">&lt;</span>internal<span style="color: #339933;">:</span>SPL<span style="color: #339933;">,</span> prototype Iterator<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">public</span> method valid <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
    Method <span style="color: #009900;">&#91;</span> <span style="color: #339933;">&lt;</span>internal<span style="color: #339933;">:</span>SPL<span style="color: #339933;">,</span> prototype Iterator<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">public</span> method <span style="color: #990000;">key</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
    Method <span style="color: #009900;">&#91;</span> <span style="color: #339933;">&lt;</span>internal<span style="color: #339933;">:</span>SPL<span style="color: #339933;">,</span> prototype Iterator<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">public</span> method <span style="color: #990000;">current</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
    Method <span style="color: #009900;">&#91;</span> <span style="color: #339933;">&lt;</span>internal<span style="color: #339933;">:</span>SPL<span style="color: #339933;">,</span> prototype Iterator<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">public</span> method <span style="color: #990000;">next</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Show information about a extension:</p>

<div class="wp_codebox"><table><tr id="p216342"><td class="code" id="p2163code42"><pre class="text" style="font-family:monospace;">php  --re json</pre></td></tr></table></div>

<p>Example output for the above:</p>

<div class="wp_codebox"><table><tr id="p216343"><td class="code" id="p2163code43"><pre class="php" style="font-family:monospace;">xtension <span style="color: #009900;">&#91;</span> <span style="color: #339933;">&lt;</span>persistent<span style="color: #339933;">&gt;</span> extension <span style="color: #009933; font-style: italic;">#10 json version 1.2.1 ] {
</span>
  <span style="color: #339933;">-</span> Functions <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">Function</span> <span style="color: #009900;">&#91;</span> <span style="color: #339933;">&lt;</span>internal<span style="color: #339933;">:</span>json<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">json_encode</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">Function</span> <span style="color: #009900;">&#91;</span> <span style="color: #339933;">&lt;</span>internal<span style="color: #339933;">:</span>json<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">json_decode</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Show configuration information for a particular extension:</p>

<div class="wp_codebox"><table><tr id="p216344"><td class="code" id="p2163code44"><pre class="text" style="font-family:monospace;">php  --ri tidy</pre></td></tr></table></div>

<p>Example output for the above:</p>

<div class="wp_codebox"><table><tr id="p216345"><td class="code" id="p2163code45"><pre class="php" style="font-family:monospace;">tidy
&nbsp;
Tidy support <span style="color: #339933;">=&gt;</span> enabled
libTidy Release <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">15</span> August <span style="color: #cc66cc;">2007</span>
Extension Version <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">2.0</span>
<span style="color: #009900;">&#40;</span><span style="color: #000088;">$Id</span><span style="color: #339933;">:</span> tidy<span style="color: #339933;">.</span>c<span style="color: #339933;">,</span>v 1<span style="color: #339933;">.</span>66<span style="color: #339933;">.</span>2<span style="color: #339933;">.</span>8<span style="color: #339933;">.</span>2<span style="color: #339933;">.</span>24 <span style="color: #cc66cc;">2007</span><span style="color: #339933;">/</span><span style="color:#800080;">09</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">20</span> <span style="color: #cc66cc;">22</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">25</span><span style="color: #339933;">:</span><span style="color: #208080;">05</span> nlopess <span style="color: #990000;">Exp</span> $<span style="color: #009900;">&#41;</span>
&nbsp;
Directive <span style="color: #339933;">=&gt;</span> Local Value <span style="color: #339933;">=&gt;</span> Master Value
tidy<span style="color: #339933;">.</span>default_config <span style="color: #339933;">=&gt;</span> no value <span style="color: #339933;">=&gt;</span> no value
tidy<span style="color: #339933;">.</span>clean_output <span style="color: #339933;">=&gt;</span> no value <span style="color: #339933;">=&gt;</span> no value</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/7-essential-php-commandline-options/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Simulating Packages in PHP</title>
		<link>http://www.codediesel.com/php/simulating-packages-in-php/</link>
		<comments>http://www.codediesel.com/php/simulating-packages-in-php/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 06:36:15 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[package]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1842</guid>
		<description><![CDATA[how to simulate java like import packages in php]]></description>
			<content:encoded><![CDATA[<p>Most modern languages have a concept of packages, wherein related classes are stored together. PHP sadly doesn&#8217;t have a similar concept. For example in Java we can use the following line to imports all classes from the java.awt.event package.</p>

<div class="wp_codebox"><table><tr id="p184246"><td class="code" id="p1842code46"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.event.*</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><span id="more-1842"></span><br />
But in the spirit of &#8216;programming into the language&#8217;, what we can do is try to simulate a &#8216;package-like&#8217; concept in PHP. Take the following directory structure.<br />
<a href="http://www.codediesel.com/wp-content/uploads/2009/11/package_dir_struct1.gif"><img src="http://www.codediesel.com/wp-content/uploads/2009/11/package_dir_struct1.gif" alt="package_dir_struct1" title="package_dir_struct1" width="273" height="180" class="aligncenter size-full wp-image-1849" /></a></p>
<p>So now if we want to import the &#8216;login.php&#8217; file in our code we must be able to do the following with a newly created &#8216;import&#8217; function:</p>

<div class="wp_codebox"><table><tr id="p184247"><td class="code" id="p1842code47"><pre class="php" style="font-family:monospace;">import<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.codediesel.Security.login&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This will include the &#8216;login.php&#8217; file from the &#8216;com/codediesel/Security&#8217; directory. To include all the files from the &#8216;Security&#8217; directory we can do the following:</p>

<div class="wp_codebox"><table><tr id="p184248"><td class="code" id="p1842code48"><pre class="php" style="font-family:monospace;">import<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.codediesel.Security.*);</span></pre></td></tr></table></div>

<p> As you can see that this is actually quite different from what the actual package concept implies, which is to import <em>related classes</em>. </p>
<p>Anyways, taking it as a conceptual idea only the code for the import function is shown below:</p>

<div class="wp_codebox"><table><tr id="p184249"><td class="code" id="p1842code49"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$basePath</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;./&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> import<span style="color: #009900;">&#40;</span><span style="color: #000088;">$classPath</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$basePath</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/* If the path ends with a '.*' then include
       all the files in the last given directory.
    */</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/(\.\*)$/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$classPath</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$importFilePath</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$classPath</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$classPath</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$importFilePath</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$importFilePath</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$d</span><span style="color: #339933;">=</span> <span style="color: #990000;">dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$basePath</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$importFilePath</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">!==</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$d</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
        <span style="color: #009900;">&#123;</span>
            <span style="color: #009933; font-style: italic;">/* Reject parent, current directories and sub directories */</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'.'</span><span style="color: #009900;">&#41;</span>  <span style="color: #339933;">||</span>
               <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'..'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span>
               <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$d</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">path</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$basePath</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$importFilePath</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #009933; font-style: italic;">/* If a single file is specified */</span>
        <span style="color: #000088;">$importFile</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$classPath</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;.php&quot;</span><span style="color: #339933;">;</span>
    	<span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$basePath</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$importFile</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Please note that this is just an idea. Detailed error handling is not included to keep the code conceptually simple to understand. It would be fun if readers could bounce around some thoughts.</p>
<h4>Now the Problems</h4>
<p>The first main problem is that this is not exactly what the package concept implies. What we are essentially doing is dressing-up the &#8216;include&#8217; concept in new clothes.</p>
<p>The second is that for multiple file inclusion as shown above, we cannot predict in which sequence the files will be included, which can raise some dependency issues.</p>
<p>Including a large number of files can incur a small performance hit. This can be better handled if developed as a PECL extension.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/simulating-packages-in-php/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Creating a .htaccess file on Windows</title>
		<link>http://www.codediesel.com/tip/creating-a-htaccess-on-windows/</link>
		<comments>http://www.codediesel.com/tip/creating-a-htaccess-on-windows/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 12:10:39 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[tip]]></category>
		<category><![CDATA[apache]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1312</guid>
		<description><![CDATA[quick htaccess file creation on windows]]></description>
			<content:encoded><![CDATA[<p>Everyone who has tried creating a .htaccess on windows knows that Windows Explorer does not accept the &#8216;.&#8217; character as the first character of a file.  For most of you this may be old stuff, but for the newbies, the quick way to create the file is to open the command prompt and type:</p>

<div class="wp_codebox"><table><tr id="p131250"><td class="code" id="p1312code50"><pre class="text" style="font-family:monospace;">copy con .htaccess [press enter]
[press ctrl Z]</pre></td></tr></table></div>

<p>The second way is to:<br />
Open notepad and save the document with file name .htaccess with the &#8216;save as type&#8217; set to &#8216;All Files&#8217;; or save the document with file name as &#8220;.htaccess&#8221; including the quotes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/tip/creating-a-htaccess-on-windows/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Pushing xpi mime content from php</title>
		<link>http://www.codediesel.com/php/sending-xpi-mime-content-from-php/</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 &#8216;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 &#8216;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><tr id="p88053"><td class="code" id="p880code53"><pre class="apache" style="font-family:monospace;"><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><tr id="p88054"><td class="code" id="p880code54"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
&nbsp;
<span style="color: #009933; font-style: italic;">/* File we want to send to the browser */</span>
<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;codediesel.xpi&quot;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #009933; 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: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Disposition: filename=<span style="color: #006699; font-weight: bold;">{$filename}</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #009933; font-style: italic;">/* Tell the browser that the content that is coming is an xpinstall */</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-type: application/x-xpinstall'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #009933; font-style: italic;">/* Also send the content length */</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Length: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">filesize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #009933; font-style: italic;">/* readfile reads the file content and echos it to the output */</span>
<span style="color: #990000;">readfile</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/sending-xpi-mime-content-from-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

