<?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>Martin's Blog</title>
	<atom:link href="http://www.martienus.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.martienus.com</link>
	<description>Logic to Art</description>
	<lastBuildDate>Fri, 12 Feb 2010 19:07:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Check size constraints for multidimensional arrays</title>
		<link>http://www.martienus.com/code/check-size-constraints-for-multidimensional-arrays.html</link>
		<comments>http://www.martienus.com/code/check-size-constraints-for-multidimensional-arrays.html#comments</comments>
		<pubDate>Mon, 08 Feb 2010 16:17:59 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[constraints]]></category>
		<category><![CDATA[multidimensional]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[recursion]]></category>

		<guid isPermaLink="false">http://www.martienus.com/?p=57</guid>
		<description><![CDATA[Multidimensional arrays are commonly used as a complex data structure. Moreover, they are used to convey data between software systems internally. In some programming languages however, there is no way to constrain the size of arrays at any stage of the array&#8217;s lifetime. PHP for instance has no ability to do this. Therefore a way [...]]]></description>
			<content:encoded><![CDATA[<p>Multidimensional arrays are commonly used as a complex data structure. Moreover, they are used to convey data between software systems internally. In some programming languages however, there is no way to constrain the size of arrays at any stage of the array&#8217;s lifetime. PHP for instance has no ability to do this. Therefore a way must be devised to make sure the structure of the passed data conforms to what the receiving system expects.</p>
<p>Think for instance pixel <a href="http://docs.gimp.org/nl/plug-in-convmatrix.html">convolution matrices</a>. These are usually 3 by 3.</p>
<p>Check out my recursive PHP function to check sizes at any dimensional depth possible:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> isDimension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dimensions</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$amount</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dimensions</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$amount</span> <span style="color: #339933;">===</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">||</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$amount</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: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dimensions</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
         <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$element</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_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$element</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: #339933;">!</span>isDimension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$element</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dimensions</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
               <span style="color: #009900;">&#123;</span>
                  <span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
               <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">else</span>
            <span style="color: #009900;">&#123;</span>
               <span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</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: #009900;">&#125;</span>
   <span style="color: #b1b100;">else</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #000088;">$pass</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And when we utilize this function in the following way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$subject</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</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: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>isDimension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</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;">print</span> <span style="color: #0000ff;">&quot;Yay&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Nay&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The output will be:</p>
<pre>Yay
</pre>
<p>If at a certain dimension size does not matter you can skip it by entering -1 as the expected size:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$subject</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>	<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
			<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</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: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span>
			<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$wellIsIt</span> <span style="color: #339933;">=</span> isDimension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Returns true</span>
<span style="color: #000088;">$wellIsIt</span> <span style="color: #339933;">=</span> isDimension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Returns true</span>
<span style="color: #000088;">$wellIsIt</span> <span style="color: #339933;">=</span> isDimension<span style="color: #009900;">&#40;</span><span style="color: #000088;">$subject</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Returns false</span></pre></td></tr></table></div>

<p>This is a recursive function, meaning that it calls itself from within itself until a certain condition is met. Then it cascades the result back to the initial function call which in turn passes it back to whatever script called the function. More about this can be read <a href="http://en.wikipedia.org/wiki/Recursion_%28computer_science%29">on wikipedia</a>.</p>
<p>There ya go</p>
]]></content:encoded>
			<wfw:commentRss>http://www.martienus.com/code/check-size-constraints-for-multidimensional-arrays.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Javascript: Remove values from array prototype</title>
		<link>http://www.martienus.com/code/javascript-remove-values-from-array-prototype.html</link>
		<comments>http://www.martienus.com/code/javascript-remove-values-from-array-prototype.html#comments</comments>
		<pubDate>Wed, 29 Oct 2008 18:33:11 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[remove]]></category>

		<guid isPermaLink="false">http://www.martienus.com/?p=33</guid>
		<description><![CDATA[In order to remove certain values or objects from an array many people iterate through it and remove unwanted occurences with the splice() method. It does what you want it to do but it&#8217;s pretty cumbersome. There are far easier and simpler ways to remove a certain value from an array.
If we have an array [...]]]></description>
			<content:encoded><![CDATA[<p>In order to remove certain values or objects from an array many people iterate through it and remove unwanted occurences with the splice() method. It does what you want it to do but it&#8217;s pretty cumbersome. There are far easier and simpler ways to remove a certain value from an array.</p>
<p>If we have an array like so: arr = [1,2,2,3,4,5,2,6] and we&#8217;d do this: arr.remove(2) we&#8217;ll end up with [1,3,4,5,6]. Nobody likes two&#8217;s anyway.</p>
<p>Here is how I do it:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">remove</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>subject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> r <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> n <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span>subject<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			r<span style="color: #009900;">&#91;</span>r.<span style="color: #660066;">length</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span>i<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: #000066; font-weight: bold;">return</span> r<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Why is this better?</p>
<ul>
<li>No copy of the original is needed to do the math</li>
<li>It is yet again inherently independent of other array prototypes or methods</li>
<li>It runs alot faster <img src='http://www.martienus.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </li>
</ul>
<p>So there you go, another mystery solved.<br />
Take care!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.martienus.com/code/javascript-remove-values-from-array-prototype.html/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>PHP: Image Resizer Class</title>
		<link>http://www.martienus.com/code/php-image-resizer-class.html</link>
		<comments>http://www.martienus.com/code/php-image-resizer-class.html#comments</comments>
		<pubDate>Thu, 19 Jun 2008 16:43:46 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[resize]]></category>

		<guid isPermaLink="false">http://www.martienus.com/?p=15</guid>
		<description><![CDATA[Being able to resize images automatically is a must for websites where users can upload their own images, avatars and so on. In PHP we can do this with the GD library. We don&#8217;t want users to upload pictures that are 2000 pixels wide and 1 pixel high. This will disrupt the layout of the [...]]]></description>
			<content:encoded><![CDATA[<p>Being able to resize images automatically is a must for websites where users can upload their own images, avatars and so on. In PHP we can do this with the <a href="http://www.php.net/manual/en/book.image.php" target="_blank">GD library</a>. We don&#8217;t want users to upload pictures that are 2000 pixels wide and 1 pixel high. This will disrupt the layout of the site on which they are shown.</p>
<p>Thus we want the image to be resized to sane proportions and dimensions so that we can be sure they fit nicely into our websites&#8217; layout.</p>
<p>Therefore I made an image resizing class myself. It actually resamples the images for better results. Here are some of the features:</p>
<ul>
<li>Handles JPG, GIF, PNG and BMP image files</li>
<li>Can make multiple resized copies</li>
<li>Can simply duplicate the original</li>
<li>Can crop images to a certain aspect ratio, maintaining proportions</li>
<li>Can save to JPG, GIF, PNG and BMP</li>
<li>Can return the image as a string (for i.e. database storage)</li>
<li>Can return the image to the browser</li>
<li>Throws exceptions for unexpected behaviour</li>
</ul>
<p>Here is some code that shows how to utilize the class in your PHP application:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include</span> <span style="color: #0000ff;">&quot;imageresizer.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Increase the allowed memory size for the bigger images</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mem_size'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">32000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
try <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$image</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> imageResizer<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'original.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Make a smaller version of the original</span>
	<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resize</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">400</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">300</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">400</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'result.gif'</span><span style="color: #339933;">,</span> GIF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Make a thumbnail of the original</span>
	<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resize</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'thumb_result.png'</span><span style="color: #339933;">,</span> PNG<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Retrieve the thumbnail as a string as BMP and show it in the browser as JPG</span>
	<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getString</span><span style="color: #009900;">&#40;</span>BMP<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$image</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">show</span><span style="color: #009900;">&#40;</span>JPG<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
catch<span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Catch and display any exceptional behaviour</span>
	<span style="color: #b1b100;">print</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Destroy object (executes the destructor) and more importantly, frees up memory</span>
<span style="color: #000088;">$image</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The resize method can have four values passed to it, namely: maximum width, maximum height, minimum width and minimum height. Either the width or the height should be the same when you use this functionality. Suppose we have the width at 400 pixels. The height of the result should be between 300 and 100 pixels. If we can&#8217;t fit the original in this aspect ratio constraint it will crop off the edges until it fits. This way the image is not stretched or compressed and we have control over both proportions and dimensions.</p>
<p><a href="http://www.martienus.com/wp-content/uploads/imageresizer.txt" target="_blank">Download the imageResizer class here!</a></p>
<p>If you come up with any improvements or bugs please let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.martienus.com/code/php-image-resizer-class.html/feed</wfw:commentRss>
		<slash:comments>43</slash:comments>
		</item>
		<item>
		<title>T-SQL: Breadth-First shortest-route search</title>
		<link>http://www.martienus.com/code/t-sql-breadth-first-shortest-route-search-stored-procedure.html</link>
		<comments>http://www.martienus.com/code/t-sql-breadth-first-shortest-route-search-stored-procedure.html#comments</comments>
		<pubDate>Wed, 18 Jun 2008 19:14:00 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[Breadth-First]]></category>
		<category><![CDATA[shortest route]]></category>
		<category><![CDATA[Stored Procedure]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://www.martienus.com/?p=14</guid>
		<description><![CDATA[A while ago I came across the problem of determining the shortest route in a many-to-many self-join table. The linker table consists of two ID columns to link nodes together and is called `vertices`. This represents an unweighted and undirected node graph.
In an unweighted undirected graph there can be no heuristic searching for there is [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago I came across the problem of determining the shortest route in a many-to-many self-join table. The linker table consists of two ID columns to link nodes together and is called `vertices`. This represents an unweighted and undirected node graph.</p>
<p>In an unweighted undirected graph there can be no heuristic searching for there is no clue (weight or direction) which possible leaf node gets you to the target quickest. Therefore an uninformed search is the only option and for this I utilized the <a href="http://en.wikipedia.org/wiki/Breadth-first_search" target="_blank">breadth-first</a> search algorithm.</p>
<p>The breadth-first algorithm visits the leafnodes per generation first and not by branch first. In practice this is the optimal method most of the time.</p>
<p>T-SQL Implementation:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
</pre></td><td class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> BFS<span style="color: #808080;">&#40;</span>
	@<span style="color: #0000FF;">FROM</span> <span style="color: #0000FF;">INT</span>,
	@<span style="color: #0000FF;">TO</span> <span style="color: #0000FF;">INT</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">AS</span>
&nbsp;
<span style="color: #0000FF;">BEGIN</span>
	<span style="color: #0000FF;">DECLARE</span> @Nodes <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#40;</span>Generation <span style="color: #0000FF;">INT</span>, p <span style="color: #0000FF;">INT</span>, r <span style="color: #0000FF;">INT</span>, <span style="color: #0000FF;">UNIQUE</span><span style="color: #808080;">&#40;</span>p, r<span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">DECLARE</span> @Generation <span style="color: #0000FF;">INT</span>
&nbsp;
	<span style="color: #0000FF;">SELECT</span> @Generation <span style="color: #808080;">=</span> <span style="color: #000;">0</span>
&nbsp;
	<span style="color: #0000FF;">INSERT</span> @Nodes
		<span style="color: #808080;">&#40;</span>
			Generation,
			p
		<span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">SELECT</span>	@Generation,
			@<span style="color: #0000FF;">FROM</span>
&nbsp;
	<span style="color: #0000FF;">WHILE</span> <span style="color: #FF00FF;">@@ROWCOUNT</span> <span style="color: #808080;">&gt;</span> <span style="color: #000;">0</span> <span style="color: #808080;">AND</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">EXISTS</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> @Nodes <span style="color: #0000FF;">WHERE</span> p <span style="color: #808080;">=</span> @<span style="color: #0000FF;">TO</span><span style="color: #808080;">&#41;</span>
		<span style="color: #0000FF;">BEGIN</span>
			<span style="color: #0000FF;">SELECT</span> @Generation <span style="color: #808080;">=</span> @Generation <span style="color: #808080;">+</span> <span style="color: #000;">1</span>
&nbsp;
			<span style="color: #0000FF;">INSERT</span>	@Nodes
				<span style="color: #808080;">&#40;</span>
					Generation,
					p,
					r
				<span style="color: #808080;">&#41;</span>
&nbsp;
			<span style="color: #0000FF;">SELECT</span>	@Generation,
					bid,
					aid
			<span style="color: #0000FF;">FROM</span>	vertices
			<span style="color: #0000FF;">WHERE</span>	aid <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> p <span style="color: #0000FF;">FROM</span> @Nodes <span style="color: #0000FF;">WHERE</span> Generation <span style="color: #808080;">=</span> @Generation <span style="color: #808080;">-</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>
				<span style="color: #808080;">AND</span> bid <span style="color: #808080;">NOT</span> <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> p <span style="color: #0000FF;">FROM</span> @Nodes<span style="color: #808080;">&#41;</span>
			<span style="color: #0000FF;">UNION</span>
			<span style="color: #0000FF;">SELECT</span>	@Generation,
				aid,
				bid
			<span style="color: #0000FF;">FROM</span>	vertices
			<span style="color: #0000FF;">WHERE</span>	bid <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> p <span style="color: #0000FF;">FROM</span> @Nodes <span style="color: #0000FF;">WHERE</span> Generation <span style="color: #808080;">=</span> @Generation <span style="color: #808080;">-</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>
				<span style="color: #808080;">AND</span> aid <span style="color: #808080;">NOT</span> <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> p <span style="color: #0000FF;">FROM</span> @Nodes<span style="color: #808080;">&#41;</span>
&nbsp;
		<span style="color: #0000FF;">END</span>
&nbsp;
	<span style="color: #008080;">-- Backtracing method: Traces the route back from target to start</span>
&nbsp;
	<span style="color: #0000FF;">DECLARE</span> @Backtrace <span style="color: #0000FF;">TABLE</span>
	<span style="color: #808080;">&#40;</span> p <span style="color: #0000FF;">INT</span> <span style="color: #808080;">&#41;</span>
&nbsp;
	<span style="color: #0000FF;">INSERT</span> @Backtrace <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">TO</span><span style="color: #808080;">&#41;</span>
&nbsp;
	<span style="color: #0000FF;">WHILE</span> @Generation <span style="color: #808080;">&gt;</span> <span style="color: #000;">0</span>
		<span style="color: #0000FF;">BEGIN</span>
			<span style="color: #0000FF;">DELETE</span> <span style="color: #0000FF;">FROM</span> @Nodes
			<span style="color: #0000FF;">WHERE</span> Generation <span style="color: #808080;">=</span> @Generation
				<span style="color: #808080;">AND</span> p <span style="color: #808080;">NOT</span> <span style="color: #808080;">IN</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> p <span style="color: #0000FF;">FROM</span> @Backtrace<span style="color: #808080;">&#41;</span>
&nbsp;
			<span style="color: #0000FF;">INSERT</span> @Backtrace
				<span style="color: #808080;">&#40;</span> p <span style="color: #808080;">&#41;</span>
&nbsp;
			<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">DISTINCT</span> r
			<span style="color: #0000FF;">FROM</span> @Nodes
			<span style="color: #0000FF;">WHERE</span> Generation <span style="color: #808080;">=</span> @Generation
&nbsp;
			<span style="color: #0000FF;">SELECT</span> @Generation <span style="color: #808080;">=</span> @Generation <span style="color: #808080;">-</span> <span style="color: #000;">1</span>
		<span style="color: #0000FF;">END</span>
<span style="color: #0000FF;">END</span>
&nbsp;
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">FROM</span> @Nodes <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> Generation, r, p</pre></td></tr></table></div>

<p>The procedure returns the following columns:</p>
<ul>
<li><strong>Generation</strong>: The amount of generations of leaf node expansion</li>
<li><strong>P</strong>: The progressive node ID</li>
<li><strong>R</strong>: The regressive node ID</li>
</ul>
<p>The regressive ID is in fact the node&#8217;s parent and the progressive ID its child. This way we can obtain the (outward) direction in the result set. Results can represent multiple shortest routes as long as they both have the minimum amount of steps (Generations) necessary.</p>
<p>If we would want to obtain the shortest route(s) between two nodes with ID&#8217;s 776 and 777 this would be a valid result.</p>
<p><strong>Execution of the Stored Procedure:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">EXEC BFS @<span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">776</span><span style="color: #66cc66;">,</span>  @<span style="color: #993333; font-weight: bold;">TO</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">777</span></pre></div></div>

<p><strong>The result set:</strong></p>
<table border="1" cellspacing="1" cellpadding="2">
<tbody>
<tr>
<th align="left">Generation</th>
<th align="left">p</th>
<th align="left">r</th>
</tr>
<tr>
<td>0</td>
<td>776</td>
<td></td>
</tr>
<tr>
<td>1</td>
<td>2881</td>
<td>776</td>
</tr>
<tr>
<td>1</td>
<td>3198</td>
<td>776</td>
</tr>
<tr>
<td>2</td>
<td>3362</td>
<td>2881</td>
</tr>
<tr>
<td>2</td>
<td>1582</td>
<td>3198</td>
</tr>
<tr>
<td>3</td>
<td>1579</td>
<td>1582</td>
</tr>
<tr>
<td>3</td>
<td>1262</td>
<td>3362</td>
</tr>
<tr>
<td>4</td>
<td>777</td>
<td>1262</td>
</tr>
<tr>
<td>4</td>
<td>777</td>
<td>1579</td>
</tr>
</tbody>
</table>
<p>As you can see this result set proposes two shortest routes between nodes 776 and 777.</p>
<p>In practice this technique has its limitations. It is not heuristic so brute force is required. With each generation that the leafs expand there is an exponential amount of nodes to be dealt with. Luckily there are ways to chop in two the exponential strain this procedure poses for the DB. One of these is the <a href="http://en.wikipedia.org/wiki/Bidirectional_search" target="_blank">bidirectional search</a> method. Alas, I will not disclose <span style="text-decoration: underline;">that</span> particular implementation.</p>
<p>Go code or something <img src='http://www.martienus.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.martienus.com/code/t-sql-breadth-first-shortest-route-search-stored-procedure.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>WYSIWYG Editing: A glimpse of the Future?</title>
		<link>http://www.martienus.com/thoughts/wysiwyg-editing-a-glimpse-of-the-future.html</link>
		<comments>http://www.martienus.com/thoughts/wysiwyg-editing-a-glimpse-of-the-future.html#comments</comments>
		<pubDate>Tue, 13 May 2008 18:18:01 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[thoughts]]></category>
		<category><![CDATA[editing]]></category>
		<category><![CDATA[formatting language]]></category>
		<category><![CDATA[wysiwyg]]></category>

		<guid isPermaLink="false">http://www.martienus.com/thoughts/wysiwyg-editing-a-glimpse-of-the-future.html</guid>
		<description><![CDATA[Wysiwyg, one of the buzzwords in the 90’s, has experienced a decline in popularity over the past years. Although prevalent in word processing applications among others it still is considered a must in rapid application development, for instance web application development.
Web application developers seek complete control over the output of their applications, not relying on [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal"><span lang="EN-US">Wysiwyg, one of the buzzwords in the 90’s, has experienced a decline in popularity over the past years. Although prevalent in word processing applications among others it still is considered a must in rapid application development, for instance web application development.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Web application developers seek complete control over the output of their applications, not relying on typical wysiwyg implementation of HTML constructs. Also, in a typical web application production environment, it is hard to have a wysiwyg representation of the final product where the sources come from many files and are subject to business logic, view engines and what not.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">The biggest hurdle to be taken though, in my opinion, is that every wysiwyg editing experience is confined to the implementation language. We speak of tables and divs and spans whereas would we want the same visual product implemented in another formatting language we would have to change our ways of thinking and moreover: think language-specific.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Let’s say you want to make an HTML layout with elements like tables, spans, divs etc. What if you had a set of building blocks of which one is say: a box. You can attribute to that box certain semantics so that it breaks a line, or is tabular. This box could also easily be implemented in another formatting language, like OOXML. This way we can think of wysiwyg building blocks which we can decouple from language-specific implementation.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">What’s the catch? Certain functionalities between languages are incomparable or through imagination too hard to abstract for it to be seen as one and the same. Also, too abstract a way of thinking makes you lose touch with the implementation level where, as mentioned above, a great deal of control is desired.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Enter the customizable building blocks.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">What if you could have complete control of how building blocks and their semantics are implemented on the language-specific level? This way developers would have complete control of the final implementation of their designs. Also, different flavors can be thought of for specific end-user requirements. This makes porting designs not only between formatting languages easy but also between different implementations of those languages a breeze.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">The main problem though is that there should be consensus between all the parties involved who manage the many formatting languages that are out there. These languages need to be abstracted to a point where they sufficiently overlap. A degree of standardization is therefore necessary.<o:p></o:p></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.martienus.com/thoughts/wysiwyg-editing-a-glimpse-of-the-future.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript: Remove duplicates from Array</title>
		<link>http://www.martienus.com/code/javascript-remove-duplicates-from-array.html</link>
		<comments>http://www.martienus.com/code/javascript-remove-duplicates-from-array.html#comments</comments>
		<pubDate>Fri, 21 Dec 2007 18:25:04 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[duplicate]]></category>
		<category><![CDATA[duplicates]]></category>
		<category><![CDATA[entries]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[remove]]></category>

		<guid isPermaLink="false">http://www.martienus.com/code/javascript-remove-duplicates-from-array.html</guid>
		<description><![CDATA[Removing duplicate entries or values from a Javascript array is something which isn&#8217;t accomodated for by the native functions in Javascript. I searched Google for a few solutions but they were all lacking something in my opinion, be it performance or just sheer elegance.
*UPDATE*
There is now an array prototype function for removing duplicates. I suggest [...]]]></description>
			<content:encoded><![CDATA[<p>Removing duplicate entries or values from a Javascript array is something which isn&#8217;t accomodated for by the native functions in Javascript. I searched Google for a few solutions but they were all lacking something in my opinion, be it performance or just sheer elegance.</p>
<p>*UPDATE*</p>
<p>There is now an array prototype function for removing duplicates. I suggest you use the prototype function instead of the functions below. It is the way it should have been done in the first place and is faster and moreover the most correct way to do it.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">unique</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> r <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	o<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> n <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> y <span style="color: #339933;">=</span> r.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> y<span style="color: #339933;">;</span> x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>r<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">continue</span> o<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		r<span style="color: #009900;">&#91;</span>r.<span style="color: #660066;">length</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">return</span> r<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You can now utilize the unique function like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">7</span><span style="color: #339933;">,</span><span style="color: #CC0000;">8</span><span style="color: #339933;">,</span><span style="color: #CC0000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> unique <span style="color: #339933;">=</span> arr.<span style="color: #660066;">unique</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>unique<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The result will be [1,2,3,4,5,6,7,8,9].</p>
<p>*UPDATE*</p>
<p>I therefore made two variants myself:</p>
<p>The first one does what you&#8217;ll expect, it&#8217;ll regard the first encountered entry as the original and all subsequent entries as duplicates.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> unique<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #003366; font-weight: bold;">var</span> r <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   o<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> n <span style="color: #339933;">=</span> a.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> y <span style="color: #339933;">=</span> r.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> y<span style="color: #339933;">;</span> x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
         <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>r<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span>a<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">continue</span> o<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      r<span style="color: #009900;">&#91;</span>r.<span style="color: #660066;">length</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> a<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000066; font-weight: bold;">return</span> r<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If we pass the following array [1, 2, 3, 1, 4, 5] to the function the result will be [1, 2, 3, 4, 5].</p>
<p>The second variant returns different results. It will regard the last encountered duplicate as the original. This may be desirable in certain situations.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> unique<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #003366; font-weight: bold;">var</span> r <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   o<span style="color: #339933;">:</span><span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> n <span style="color: #339933;">=</span> a.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> i <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span> <span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> n<span style="color: #339933;">;</span> x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
         <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span>a<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">continue</span> o<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      r<span style="color: #009900;">&#91;</span>r.<span style="color: #660066;">length</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> a<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000066; font-weight: bold;">return</span> r<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The output in this case will be [2, 3, 1, 4, 5].</p>
<p>I wrote both these functions with performance in mind. I have thoroughly tested and profiled both of them. Here are the benefits over other solutions to the problem:</p>
<ul>
<li>it&#8217;s just one function</li>
<li>the loop length is determined through the object model just once per loop (see the <strong>for</strong> statements: var i = 0, n = a.length;)</li>
<li>after a duplicate has been detected in the nested loop it doesn&#8217;t iterate further but goes on to the next entry in the source array (see <strong>continue</strong> statement)</li>
</ul>
<p>You can easily adapt the function to make it a prototype function of an Array object. If people don&#8217;t know how to do it just ask, i&#8217;ll add it.</p>
<p>Hope this helps someone out!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.martienus.com/code/javascript-remove-duplicates-from-array.html/feed</wfw:commentRss>
		<slash:comments>43</slash:comments>
		</item>
		<item>
		<title>Regex: validate e-mail address in PHP</title>
		<link>http://www.martienus.com/code/regex-validate-e-mail-address-in-php.html</link>
		<comments>http://www.martienus.com/code/regex-validate-e-mail-address-in-php.html#comments</comments>
		<pubDate>Fri, 21 Dec 2007 18:23:03 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.martienus.com/coding/regex-validate-e-mail-address-in-php.html</guid>
		<description><![CDATA[Validating whether an e-mail address conforms to the (informal) e-mail address specification is a good way of improving the overall validity of the information entered by a user. Regular expressions can help us in accomplishing this.
The following example doesn&#8217;t follow the RFC 2822 specification per se but enforces the more strict rules which are common [...]]]></description>
			<content:encoded><![CDATA[<p>Validating whether an e-mail address conforms to the (informal) e-mail address specification is a good way of improving the overall validity of the information entered by a user. Regular expressions can help us in accomplishing this.</p>
<p>The following example doesn&#8217;t follow the <a href="http://tools.ietf.org/html/rfc2822" target="_blank">RFC 2822</a> specification per se but enforces the more strict rules which are common for the internet nowadays and for instance enforced by Live Hotmail and Gmail.</p>
<p>The Wikipedia article about <a href="http://en.wikipedia.org/wiki/E-mail_address" target="_blank">e-mail addresses</a> is somewhat easier to digest than the original specification and is a good starting point for e-mail validation techniques.</p>
<p>Here goes!</p>
<h3>Anatomy for i.e. <strong>john@doe.example.com</strong>:</h3>
<ul>
<li><strong>john</strong> is the local name</li>
<li><strong>doe</strong> is the subdomain name</li>
<li><strong>example</strong> is the domain name</li>
<li><strong>com</strong> is the top-level domain name</li>
</ul>
<h3>The rules:</h3>
<ul>
<li>Local name may only contain letters, digits, hyphen &#8216;-&#8217;, underscore &#8216;_&#8217; and periods &#8216;.&#8217;</li>
<li>Local name may not begin and/or end with a period</li>
<li>Local name may not contain two or more subsequent periods &#8216;..&#8217;</li>
<li>Sub-domain name may only contain letters, digits and hyphen</li>
<li>Multiple sub-domains are permitted</li>
<li>Domain name may only contain letters, digits and hyphen</li>
<li>Domain name and sub-domain name may not begin and/or end with a hyphen</li>
<li>Domain name and sub-domain must be between 2 to 63 characters long</li>
<li>Top-level domain may only contain letters</li>
<li>Top-level domain must be between 2 to 6 characters long</li>
<li>Sub-domain names, the domain name and the top-level domain name are separated by single periods &#8216;.&#8217;</li>
</ul>
<h3>The regular expression (Perl compatible):</h3>

<div class="wp_syntax"><div class="code"><pre class="reg" style="font-family:monospace;">/^<span style="color: #000000;">&#91;</span>A-z0-9\-_<span style="color: #000000;">&#93;</span>+<span style="color: #000000;">&#40;</span>\.<span style="color: #000000;">&#91;</span>A-z0-9\-_<span style="color: #000000;">&#93;</span>+<span style="color: #000000;">&#41;</span>*@<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>A-z0-9<span style="color: #000000;">&#93;</span>+\-?<span style="color: #000000;">&#91;</span>A-z0-9<span style="color: #000000;">&#93;</span>+<span style="color: #000000;">&#41;</span>+\.<span style="color: #000000;">&#41;</span>+<span style="color: #000000;">&#91;</span>A-z<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#123;</span>2,6<span style="color: #000000;">&#125;</span>$/</pre></div></div>

<p>And a PHP function to validate the e-mail addresses:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Pass the e-mail address to the function.</span>
<span style="color: #666666; font-style: italic;">// Will return true when valid, false when invalid.</span>
<span style="color: #000000; font-weight: bold;">function</span> validate_emailaddress<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^[A-z0-9\-_]+(\.[A-z0-9\-_]+)*@(([A-z0-9]+\-?[A-z0-9]+)+\.)+[A-z]{2,6}$/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that the solution shown will discount alot of possible <a href="http://tools.ietf.org/html/rfc2822" target="_blank">RFC 2822</a> compliant e-mail adressess as invalid but these are mostly used in intranet settings. Stricter rules apply for the web and these are the basis for the expression I&#8217;ve made.</p>
<p>What the expression doesn&#8217;t account for:</p>
<ul>
<li>It doesn&#8217;t check whether the subdomain and domain names&#8217; length exceeds the maximum of 63 characters</li>
<li>Every made up top-level domain will pass, given it is between 2 and 6 characters long</li>
<li>Something I might have missed?</li>
</ul>
<p>If you have a solution for the aforementioned shortcomings, please let me know!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.martienus.com/code/regex-validate-e-mail-address-in-php.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Convert Magic eDeveloper / Pervasive date: `Days since AD` in PHP</title>
		<link>http://www.martienus.com/code/convert-magic-edeveloper-pervasive-date-days-since-ad-in-php.html</link>
		<comments>http://www.martienus.com/code/convert-magic-edeveloper-pervasive-date-days-since-ad-in-php.html#comments</comments>
		<pubDate>Fri, 21 Dec 2007 18:07:34 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[convert]]></category>
		<category><![CDATA[Date Format]]></category>
		<category><![CDATA[Magic eDeveloper]]></category>
		<category><![CDATA[Pervasive]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.martienus.com/code/convert-magic-edeveloper-pervasive-date-days-since-ad-in-php.html</guid>
		<description><![CDATA[A while ago I was working on a PHP web-application that uses a Pervasive SQL database which belonged to a Magic eDeveloper application. I encountered a really weird date format in the database which I couldn&#8217;t really place. I had a hunch that it might be the amount of days since AD (01-01-0000). This turned [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago I was working on a PHP web-application that uses a Pervasive SQL database which belonged to a Magic eDeveloper application. I encountered a really weird date format in the database which I couldn&#8217;t really place. I had a hunch that it might be the amount of days since AD (01-01-0000). This turned out to be right. Me and a friend devised a way to convert these dates to a unix timestamp in the following manner:</p>
<ul>
<li>Determine the amount of days from AD (01-01-0000) to the Unix epoch (01-01-1970). Outcome: 719163.</li>
<li>Subtract the weird date with the amount determined above.</li>
<li>Multiply the outcome by the amount of seconds in a day (86400).</li>
</ul>
<p>Presto, there you have your unix timestamp.</p>
<p>The code in PHP:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// A simple way to convert Magic date to a unix timestamp and vice versa</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;AD_TO_UNIXEPOCH&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">719163</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SECONDS_IN_DAY&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">86400</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Function that converts the days to unixtime</span>
<span style="color: #000000; font-weight: bold;">function</span> days2unixtime<span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span> <span style="color: #339933;">-</span> AD_TO_UNIXEPOCH<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> SECONDS_IN_DAY<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// The other way around</span>
<span style="color: #000000; font-weight: bold;">function</span> unixtime2days<span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">return</span> AD_TO_UNIXEPOCH <span style="color: #339933;">+</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span> <span style="color: #339933;">/</span> SECONDS_IN_DAY<span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Proof of concept</span>
<span style="color: #000088;">$magicdate</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">732468</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$unixtime</span> <span style="color: #339933;">=</span> days2unixtime<span style="color: #009900;">&#40;</span><span style="color: #000088;">$magicdate</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #990000;">strftime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%d</span>-%m-%Y&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$unixtime</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: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Prepare for a hellish output ;)</span>
<span style="color: #b1b100;">print</span> unixtime2days<span style="color: #009900;">&#40;</span><span style="color: #000088;">$unixtime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// And converted back</span></pre></td></tr></table></div>

<p>NB: For dates before the Unix epoch you will get a negative result.</p>
<p>Hope this helps someone who&#8217;s encountered the same problem.<br />
If you know a better solution please leave a comment or mail.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.martienus.com/code/convert-magic-edeveloper-pervasive-date-days-since-ad-in-php.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Welcome to my blog!</title>
		<link>http://www.martienus.com/thoughts/welcome-to-my-blog.html</link>
		<comments>http://www.martienus.com/thoughts/welcome-to-my-blog.html#comments</comments>
		<pubDate>Fri, 21 Dec 2007 17:00:27 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
				<category><![CDATA[thoughts]]></category>

		<guid isPermaLink="false">http://www.martienus.com/code/welcome-to-my-blog.html</guid>
		<description><![CDATA[Hello! Welcome to my new blog. This blog will be mostly programming oriented whereas I also have a myspace blog where I post some more personal stuff. What can we expect from this blog? Well, for starters there will be a lot of code snippets of how I tackle some common problems in my profession [...]]]></description>
			<content:encoded><![CDATA[<p>Hello! Welcome to my new blog. This blog will be mostly programming oriented whereas I also have a <a href="http://www.myspace.com/martienus" target="_blank">myspace</a> blog where I post some more personal stuff. What can we expect from this blog? Well, for starters there will be a lot of code snippets of how I tackle some common problems in my profession as a programmer. I&#8217;ve looked around for solutions to these problems on the internet and I found out that most solutions are poorly written, or lack good documentation. I got so fed up with it that I wanted to do it <span style="text-decoration: underline;">right</span> this time. Hence the blog!</p>
<p>Feel free to look around. If you have any questions regarding this site or my person go to the contact page, or just leave a comment. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.martienus.com/thoughts/welcome-to-my-blog.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 2.048 seconds -->
<!-- Cached page served by WP-Cache -->
