<?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>Ersin Acar &#187; PHP</title>
	<atom:link href="http://ersinacar.com/category/php/feed" rel="self" type="application/rss+xml" />
	<link>http://ersinacar.com</link>
	<description>PHP and Web Technologies Freak</description>
	<lastBuildDate>Fri, 06 Jan 2012 14:19:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>PHP 5.3 Released!! Horeyyy!!</title>
		<link>http://ersinacar.com/php-5-3-released-horeyyy_168.html</link>
		<comments>http://ersinacar.com/php-5-3-released-horeyyy_168.html#comments</comments>
		<pubDate>Mon, 27 Jul 2009 08:41:22 +0000</pubDate>
		<dc:creator>Ersin Acar</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[namespaces]]></category>
		<category><![CDATA[php 5.3]]></category>

		<guid isPermaLink="false">http://ersinacar.com/?p=168</guid>
		<description><![CDATA[I can&#8217;t blog much cuz of my works. I&#8217;m pretty busy in these days. When i check php.net, i saw PHP 5.3 released post in 30 Jun -.- nevermind. After a long string of delays, PHP 5.3 is finally out. On the course of last 2 years, I was pretty sure a number of times [...]]]></description>
			<content:encoded><![CDATA[<p>I can&#8217;t blog much cuz of my works. I&#8217;m pretty busy in these days. When i check <a href="http://php.net">php.net</a>,  i saw PHP 5.3 released post in 30 Jun -.- nevermind. After a long string of delays, <a href="http://php.net/releases/5_3_0.php">PHP 5.3 is finally out</a>.  On the course of last 2 years, I was pretty sure a number of times that it will happen next month the latest, but there always were good reasons to postpone it. Now finally it’s officially out. I think it’s a huge step for PHP. Download it and try it!</p>
<p>Some major new features in 5.3:<br />
<span id="more-168"></span><br />
   1. <a href="http://php.net/namespaces">Namespaces</a>! They didn’t end up exactly as I thought they would but they are a major feature PHP was missing for a long time, and I’m very curious to see how it works out in big projects.<br />
   2.<a href="http://www.php.net/manual/en/functions.anonymous.php"> Closures and anonymous functions</a>! PHP now has first-class functions, and you can do all kinds of crazy stuff with it. Or just make your code easier to read and maintain <img src='http://ersinacar.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
   3.<a href="http://php.net/gc_enable"> Garbage collection</a>. PHP engine, being refcount-based, always has had a slight problem with reference loops. Even though usually it was not a big issue since at the end of the request everything is cleaned up, for long-running PHP applications not based on short request pattern it became a problem. Not anymore – now the engine knows to clean up such loops.<br />
   4. <a href="http://php.net/lsb">Late static binding</a> – it’s somewhat exotic thing for people that never encountered it, but was very burning issue for people that did need it. Basically, when class Foo extends class Bar, and the method func() defined in Foo is called as Bar::func(), there was no way to distinguish it from Foo::func(). Now there is. This allows to implement all kinds of cool patterns like ActiveRecord.<br />
   5. <a href="http://php.net/intl">Intl extension</a> in core – lots of functions to allow you to internationalize your application.<br />
   6. <a href="http://php.net/phar">Phar</a> in core – now you can pack all the application in one neat file and still be able to run it!</p>
<p>Also in 5.3:</p>
<p>   1. Nowdocs – same as <a href="http://www.php.net/manual/en/language.types.string.php">heredocs</a>, but doesn’t parse variables. Excellent feature for somebody that wants to include bing chunk of text into the script which can happen to have $’s etc. in it.<br />
   2. ?: shortcut. That’s simple – $a?:$b is $a if $a is true, otherwise it’s $b.<br />
   3. goto. Yes, I know. But now we have it too. Deal with it. <img src='http://ersinacar.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
   4. <a href="http://php.net/mysqli.mysqlnd">mysqlnd</a> – native PHP-specific mysql driver.</p>
<p>Last but definitely not least – tons of performance improvements, bug fixes, etc. <a href="http://php.net/downloads.php">Download it</a> today! <img src='http://ersinacar.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
]]></content:encoded>
			<wfw:commentRss>http://ersinacar.com/php-5-3-released-horeyyy_168.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to find the size of a directory and free space in linux</title>
		<link>http://ersinacar.com/how-to-find-the-size-of-a-directory-and-free-space_153.html</link>
		<comments>http://ersinacar.com/how-to-find-the-size-of-a-directory-and-free-space_153.html#comments</comments>
		<pubDate>Wed, 01 Jul 2009 12:35:59 +0000</pubDate>
		<dc:creator>Ersin Acar</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://ersinacar.com/?p=153</guid>
		<description><![CDATA[This article explains 2 simple commands that most people want to know when they are using Linux. You can find the size of a directory and find the amount of free space, which is on your computer. The command you use to specify the directory size is &#8220;du&#8221;. And, to the free space you can [...]]]></description>
			<content:encoded><![CDATA[<p>This article explains 2 simple commands that most people want to know when they are using Linux. You can find the size of a directory and find the amount of free space, which is on your computer. The command you use to specify the directory size is &#8220;du&#8221;. And, to the free space you can &#8220;df.&#8221;</p>
<p>All information in this article can be found in the man pages for du and df. In case you get bored reading the man pages, and you want your work quickly, then this article is for you.<br />
<span id="more-153"></span><br />
<strong>&#8216;du&#8217; &#8211; find the size of a directory</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">du</span></pre></div></div>

<p>Enter the above at the command prompt, you can view a list of directories in the current directory with their sizes. The last line of output gives you the size of the current directory including subdirectories. The size given the size of the files and directories in the current directory and all its subdirectories. Note that by default are the size in kilobytes.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">du</span> <span style="color: #000000; font-weight: bold;">/</span> home <span style="color: #000000; font-weight: bold;">/</span> david</pre></div></div>

<p>The above command would be the directory, size of the directory / home / david</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$du</span>  <span style="color: #660033;">-h</span></pre></div></div>

<p>This command gives you a better outcome than standard. The option &#8220;-h&#8221; stands for the human-readable format. So that the size of the files and directories are this time with a suffix &#8216;K&#8217; if its kilobytes and &#8220;M&#8221; if its Megabytes and &#8216;G&#8217;, if its gigabytes.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$du</span> <span style="color: #660033;">-ah</span></pre></div></div>

<p>This command would be in his performance, not just the directories, but also all the files in the current directory. Note that &#8220;you&#8221; always counts all files and directories, with the final size in the last line. But the &#8220;-a&#8221; shows the file name along with the directory name in the output. &#8216;-h&#8217; is re-readable form.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$du</span> <span style="color: #660033;">-c</span></pre></div></div>

<p>This gives you a large sum in the last line of output. So, if your directory is 30MB the last 2 lines of output would be</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">30M.
30M total</pre></div></div>

<p>In the first line will be the default last line of the &#8220;du&#8221; output, indicating the size of the list and a further display of the same size, followed by the string &#8220;Total&#8221;. This is useful if you run this command with the command grep only the final size of a directory, as shown below.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">du</span> <span style="color: #660033;">-ch</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> total</pre></div></div>

<p>This would have only one line in the output shows the size of the current directory including all subdirectories.</p>
<p>Note: If you are not familiar with the lines (around the above command possible) refer to Article No. 24 Grep is one of the most important commands in Unix. See Rule No. 25 to learn more about grep.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$du</span> <span style="color: #660033;">-s</span></pre></div></div>

<p>This shows a summary of the directory size. It is the easiest way to know the size of the current directory.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$du</span> <span style="color: #660033;">-S</span></pre></div></div>

<p>This would increase the size of the current directory without the size of the subdirectories in that directory. So it basically shows the total size of all files in the current directory.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$du</span> <span style="color: #660033;">--exculde</span> = mp3</pre></div></div>

<p>The above command would be the size of the current directory with all subdirectories, but it would make all files with the given pattern in their filenames. Thus in the above case, if it happens, all MP3 files in the current directory or its subdirectories, their size is not taken into account when calculating the total list size.</p>
<p><strong>&#8216;df&#8217; &#8211; finding the disk free space / disk usage</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$df</span></pre></div></div>

<p>Enter the above is a table consisting of 6 columns. All columns are very easy to understand. Remember that the &#8220;size&#8221;, &#8220;Used&#8221; and &#8220;free&#8221; columns use kilobytes as a unit. The &#8220;Use% column shows the use as a percentage, which is also very useful.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$df</span> <span style="color: #660033;">-h</span></pre></div></div>

<p>Shows the same performance as the previous one, but the command &#8220;-h&#8221; shows human-readable format. Therefore, instead of kilobytes as the unit of output would have been &#8220;M&#8221; for megabytes and &#8220;G&#8221; for gigabytes.</p>
<p>The majority of users are not on the other parameters, which are to &#8220;df&#8221;. So I will not discuss.</p>
<p>I will again show you an example that I am on my computer. I have actually saved this as a script called &#8220;use&#8221; because I think it often.</p>
<p>Example:</p>
<p>I have my Linux installation on / dev/hda1 is mounted and I have my Windows partitions as well (by default every time Linux boots). So &#8216;df&#8217; default shows me the disk usage of my Linux and Windows partitions. And I&#8217;m only in the hard disk using the Linux partitions. That is what I use:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">$df</span>-h <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #000000; font-weight: bold;">/</span> dev<span style="color: #000000; font-weight: bold;">/</span>hda1 <span style="color: #000000; font-weight: bold;">|</span> cut-c <span style="color: #000000;">41</span>-<span style="color: #000000;">43</span></pre></div></div>

<p>This command displays the following on my computer</p>
<p>45%</p>
<p>Basically, this command &#8220;df&#8221; which uses all the disk partitions and then extracts the lines with / dev/hda1, because I am only in this. Then cut the characters from the 41st to 43 Column, since the columns, the use in%, that is what I want.</p>
]]></content:encoded>
			<wfw:commentRss>http://ersinacar.com/how-to-find-the-size-of-a-directory-and-free-space_153.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>how to convert linux timestamp to readable date in PHP</title>
		<link>http://ersinacar.com/how-to-convert-linux-timestamp-to-readable-date-in-php_145.html</link>
		<comments>http://ersinacar.com/how-to-convert-linux-timestamp-to-readable-date-in-php_145.html#comments</comments>
		<pubDate>Tue, 02 Jun 2009 14:36:43 +0000</pubDate>
		<dc:creator>Ersin Acar</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[timestamp]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://ersinacar.com/?p=145</guid>
		<description><![CDATA[Another simple trick; $timestamp=&#34;1243949621&#34;; echo date&#40;&#34;d-m-Y&#34;, $timestamp&#41;; //02-06-2009]]></description>
			<content:encoded><![CDATA[<p>Another simple trick;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$timestamp</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1243949621&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span>  <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;d-m-Y&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//02-06-2009</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://ersinacar.com/how-to-convert-linux-timestamp-to-readable-date-in-php_145.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Copy entrie directories&#8217; content in PHP</title>
		<link>http://ersinacar.com/copy-entrie-directories-content-in-php_142.html</link>
		<comments>http://ersinacar.com/copy-entrie-directories-content-in-php_142.html#comments</comments>
		<pubDate>Tue, 02 Jun 2009 10:06:44 +0000</pubDate>
		<dc:creator>Ersin Acar</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[copy]]></category>
		<category><![CDATA[dir]]></category>
		<category><![CDATA[directory]]></category>
		<category><![CDATA[mkdir]]></category>
		<category><![CDATA[opendir]]></category>
		<category><![CDATA[readdir]]></category>

		<guid isPermaLink="false">http://ersinacar.com/?p=142</guid>
		<description><![CDATA[Here&#8217;s an easy to use function to copy entire directories function dir_copy&#40;$src,$dst&#41; &#123; $dir = opendir&#40;$src&#41;; @mkdir&#40;$dst&#41;; while&#40;false !== &#40; $file = readdir&#40;$dir&#41;&#41; &#41; &#123; if &#40;&#40; $file != '.' &#41; &#38;&#38; &#40; $file != '..' &#41;&#41; &#123; if &#40; is_dir&#40;$src . '/' . $file&#41; &#41; &#123; dir_copy&#40;$src . '/' . $file,$dst . '/' . [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s an easy to use function to copy entire directories</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> dir_copy<span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #339933;">,</span><span style="color: #000088;">$dst</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$dir</span> <span style="color: #339933;">=</span> <span style="color: #990000;">opendir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #339933;">@</span><span style="color: #990000;">mkdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dst</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">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: #990000;">readdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</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: #b1b100;">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;">&amp;&amp;</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: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</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;">&#123;</span>
                dir_copy<span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$file</span><span style="color: #339933;">,</span><span style="color: #000088;">$dst</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</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: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #990000;">copy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$src</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$file</span><span style="color: #339933;">,</span><span style="color: #000088;">$dst</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</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: #009900;">&#125;</span>
    <span style="color: #990000;">closedir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dir</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://ersinacar.com/copy-entrie-directories-content-in-php_142.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Remove last char of string in PHP</title>
		<link>http://ersinacar.com/remove-last-char-of-string-in-php_138.html</link>
		<comments>http://ersinacar.com/remove-last-char-of-string-in-php_138.html#comments</comments>
		<pubDate>Sat, 30 May 2009 17:18:29 +0000</pubDate>
		<dc:creator>Ersin Acar</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[remove]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[substr]]></category>

		<guid isPermaLink="false">http://ersinacar.com/?p=138</guid>
		<description><![CDATA[Simple and clear; substr_replace&#40;$string ,&#34;&#34;,-1&#41;;]]></description>
			<content:encoded><![CDATA[<p>Simple and clear;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">substr_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span> <span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://ersinacar.com/remove-last-char-of-string-in-php_138.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to print all defined variables in PHP</title>
		<link>http://ersinacar.com/how-to-print-all-defined-variables-in-php_132.html</link>
		<comments>http://ersinacar.com/how-to-print-all-defined-variables-in-php_132.html#comments</comments>
		<pubDate>Mon, 25 May 2009 08:58:38 +0000</pubDate>
		<dc:creator>Ersin Acar</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[defined]]></category>
		<category><![CDATA[get_defined_vars]]></category>

		<guid isPermaLink="false">http://ersinacar.com/?p=132</guid>
		<description><![CDATA[we&#8217;ll go it with get_defined_vars() function. get_defined_vars returns multidimensional array with all defiend variables in PHP here we go ; $array = get_defined_vars&#40;&#41;; &#160; print_r&#40;$array&#41;; if you want to find out only SERVER vars you can try this; print_r&#40;$array&#91;&#34;_SERVER&#34;&#93;&#41;; if you want to see vars in regular echo &#34;&#38;lt;pre&#38;gt;&#34;; print_r&#40;$array&#41;; echo &#34;&#38;lt;/pre&#38;gt;&#34;;]]></description>
			<content:encoded><![CDATA[<p>we&#8217;ll go it with <strong>get_defined_vars()</strong> function.  get_defined_vars returns multidimensional array with all defiend variables in PHP<br />
here we go ; <span id="more-132"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">get_defined_vars</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>if you want to find out only SERVER vars you can try this;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;_SERVER&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>if you want to see vars in regular</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&amp;lt;pre&amp;gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&amp;lt;/pre&amp;gt;&quot;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://ersinacar.com/how-to-print-all-defined-variables-in-php_132.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mysql Dump and Restore commands</title>
		<link>http://ersinacar.com/mysql-dump-and-restore-commands_125.html</link>
		<comments>http://ersinacar.com/mysql-dump-and-restore-commands_125.html#comments</comments>
		<pubDate>Thu, 21 May 2009 17:50:55 +0000</pubDate>
		<dc:creator>Ersin Acar</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[dump]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[restore]]></category>

		<guid isPermaLink="false">http://ersinacar.com/?p=125</guid>
		<description><![CDATA[if you want to backup and restore your big MySQL databases you need better solution than phpmyadmin So here we go; To dump all databases to a file: $ mysqldump -u dbuser -pdbpass --all-databases &#62; all-databases.sql To dump a single database to a file: $ mysqldump -u dbuser -pdbpass somedb &#62; somedb.sql Dump InnoDB in [...]]]></description>
			<content:encoded><![CDATA[<p>if you want to backup and restore your big MySQL databases you need better solution than phpmyadmin</p>
<p>So here we go;</p>
<p>To dump  all databases to a file:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ mysqldump <span style="color: #660033;">-u</span> dbuser <span style="color: #660033;">-pdbpass</span> <span style="color: #660033;">--all-databases</span> <span style="color: #000000; font-weight: bold;">&gt;</span> all-databases.sql</pre></div></div>

<p>To dump  a single database to a file:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ mysqldump <span style="color: #660033;">-u</span> dbuser <span style="color: #660033;">-pdbpass</span> somedb <span style="color: #000000; font-weight: bold;">&gt;</span> somedb.sql</pre></div></div>

<p>Dump InnoDB in a single transaction:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ mysqldump <span style="color: #660033;">-u</span> dbuser <span style="color: #660033;">-pdbpass</span> <span style="color: #660033;">--single-transaction</span> somedb <span style="color: #000000; font-weight: bold;">&gt;</span> somedb.sql</pre></div></div>

<p>To restore:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ mysql <span style="color: #660033;">-u</span> dbuser <span style="color: #660033;">-pdbpass</span> somedb <span style="color: #000000; font-weight: bold;">&lt;</span> somedb.sql</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://ersinacar.com/mysql-dump-and-restore-commands_125.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to get file extensions in PHP</title>
		<link>http://ersinacar.com/how-to-get-file-extensions-in-php_123.html</link>
		<comments>http://ersinacar.com/how-to-get-file-extensions-in-php_123.html#comments</comments>
		<pubDate>Thu, 21 May 2009 09:22:22 +0000</pubDate>
		<dc:creator>Ersin Acar</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[count]]></category>
		<category><![CDATA[explode]]></category>
		<category><![CDATA[extension]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[preg]]></category>
		<category><![CDATA[preg_replace]]></category>
		<category><![CDATA[split]]></category>
		<category><![CDATA[strpos]]></category>
		<category><![CDATA[strrchr]]></category>

		<guid isPermaLink="false">http://ersinacar.com/?p=123</guid>
		<description><![CDATA[There are several methods get a file extension using PHP: &#60;? $filename = 'myfile.jpg'; &#160; // 1. The &#34;explode/end&#34; method $ext = end&#40;explode&#40;'.', $filename&#41;&#41;; &#160; // 2. The &#34;strrchr&#34; method $ext = substr&#40;strrchr&#40;$filename, '.'&#41;, 1&#41;; &#160; // 3. The &#34;strrpos&#34; method $ext = substr&#40;$filename, strrpos&#40;$filename, '.'&#41; + 1&#41;; &#160; // 4. The &#34;preg_replace&#34; method $ext [...]]]></description>
			<content:encoded><![CDATA[<p>There are several methods get a file extension using PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'myfile.jpg'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 1. The &quot;explode/end&quot; method</span>
<span style="color: #000088;">$ext</span> <span style="color: #339933;">=</span> <span style="color: #990000;">end</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span><span style="color: #339933;">,</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;
<span style="color: #666666; font-style: italic;">// 2. The &quot;strrchr&quot; method</span>
<span style="color: #000088;">$ext</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strrchr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</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: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 3. The &quot;strrpos&quot; method</span>
<span style="color: #000088;">$ext</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #990000;">strrpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</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: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 4. The &quot;preg_replace&quot; method</span>
<span style="color: #000088;">$ext</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^.*\.([^.]+)$/D'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'$1'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 5. The &quot;never use this&quot; method</span>
<span style="color: #666666; font-style: italic;">//   From: http://php.about.com/od/finishedphp1/qt/file_ext_PHP.htm</span>
<span style="color: #000088;">$exts</span> <span style="color: #339933;">=</span> <span style="color: #990000;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;[/<span style="color: #000099; font-weight: bold;">\\</span>.]&quot;</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: #000088;">$n</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$exts</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: #000088;">$ext</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$exts</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$n</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://ersinacar.com/how-to-get-file-extensions-in-php_123.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Determine whether an IP is within a certain range</title>
		<link>http://ersinacar.com/determine-whether-an-ip-is-within-a-certain-range_112.html</link>
		<comments>http://ersinacar.com/determine-whether-an-ip-is-within-a-certain-range_112.html#comments</comments>
		<pubDate>Sat, 16 May 2009 08:31:43 +0000</pubDate>
		<dc:creator>Ersin Acar</dc:creator>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[ip]]></category>
		<category><![CDATA[ip range]]></category>
		<category><![CDATA[range]]></category>

		<guid isPermaLink="false">http://ersinacar.com/?p=112</guid>
		<description><![CDATA[I spend a lot of time lurking in the channel # PHP (EFnet and freenode, please &#8211; no flame wars) and this issue is a frequently asked that the rule is a simple answer in the form of the use of strpos (), or best to ip2long () in a more than less than reply. [...]]]></description>
			<content:encoded><![CDATA[<p>
I spend a lot of time lurking in the channel # PHP (EFnet and freenode, please &#8211; no flame wars) and this issue is a frequently asked that the rule is a simple answer in the form of the use of strpos (), or best to ip2long () in a more than less than reply.</p>
<p>Unfortunately, although the people in the rule that an IP address is just an unsigned 32-bit integer, and is easy to determine, usually with $ _SERVER [ "REMOTE_ADDR"], where the real challenge &#8211; is in defining the range within which it must decide whether the IP address. IP ranges are usually divided into three categories (in increasing complexity):</p>
<p>         1. Wild Card: 192.168.10 .*<br />
         2. Start-end: 10.1.0.0-10.1.255.255<br />
         3. * CIDR: 172.16.1.0/24</p>
<p>      * Classless Inter-Domain Routing<br />
<span id="more-112"></span><br />
The wild card method, or &#8216;class&#8217;, lets you in the class A (10 .*.*.*), Class B (172.16 .*.*) or Class C (192.168.10 .*) levels granularity of the things we like in the old days (before the web decided to make the popular internet). But still, this is just not granular enough for practice.</p>
<p>Thus was born CIDR (yes, I&#8217;m talking about skipping the end of last start now). CIDR was on the concept that we really do not need to create networks to 8, 16, 24-bit boundaries, and we could be more granular by using an arbitrary number (from 2-30) to a series of networks. Details about why you can not use &#8220;31&#8243; beyond the scope of this article.</p>
<p>CIDR renaming of the former Class A, B and C networks / 8, / 16 and / 24, and reflects the left-most significant bits of the 32-bit IP address. Thus was born the possibility of very specific IP ranges in the form abcd / xx. However, part of the problem with this is that, although it succinctly describes the network beginning and end, the most normal mortal people could not decipher. CIDR addressing may also be in the form of a longer netmask, eg abcd/255.255.255.224</p>
<p>The simplified form of start-IP &#8211; IP in the end was just the place for mortals and is typically used by persons without networking background. There is also strong in the consumer broadband router, and especially in Microsoft Windows DHCP server.</p>
<p>To have declared, as a number, and hence that a network mask is, how can we use this knowledge to help us in determining whether an IP is in an area?</p>
<p>What this article will attempt to do if the design of algorithms for the verification of IP addresses easier.</p>
<p>Logically, Method 1 (the wildcard), the method can be easily 2 (start-end) through the use of setting start and end of the wildcard string and replaces the character &#8220;*&#8221; and 0 for the start and 255 for the end, for example, &#8220;192.168.10 .*&#8221; will &#8220;192.168.10.0-192.168.10.255&#8243; which (I hope) will be for everyone apparently.</p>
<p>We can then assess whether Method1 and Method2 in the same way. In this, we simply use the PHP function in ip2long () for all 3 values and a mathematical test start <= IP <= end.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">list</span> <span style="color: #009900;">&#40;</span>$ low<span style="color: #339933;">,</span> $ II<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'-'</span><span style="color: #339933;">,</span> $<span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $ Lower_dec <span style="color: #339933;">=</span> <span style="color: #990000;">ip2long</span> <span style="color: #009900;">&#40;</span>$ lower<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $ Upper_dec <span style="color: #339933;">=</span> <span style="color: #990000;">ip2long</span> <span style="color: #009900;">&#40;</span>$ top<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $ Ip_dec <span style="color: #339933;">=</span> <span style="color: #990000;">ip2long</span> <span style="color: #009900;">&#40;</span>$ ip<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>$ ip_dec<span style="color: #339933;">&gt;</span> <span style="color: #339933;">=</span> $ lower_dec<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> <span style="color: #339933;">&amp;</span> <span style="color: #009900;">&#40;</span>$ ip_dec <span style="color: #339933;">&lt;=</span> $ upper_dec<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We have, however, a complicating factor here &#8211; PHP does not do unsigned integer (32 bit) &#8211; which would be necessary for this math to work. We can do this by switching to negate floating point datatypes. PHP stores floating species as 64-bit and thus will have no problem with the IPv4 address space (Note &#8211; This is obviously not enough for granular 128bit IPv6 addressing). It is the easiest way to solve the start <= IP <= end of problem with IP-and floating numers is the following piece of code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">$ Lower_dec <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span> <span style="color: #990000;">sprintf</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">% u</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">ip2long</span> <span style="color: #009900;">&#40;</span>$ lower<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $ Upper_dec <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span> <span style="color: #990000;">sprintf</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">% u</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">ip2long</span> <span style="color: #009900;">&#40;</span>$ top<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $ Ip_dec <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>float<span style="color: #009900;">&#41;</span> <span style="color: #990000;">sprintf</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">% u</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">ip2long</span> <span style="color: #009900;">&#40;</span>$ ip<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>$ ip_dec<span style="color: #339933;">&gt;</span> <span style="color: #339933;">=</span> $ lower_dec<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span> <span style="color: #339933;">&amp;</span> <span style="color: #009900;">&#40;</span>$ ip_dec <span style="color: #339933;">&lt;=</span> $ upper_dec<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Next we have the challenge of handing the CIDR netmasks. What we can do is to create a format CIDR IP address / netmask, and calculate the start and end IP addresses of this block and proceed as before &#8211; but that would not take a joke &#8211; and would mean that I do not really taught by this article.</p>
<p>The method we use here is how the whole world of Internet routers, if a destination IP is in a given CIDR address space. And we receive and dirty with bitmask and bitwise logical operators.</p>
<p>This is a real world example, my web server IP 80.76.201.37 and the netblock in which it resides, is 80.76.201.32/27, how does this all mean?</p>
<p>Well, the / 27 indicates that the first 27 bits of the IP address are the same network and IP address in this network (area) have the same only 27 bits are identical. Bits 28-32 are variable and may be 5 bits of the variation. If you know your binary, so this means, 32 IP addresses. (However, with routing, you can not use the top and bottom of a range of IP addresses, because this special and the network and broadcast addresses. [This is also the reason why a / 31 is not much (except for PPP connections) you can not use the 2-addresses, which gives you room]).</p>
<p>So thinking logically, bitwise, if my IP address and CIDR specification, then all I have to do is verify that the first 27 bits are all involved, and I&#8217;m good. Right. How would we do this in PHP? Sounds simple, can only use PHP&#8217;s bitwise logical AND operator: &#038;<br />
Also correct.</p>
<p>To achieve this, we need to convert 27 in 27, which really means &#8211; a 32-bit number of 27 ones and zeros in binary-5 (which is what really looks like 255,255,255,224).</p>
<p>In pseudo-code that you then do if (IP &#038; bitmask) == (RANGE &#038; bitmask), then everything is good and you know that the IP is in the area.</p>
<p>Visualization of this with our IP address (using the Unix tool very handy ipcalc):</p>
<p>      Address: 80.76.201.37 01010000.01001100.11001001.00100101<br />
      Netmask: 255,255,255,224 11111111.11111111.11111111.11100000<br />
      Wildcard: 0.0.0.31 00000000.00000000.00000000.00011111</p>
<p>      Network: 80.76.201.32/27 01010000.01001100.11001001.00100000<br />
      HostMin: 80.76.201.33 01010000.01001100.11001001.00100001<br />
      HostMax: 80.76.201.62 01010000.01001100.11001001.00111110<br />
      Broadcast: 80.76.201.63 01010000.01001100.11001001.00111111<br />
      Hosts / Net: 30</p>
<p>You can do this in the wild card line-0.0.0.31, and the network-ored with wildcard results in the broadcast address: 80.76.201.63.</p>
<p>Knowing this, then the IP address ANDed with the network address is the same value as the margin ANDed with the network address and thus can be used as a comparison for an IP Broadcast resident in this area.</p>
<p>How can we network address in PHP, we have two strategies, one is in such a simple substr () and take the left bits of the number pad and then just to the right with 0s. Or we can do some math with &#8220;NOT from 2 to the power of the (32-area) &#8211; 1&#8243;. In order for our value / 27 This gives us the decimal value 31, NOTED results (65536-31) (representation in bit-form &#8211; PHP, it is considered a negative whole number, but we do not need to worry about that).</p>
<p>I am sure that from now on, you scream for some code (and if you are stuck in this long, you really deserve it).</p>
<p>Code to manipulate a number / netmask in a broadcast address, with mathematics, provided:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      $ Ip <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;80.76.201.37&quot;</span><span style="color: #339933;">;</span>
      $ Row <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;80.76.201.32&quot;</span><span style="color: #339933;">;</span>
      $ Netmask <span style="color: #339933;">=</span> <span style="color: #cc66cc;">27</span><span style="color: #339933;">;</span></pre></div></div>

<p>We can change the IP addresses too long integers with ip2long (with variable_dec &#8211; December is the abbreviation for decimal):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      $ Range_dec <span style="color: #339933;">=</span> <span style="color: #990000;">ip2long</span> <span style="color: #009900;">&#40;</span>$ <span style="color: #990000;">range</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      $ Ip_dec <span style="color: #339933;">=</span> <span style="color: #990000;">ip2long</span> <span style="color: #009900;">&#40;</span>$ ip<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This gives us the basis of our mathematics, we need now only to the broadcast address.</p>
<p>Strategy 1 with str_pad to a string by padding with 0s and 1s.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      $ Netmask_dec <span style="color: #339933;">=</span> <span style="color: #990000;">bindec</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">str_pad</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> $ netmask<span style="color: #339933;">,</span><span style="color: #0000ff;">'1 '</span><span style="color: #009900;">&#41;</span>
       <span style="color: #339933;">.</span> <span style="color: #990000;">str_pad</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">32</span> <span style="color: #339933;">-</span> $ netmask<span style="color: #339933;">,</span><span style="color: #0000ff;">'0 '</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We can achieve the same result if the mathematics of NOTtin wildcard value. That is our strategy 2:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      $ Wildcard_dec <span style="color: #339933;">=</span> <span style="color: #990000;">pow</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">32</span> <span style="color: #339933;">-</span> $ netmask<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
      $ Netmask_dec <span style="color: #339933;">=</span> ~ $ wildcard_dec<span style="color: #339933;">;</span></pre></div></div>

<p>Once we know that the netmask address (decimal), as we have here, we know that if Anding this with the original IP results to check against the margin ANDed with the netmask, the IP is in the area, defined by the range / mask.</p>
<p>This can be done simply with:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>$ ip_dec <span style="color: #339933;">&amp;</span> $ netmask_dec<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #009900;">&#40;</span>$ range_dec <span style="color: #339933;">&amp;</span> $ netmask_dec<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I moved all this logic in an easy to make the file to a single function ip_in_range ($ ip, $ range) in which $ ip is the IP address you want to validate and $ field is a one of the above formats , Wild Card, start-end solution or CIDR. The function is a simple TRUE or FALSE, if the IP is in this area.</p>
]]></content:encoded>
			<wfw:commentRss>http://ersinacar.com/determine-whether-an-ip-is-within-a-certain-range_112.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick sort for associative arrays</title>
		<link>http://ersinacar.com/quick-sort-for-associative-arrays_109.html</link>
		<comments>http://ersinacar.com/quick-sort-for-associative-arrays_109.html#comments</comments>
		<pubDate>Sat, 16 May 2009 08:19:49 +0000</pubDate>
		<dc:creator>Ersin Acar</dc:creator>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Off Topic]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[associative]]></category>
		<category><![CDATA[quick sort]]></category>
		<category><![CDATA[sorting]]></category>

		<guid isPermaLink="false">http://ersinacar.com/?p=109</guid>
		<description><![CDATA[Here is the way of quick sort in associative arrays; &#60;? &#160; function qsort&#40;$a,$f&#41; &#123; qsort_do&#40;&#38;$a,0,Count&#40;$a&#41;-1,$f&#41;; &#125; &#160; function qsort_do&#40;$a,$l,$r,$f&#41; &#123; if &#40;$l &#60; $r&#41; &#123; qsort_partition&#40;&#38;$a,$l,$r,&#38;$lp,&#38;$rp,$f&#41;; qsort_do&#40;&#38;$a,$l,$lp,$f&#41;; qsort_do&#40;&#38;$a,$rp,$r,$f&#41;; &#125; &#125; &#160; function qsort_partition&#40;$a,$l,$r,$lp,$rp,$f&#41; &#123; $i = $l+1; $j = $l+1; &#160; while &#40;$j &#60;= $r&#41; &#123; if &#40;$f&#40;$a&#91;$j&#93;,$a&#91;$l&#93;&#41;&#41; &#123; $tmp = $a&#91;$j&#93;; $a&#91;$j&#93; = [...]]]></description>
			<content:encoded><![CDATA[<p>Here is the way of quick sort in associative arrays;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> qsort<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #000088;">$f</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
       qsort_do<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #990000;">Count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</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: #000088;">$f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> qsort_do<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #000088;">$l</span><span style="color: #339933;">,</span><span style="color: #000088;">$r</span><span style="color: #339933;">,</span><span style="color: #000088;">$f</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
       <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$l</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
               qsort_partition<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #000088;">$l</span><span style="color: #339933;">,</span><span style="color: #000088;">$r</span><span style="color: #339933;">,&amp;</span><span style="color: #000088;">$lp</span><span style="color: #339933;">,&amp;</span><span style="color: #000088;">$rp</span><span style="color: #339933;">,</span><span style="color: #000088;">$f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
               qsort_do<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #000088;">$l</span><span style="color: #339933;">,</span><span style="color: #000088;">$lp</span><span style="color: #339933;">,</span><span style="color: #000088;">$f</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
               qsort_do<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #000088;">$rp</span><span style="color: #339933;">,</span><span style="color: #000088;">$r</span><span style="color: #339933;">,</span><span style="color: #000088;">$f</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;">function</span> qsort_partition<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span><span style="color: #000088;">$l</span><span style="color: #339933;">,</span><span style="color: #000088;">$r</span><span style="color: #339933;">,</span><span style="color: #000088;">$lp</span><span style="color: #339933;">,</span><span style="color: #000088;">$rp</span><span style="color: #339933;">,</span><span style="color: #000088;">$f</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
       <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$l</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> 
       <span style="color: #000088;">$j</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$l</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> 
&nbsp;
       <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$j</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
               <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$f</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$j</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$l</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
                       <span style="color: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$j</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
                       <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$j</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
                       <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tmp</span><span style="color: #339933;">;</span> 
                       <span style="color: #000088;">$i</span><span style="color: #339933;">++;</span> 
               <span style="color: #009900;">&#125;</span> 
               <span style="color: #000088;">$j</span><span style="color: #339933;">++;</span> 
       <span style="color: #009900;">&#125;</span> 
&nbsp;
       <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$l</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
       <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$l</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> 
       <span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span> 
&nbsp;
       <span style="color: #000088;">$lp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> 
       <span style="color: #000088;">$rp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://ersinacar.com/quick-sort-for-associative-arrays_109.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

