<?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>html &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/html/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Computer Algorithms: Finding the Lowest Common Ancestor</title>
		<link>/2012/08/24/computer-algorithms-finding-the-lowest-common-ancestor/</link>
		<comments>/2012/08/24/computer-algorithms-finding-the-lowest-common-ancestor/#comments</comments>
		<pubDate>Fri, 24 Aug 2012 12:54:54 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[B-tree]]></category>
		<category><![CDATA[Binary search tree]]></category>
		<category><![CDATA[binary search trees]]></category>
		<category><![CDATA[Binary trees]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Graph theory]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[Lowest common ancestor]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[proper solution]]></category>
		<category><![CDATA[Rope]]></category>
		<category><![CDATA[Ternary tree]]></category>
		<category><![CDATA[Tree]]></category>
		<category><![CDATA[two algorithms]]></category>

		<guid isPermaLink="false">/?p=3314</guid>
		<description><![CDATA[Introduction Here’s one task related to the tree data structure. Given two nodes, can you find their lowest common ancestor? In a matter of fact this task always has a proper solution, because at least the root node is a common ancestor of all pairs of nodes. However here the task is to find the &#8230; <a href="/2012/08/24/computer-algorithms-finding-the-lowest-common-ancestor/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Finding the Lowest Common Ancestor</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/07/03/computer-algorithms-balancing-a-binary-search-tree/" rel="bookmark" title="Computer Algorithms: Balancing a Binary Search Tree">Computer Algorithms: Balancing a Binary Search Tree </a></li>
<li><a href="/2012/06/22/computer-algorithms-binary-search-tree-data-structure/" rel="bookmark" title="Computer Algorithms: Binary Search Tree">Computer Algorithms: Binary Search Tree </a></li>
<li><a href="/2012/08/07/computer-algorithms-heap-and-heapsort-data-structure/" rel="bookmark" title="Computer Algorithms: Heap and Heapsort">Computer Algorithms: Heap and Heapsort </a></li>
<li><a href="/2010/09/29/construct-a-sorted-php-linked-list/" rel="bookmark" title="Construct a Sorted PHP Linked List">Construct a Sorted PHP Linked List </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>Here’s one task related to the tree data structure. Given two nodes, can you find their lowest common ancestor? </p>
<p>In a matter of fact this task always has a proper solution, because at least the root node is a common ancestor of all pairs of nodes. However here the task is to find the lowest one, which can be quite far from the root. </p>
<figure id="attachment_3315" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/08/1.-Finding-the-Lowest-Common-Ancestor.png"><img src="/wp-content/uploads/2012/08/1.-Finding-the-Lowest-Common-Ancestor.png" alt="Finding the Lowest Common Ancestor" title="Finding the Lowest Common Ancestor" width="620" height="362" class="size-full wp-image-3315" srcset="/wp-content/uploads/2012/08/1.-Finding-the-Lowest-Common-Ancestor.png 620w, /wp-content/uploads/2012/08/1.-Finding-the-Lowest-Common-Ancestor-300x175.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">Finding the Lowest Common Ancestor</figcaption></figure>
<p>We don’t care what kind of trees we have. However the solution, as we will see, can be very different depending on the tree type. Indeed finding the lowest common ancestor can have linear complexity for binary search trees, which isn’t true for ordinary trees.<span id="more-3314"></span></p>
<h2>Overview</h2>
<p>Let’s say we have a tree (not binary!) and two nodes from this tree. The task is to find their lowest common ancestor. The thing is that we don’t know much about where they appear to be in the tree. </p>
<p>We can think of this tree as a DOM tree of any single HTML page online. It is not binary or balanced and can’t be sure where these nodes are. </p>
<figure id="attachment_3317" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/08/2.-Lowest-Common-Ancestor.png"><img src="/wp-content/uploads/2012/08/2.-Lowest-Common-Ancestor.png" alt="Lowest Common Ancestor" title="Lowest Common Ancestor" width="620" height="399" class="size-full wp-image-3317" srcset="/wp-content/uploads/2012/08/2.-Lowest-Common-Ancestor.png 620w, /wp-content/uploads/2012/08/2.-Lowest-Common-Ancestor-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">&nbsp;</figcaption></figure>
<p>First we must find both paths from the root to each one of the target nodes. Note that this requires additional memory! Then, in linear time we can pass through these “paths” and scan them from the root down to the nodes. We expect these to arrays to be equal at least in their first element (the root).  Using this scenario the lowest common ancestor is the last equal element in both arrays. </p>
<figure id="attachment_3318" style="width: 621px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/08/3.-Lowest-Common-Ancestor.png"><img src="/wp-content/uploads/2012/08/3.-Lowest-Common-Ancestor.png" alt="Lowest Common Ancestor Paths" title="Lowest Common Ancestor Paths" width="621" height="338" class="size-full wp-image-3318" srcset="/wp-content/uploads/2012/08/3.-Lowest-Common-Ancestor.png 621w, /wp-content/uploads/2012/08/3.-Lowest-Common-Ancestor-300x163.png 300w" sizes="(max-width: 621px) 100vw, 621px" /></a><figcaption class="wp-caption-text">Once we know the paths from the root down to the nodes, we can compare them in order to find the lowest common ancestor!</figcaption></figure>
<p>To see how this algorithm can be dramatically changed depending on the data structure, let’s see another example. Now let’s say we have a binary search tree (BST). We know that in a BST all the elements in the left sub-tree are smaller than the root and all the items on the right sub-tree are greater than the root. This is true also for the left and the right sub-trees.</p>
<p>Now because we’re searching the lowest common ancestor, we don’t need to collect the paths from the root to the nodes in two arrays. We just know that the greater items are on the right, while the smaller items are on the left. This can help us find the lowest ancestor starting directly from the root.</p>
<figure id="attachment_3319" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/08/4.-Lowest-Common-in-a-BST.png"><img src="/wp-content/uploads/2012/08/4.-Lowest-Common-in-a-BST.png" alt="Lowest Common in a BST" title="Lowest Common in a BST" width="620" height="399" class="size-full wp-image-3319" srcset="/wp-content/uploads/2012/08/4.-Lowest-Common-in-a-BST.png 620w, /wp-content/uploads/2012/08/4.-Lowest-Common-in-a-BST-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">In a BST we compare both values with a given node (starting from the root). In case the node&#8217;s value is in between them &#8211; this is the lowest common ancestor. If not &#8211; we go either on the left or on the right!</figcaption></figure>
<p>What we do is to compare the two keys of the target nodes with the root key. If one of the keys are smaller, and the other is greater than the root&#8217;s key, then obviously the root is the lowest common ancestor. This is true because one of the items will be somewhere in the left sub-tree, while the other will be in the right sub-tree. </p>
<p>In case both values are greater (or smaller) than the root, we can move to the right (or to the left) sub-tree and try again with the same procedure. Thus the first node which key is in between the two target values will be the lowest common ancestor.</p>
<h2>Code</h2>
<p>Here’s a very simple PHP implementation showing us these two algorithms.</p>
<pre lang="PHP">
class Tree
{
    public $node = null;
    public $id = null;
    public $parent = null;
    public $children = array();
    
    public function __construct($node, $id = null)
    {
        $this->node = $node;
        $this->id = $id;
    }
    
    public function addChild(Node &$n)
    {
        $n->parent = $this;
        $this->children[] = $n;
    }
    
    /**
     * Returns an element by its id
     * 
     * @param mixed $id
     * @return Node 
     */
    public function search($id)
    {
        if ($this->id == $id) {
            return $this;
        }

        $a = false;
        
        // search all the children starting from the left-most
        foreach ($this->children as $child) {
            $a = $child->search($id);
        }
        
        return $a;
    }
    
    /**
     * Finds a path from the root to the 
     * item and returns it as a list
     * 
     * @param mixed $id 
     * @return array
     */
    public function find_path($id, &$path)
    {
        array_push($path, $this->id);
        
        if ($this->id == $id) {
            return 1;
        }
        
        foreach ($this->children as $child)  {
            if (1 == $child->find_path($id, $path)) return 1;
            array_pop($path);
        }
    }
    
    public function __toString()
    {
        return $this->node . ' ' . $this->id . "\n";
    }
}

$dom = new Tree('DOM', 'ROOT');

$body = new Tree('BODY', 1);
$div1 = new Tree('DIV', 'div-1');
$div2 = new Tree('DIV', 'my-id');

$a = new Tree("A", 'some-link');

$dom->addChild($body);
$body->addChild($div1);
$body->addChild($div2);
$div2->addChild($a);

$path1 = $path2 = array();
$dom->find_path('div-1', $path1);
$dom->find_path('some-link', $path2);
</pre>
<h3>Finding Lowest Common Ancestor in a BST</h3>
<pre lang="PHP">
class Tree
{
    public $key;
    
    public $parent  = null;
    public $left    = null;
    public $right   = null;
    
    public function __construct($key) 
    {
        $this->key = $key;
    }
    
    public function insert(Tree $n) 
    {
        if ($this->key < $n->key) {
            if ($this->right == null) {
                // insert
                $this->right = $n;
                $n->parent = $this;
            } else {
                $this->right->insert($n);
            }
        }
        if ($this->key > $n->key) {
            if ($this->left == null) {
                // insert
                $this->left = $n;
                $n->parent = $this;
            } else {
                $this->left->insert($n);
            }
        }
    }
}

$t = new Tree(10);

$n1 = new Tree(20);
$n2 = new Tree(5);
$n3 = new Tree(7);
$n4 = new Tree(13);

$t->insert($n1);
$t->insert($n2);
$t->insert($n3);

function find_common($node1, $node2, $tree) 
{
    if ($node1->key < $tree->key && $node2->key > $tree->key) {
        return $tree;
    } else if ($node1->key < $tree->key && $node2->key < $tree->key) {
        find_common($node1, $node2, $tree->left);
    } else if ($node1->key > $tree->key && $node2->key > $tree->key) {
        find_common($node1, $node2, $tree->right);
    }
}

$node = find_common($n3, $n4, $t);
</pre>
<h2>Application</h2>
<p>A typical use-case of this algorithm is finding the lowest common ancestor of two nodes in a DOM tree. Sometimes we just need to attach an event listener to both items (even before they are attached to the DOM!). Although attaching this event to the “document” will work just fine, all the elements from the nodes up to the root will be “capturing” these events due to event bubbling. Thus attaching the event to the lowest common ancestor is a better solution.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/07/03/computer-algorithms-balancing-a-binary-search-tree/" rel="bookmark" title="Computer Algorithms: Balancing a Binary Search Tree">Computer Algorithms: Balancing a Binary Search Tree </a></li>
<li><a href="/2012/06/22/computer-algorithms-binary-search-tree-data-structure/" rel="bookmark" title="Computer Algorithms: Binary Search Tree">Computer Algorithms: Binary Search Tree </a></li>
<li><a href="/2012/08/07/computer-algorithms-heap-and-heapsort-data-structure/" rel="bookmark" title="Computer Algorithms: Heap and Heapsort">Computer Algorithms: Heap and Heapsort </a></li>
<li><a href="/2010/09/29/construct-a-sorted-php-linked-list/" rel="bookmark" title="Construct a Sorted PHP Linked List">Construct a Sorted PHP Linked List </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/08/24/computer-algorithms-finding-the-lowest-common-ancestor/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Computer Algorithms: Data Compression with Diagram Encoding and Pattern Substitution</title>
		<link>/2012/01/23/computer-algorithms-data-compression-with-diagram-encoding-and-pattern-substitution/</link>
		<comments>/2012/01/23/computer-algorithms-data-compression-with-diagram-encoding-and-pattern-substitution/#respond</comments>
		<pubDate>Mon, 23 Jan 2012 14:58:48 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Coding theory]]></category>
		<category><![CDATA[Computer file formats]]></category>
		<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[conventional compressing tool]]></category>
		<category><![CDATA[Data compression]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[Information theory]]></category>
		<category><![CDATA[Lossy compression]]></category>
		<category><![CDATA[pattern substitution algorithm]]></category>
		<category><![CDATA[pattern substitution algorithms]]></category>
		<category><![CDATA[Pattern Substitution The pattern substitution algorithm]]></category>
		<category><![CDATA[Run-length encoding]]></category>
		<category><![CDATA[web hosting]]></category>

		<guid isPermaLink="false">/?p=2623</guid>
		<description><![CDATA[Overview Two variants of run-length encoding are the diagram encoding and the pattern substitution algorithms. The diagram encoding is actually a very simple algorithm. Unlike run-length encoding, where the input stream must consists of many repeating elements, as “aaaaaaaa” for instance, which are very rare in a natural language, there are many so called “diagrams” &#8230; <a href="/2012/01/23/computer-algorithms-data-compression-with-diagram-encoding-and-pattern-substitution/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Data Compression with Diagram Encoding and Pattern Substitution</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/01/30/computer-algorithms-data-compression-with-relative-encoding/" rel="bookmark" title="Computer Algorithms: Data Compression with Relative Encoding">Computer Algorithms: Data Compression with Relative Encoding </a></li>
<li><a href="/2012/02/06/computer-algorithms-data-compression-with-prefix-encoding/" rel="bookmark" title="Computer Algorithms: Data Compression with Prefix Encoding">Computer Algorithms: Data Compression with Prefix Encoding </a></li>
<li><a href="/2012/01/09/computer-algorithms-data-compression-with-run-length-encoding/" rel="bookmark" title="Computer Algorithms: Data Compression with Run-length Encoding">Computer Algorithms: Data Compression with Run-length Encoding </a></li>
<li><a href="/2012/01/16/computer-algorithms-data-compression-with-bitmaps/" rel="bookmark" title="Computer Algorithms: Data Compression with Bitmaps">Computer Algorithms: Data Compression with Bitmaps </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Overview</h2>
<p>Two variants of <a href="/2012/01/09/computer-algorithms-data-compression-with-run-length-encoding/" title="Computer Algorithms: Data Compression with Run-length Encoding">run-length encoding</a> are the diagram encoding and the pattern substitution algorithms. The diagram encoding is actually a very simple algorithm. Unlike run-length encoding, where the input stream must consists of many repeating elements, as <strong>“aaaaaaaa”</strong> for instance, which are very rare in a natural language, there are many so called “diagrams” in almost any natural language. In plain English there are some diagrams as <strong>“the”</strong>, <strong>“and”</strong>, <strong>“ing”</strong> (in the word “waiting” for example), <strong>“ a”</strong>, <strong>“ t”</strong>, <strong>“ e”</strong> and many doubled letters. Actually we can extend those diagrams by adding surrounding spaces. Thus we can encode not only “the”, but “ the “, which are 5 characters (2 spaces and 3 letters) with something shorter. In the other hand, as I said, in plain English there are two many doubled letters, which unfortunately aren’t something special for run-length encoding and the compression ratio will be small. Even worse the encoded text may happen to be longer than the input message. Let’s see some examples.</p>
<p>Let’s say we’ve to encode the message “successfully accomplished”, which consists of four doubled letters. However to compress it with run-length encoding we’ll need at least 8 characters, which doesn’t help us a lot.</p>
<pre>
// 8 chars replaced by 8 chars!?
input: 	"successfully accomplished"
output:	"su2ce2sfu2ly a2complished"
</pre>
<p>The problem is that if the input text contains numbers, “2” in particular, we’ve to chose an escape symbol (“@” for example), which we’ll use to mark where the encoded run begins. Thus if the input message is “2 successfully accomplished tasks”, it will be encoded as “2 su@2ce@2sfu@2ly a@2complished tasks”. Now the output message is longer!!! than the input string.</p>
<pre>
// the compressed message is longer!!!
input:	"2 successfully accomplished"
output:	"2 su@2ce@2sfu@2ly a@2complished tasks"
</pre>
<p>Again if the input stream contains the escape symbol, we have to find another one, and the problem is that it is often too difficult to find short escape symbol that doesn’t appear in the input text, without a full scan of the text.<span id="more-2623"></span></p>
<p>That is why run-length encoding isn’t a good solution when compressing plain text, where long runs rarely appear. Well, of course, there are exceptions. For example such an exception is the lossy text compression with run-length encoding. It is intuitively clear that compressing text with loss is rarely useful, especially when you’ve to decompress exactly the same text. However there are some cases that lossy compression may be useful. Such case can be removing spaces. Indeed the text <strong>“successfully      accomplished”</strong> brings us exactly the same information as <strong>“successfully accomplished”</strong>. In this case we can simply remove those spaces. Indeed we can use a marker to indicate the long run of spaces like <strong>“successfully@6 accomplished”</strong> in order to decompress the input string with absolutely no loss, but we can also throw those symbols away. This desision depends on the goal. Exactly with the same goal in mind we can remove new lines and tabs, only if we’re sure that the sense of the text is preserved. Yet again, a problem is that such long runs don’t happen to occur in random texts. That is why it’s better to use diagram encoding for plain text compression instead of run-length encoding.</p>
<h2>Few Questions</h2>
<p>After understanding the principles of the diagram encoding, let’s see some examples. In the example above it is better to replace doubled letters with something shorter. Let’s say # for “cc”, @ for “ss” and % for “ll”. Thus the input text will be compressed as “su#e@fu%y a#omplished”,  which is shorter. But yet again what will happen if the input message contains one of the substitutions? Also we can’t say if there are many doubled letters and enough reasonable substitutions for them. A better approach is to replace patterns. </p>
<figure id="attachment_2640" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/01/DiagramEncodingonTexts.png"><img src="/wp-content/uploads/2012/01/DiagramEncodingonTexts.png" alt="Compressing texts with diagram encoding" title="Compressing texts with diagram encoding" width="620" class="size-full wp-image-2640" srcset="/wp-content/uploads/2012/01/DiagramEncodingonTexts.png 683w, /wp-content/uploads/2012/01/DiagramEncodingonTexts-300x128.png 300w" sizes="(max-width: 683px) 100vw, 683px" /></a><figcaption class="wp-caption-text">Run-length encoding isn&#039;t a good approach for text compression, because long runs rarely appear in a natural language.</figcaption></figure>
<h2>Pattern Substitution</h2>
<p>The pattern substitution algorithm is a variant of the diagram encoding. As I said above in plain English a very commonly used pattern can be “ the “, which is five characters long. We can now replace it with something like “$%” for example. In this case the message <strong>“I send the message”</strong> will become <strong>“I send$%message”</strong>. However there are some obstacles to overcome.</p>
<p>The first problem is that we need to know the language and somehow to define commonly used patterns in a dictionary. What would happen with a message written in some language we don’t know nothing about. Let’s say &#8211; Latin like the example bellow.</p>
<blockquote><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras venenatis, sapien eget suscipit placerat, justo quam blandit mauris, quis tempor ante sapien sodales augue. Praesent ut mauris quam. Phasellus scelerisque, ante quis consequat tristique, metus turpis consectetur leo, vitae facilisis sapien mi eu sapien. Praesent vitae ligula elit, et faucibus augue. Sed rhoncus sodales dolor ut gravida. In quis augue ac nulla auctor mattis sed sed libero. Donec eget purus eget enim tempor porta vitae eget diam. Mauris aliquet malesuada ipsum, non pulvinar urna vestibulum ac. Donec feugiat velit vitae nunc cursus imperdiet. Donec accumsan faucibus dictum. Phasellus sed mauris sapien. Maecenas mi metus, tincidunt sed rhoncus nec, sodales non sapien.</p></blockquote>
<p>Clearly without knowing Latin it isn’t easy to define which are those commonly used patterns. The thing is that it&#8217;s better to use pattern substitution if you know in advance the set of words and characters.</p>
<p>The second problem is related to decompression. It is obvious that we need to define a dictionary and this dictionary must be used when decoding the message. It will be great also if we find more patterns longer than three characters. If not, the compression ratio will be low. Unfortunately such patterns aren’t very common in any natural language.</p>
<figure id="attachment_2643" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/01/PatternSubstitutiononTexts.png"><img src="/wp-content/uploads/2012/01/PatternSubstitutiononTexts.png" alt="Text compression with diagram encoding and pattern substitution" title="Text compression with diagram encoding and pattern substitution" width="620" class="size-full wp-image-2643" srcset="/wp-content/uploads/2012/01/PatternSubstitutiononTexts.png 683w, /wp-content/uploads/2012/01/PatternSubstitutiononTexts-300x128.png 300w" sizes="(max-width: 683px) 100vw, 683px" /></a><figcaption class="wp-caption-text">Diagram encoding and pattern substitution are far more suitable for text compression than run-length encoding. In fact, pattern substitution is very effective on compressing programming languages.</figcaption></figure>
<h2>Application</h2>
<p>It is interesting to answer the question, how to use diagram encoding or patter substitution to compress text in natural language, especially when we don’t know the language in detail? The answer hides in the question. We wont compress natural languages, but machine language. Exactly machine (programming) languages are limited to a smaller sets of words and symbols. Isn’t it true for any programing language? Like PHP, where words like <strong>“function”</strong>, <strong>“while”</strong>, <strong>“for”</strong>, <strong>“break”</strong>, <strong>“switch”</strong>, <strong>“foreach”</strong> happen to be often in use, or HTML with its defined set of tags. Perhaps the best example is CSS, where only the values of the properties can vary. CSS files also tend to have multiple new lines, tabs and spaces, which only humans read.</p>
<p>The question here is why should we compress those file types. It’s clear that after the compression they will be completely useless, both for humans and machines. Yes, that is true, but what if we have to store versions of those files into a DB. Kind of a backup. Imagine you’re working for a web hosting company that has to store daily versions of the sites it’s hosting. Thus the volume of stored information even for small companies hosting only few sites can be enormous. The problem is that compressing those files with some conventional compressing tool isn’t a good idea. Thus we’ve to save a copy of the entire site every day, but as we know the difference between daily versions of a site can be small. A version control system is another solution, but then you’ve to store the plain text of the files. </p>
<p>Perhaps a better approach is to compress the text using pattern substitution and then saving only differences &#8211; kind of version control, which can be done with “relative encoding”.</p>
<p>Using the above method we can save lots of disk space and in the same time we can compress/decompress easily. Another good thing is that you can save only changes to the initial files, like version control, which can also be compressed.</p>
<h2>Implementation</h2>
<p>The implementation of this algorithm is again on PHP and tries only to describe the main principles of compression. In this case I tried to compress a CSS file using the compression above. Although this example is quite primitive we can see some interesting facts. First of all you only need encoding and decoding dictionaries. Practically the encoding and decoding processes are equal, so you don’t need to implement two different functions. Here in this example a native PHP function is used &#8211; str_replace, because the purpose of this algorithm is not to describe pattern substitution techniques, but pattern substitution. It assumes that today’s programming languages have string manipulation functions for the purposes of this task.</p>
<pre lang="PHP">
$str = file_get_contents('large_style_file.css');

$encoding_dict = array(
	"\n" 		=> '$0',
	'text' 		=> '$1',
	'color' 	=> '$2',
	'display' 	=> '$3',
	'font' 		=> '$4',
	'width' 	=> '$5',
	'height'	=> '$6',	
	' '		=> '',
);

function replace_patterns($input, $dict) 
{
	foreach ($dict as $pattern => $replace) {
		$input = str_replace($pattern, $replace, $input);
	}
	
	return $input;
}

$result = replace_patterns($str, $encoding_dict);
</pre>
<p>By only replacing few CSS properties I achieved almost 40% of compression ratio (as shows the diagram bellow). The initial file is 202 KB, while compressed it&#8217;s only 131 KB. Of course, it all depends on the CSS file, but how about replacing all property names with shorter ones. Perhaps then the compression will be even better.</p>
<figure id="attachment_2647" style="width: 600px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/01/chart_1.png"><img src="/wp-content/uploads/2012/01/chart_1.png" alt="CSS compression with pattern substitution" title="CSS compression with pattern substitution" width="600" height="371" class="size-full wp-image-2647" srcset="/wp-content/uploads/2012/01/chart_1.png 600w, /wp-content/uploads/2012/01/chart_1-300x185.png 300w" sizes="(max-width: 600px) 100vw, 600px" /></a><figcaption class="wp-caption-text"> </figcaption></figure>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/01/30/computer-algorithms-data-compression-with-relative-encoding/" rel="bookmark" title="Computer Algorithms: Data Compression with Relative Encoding">Computer Algorithms: Data Compression with Relative Encoding </a></li>
<li><a href="/2012/02/06/computer-algorithms-data-compression-with-prefix-encoding/" rel="bookmark" title="Computer Algorithms: Data Compression with Prefix Encoding">Computer Algorithms: Data Compression with Prefix Encoding </a></li>
<li><a href="/2012/01/09/computer-algorithms-data-compression-with-run-length-encoding/" rel="bookmark" title="Computer Algorithms: Data Compression with Run-length Encoding">Computer Algorithms: Data Compression with Run-length Encoding </a></li>
<li><a href="/2012/01/16/computer-algorithms-data-compression-with-bitmaps/" rel="bookmark" title="Computer Algorithms: Data Compression with Bitmaps">Computer Algorithms: Data Compression with Bitmaps </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/01/23/computer-algorithms-data-compression-with-diagram-encoding-and-pattern-substitution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A JavaScript Trick You Should Know</title>
		<link>/2011/07/12/a-javascript-trick-you-should-know/</link>
		<comments>/2011/07/12/a-javascript-trick-you-should-know/#comments</comments>
		<pubDate>Tue, 12 Jul 2011 14:49:46 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Brace notation]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Concatenation]]></category>
		<category><![CDATA[Data types]]></category>
		<category><![CDATA[Formal languages]]></category>
		<category><![CDATA[heredoc]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[JavaScript syntax]]></category>
		<category><![CDATA[js solution]]></category>
		<category><![CDATA[possible solution]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">/?p=2333</guid>
		<description><![CDATA[JavaScript Strings You should know that in JavaScript you cannot simply write a multilined code chunk, i.e. something like this: var str = 'hello world'; This will result in an error, which can be a problem when you deal with large strings. Imagine a string containing some HTML that should be injected via innerHTML. Now &#8230; <a href="/2011/07/12/a-javascript-trick-you-should-know/" class="more-link">Continue reading <span class="screen-reader-text">A JavaScript Trick You Should Know</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2011/08/18/powerful-php-less-known-string-manipulation/" rel="bookmark" title="Powerful PHP: Less Known String Manipulation">Powerful PHP: Less Known String Manipulation </a></li>
<li><a href="/2010/08/26/javascript-flexibility-regex-match/" rel="bookmark" title="JavaScript Flexibility &#8211; Regex match()">JavaScript Flexibility &#8211; Regex match() </a></li>
<li><a href="/2010/08/24/flexible-javascript-splitting-strings/" rel="bookmark" title="Flexible JavaScript &#8211; Splitting Strings">Flexible JavaScript &#8211; Splitting Strings </a></li>
<li><a href="/2010/08/25/flexible-javascript-replace-in-a-string/" rel="bookmark" title="Flexible JavaScript &#8211; Replace in a String">Flexible JavaScript &#8211; Replace in a String </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>JavaScript Strings</h2>
<p>You should know that in JavaScript you cannot simply write a multilined code chunk, i.e. something like this:</p>
<pre lang="javascript">
var str = 'hello
world';
</pre>
<p>This will result in an error, which can be a problem when you deal with large strings. Imagine a string containing some HTML that should be injected via innerHTML. Now one possible solution is to concatenate two or more strings, by splitting the initial string.</p>
<pre lang="javascript">
var str = 'hello'
        + 'world';
</pre>
<p>Howerver this solution result in some additional operations like concatenation. It can be very nice if there was something like the PHP multiline string format.</p>
<h2>The PHP Example</h2>
<p>In PHP you have the same use case with strings. You can have a string concatenated over multiple lines, but you cannot split lines like so:</p>
<pre lang="php">
// the following line is wrong
$str = 'hello
world'; 

// a possible solution to the line above is
$str = 'hello'
     . 'world';
// which is very similar to the js solution in the second example

// ... but in PHP there is the so called HEREDOC
$str = <<<EOD
hello
world
EOD;
</pre>
<p>The heredoc gives us the power to write multiline strings. </p>
<h2>The JavaScript Trick</h2>
<p>Actually in JavaScript there is something that's exactly what Heredoc is meant to be for PHP. Take a look at the last example:</p>
<pre lang="javascript">
var text = <>
this <br />
is
my
multi-line
text
</>.toString();

document.body.innerHTML = text;
</pre>
<p>Thus you can write multiline strings in the JavaScript code.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2011/08/18/powerful-php-less-known-string-manipulation/" rel="bookmark" title="Powerful PHP: Less Known String Manipulation">Powerful PHP: Less Known String Manipulation </a></li>
<li><a href="/2010/08/26/javascript-flexibility-regex-match/" rel="bookmark" title="JavaScript Flexibility &#8211; Regex match()">JavaScript Flexibility &#8211; Regex match() </a></li>
<li><a href="/2010/08/24/flexible-javascript-splitting-strings/" rel="bookmark" title="Flexible JavaScript &#8211; Splitting Strings">Flexible JavaScript &#8211; Splitting Strings </a></li>
<li><a href="/2010/08/25/flexible-javascript-replace-in-a-string/" rel="bookmark" title="Flexible JavaScript &#8211; Replace in a String">Flexible JavaScript &#8211; Replace in a String </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/07/12/a-javascript-trick-you-should-know/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>jQuery.unbind()</title>
		<link>/2011/04/05/jquery-unbind/</link>
		<comments>/2011/04/05/jquery-unbind/#comments</comments>
		<pubDate>Tue, 05 Apr 2011 08:51:08 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[Button]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Event]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[HTTP cookie]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jquery method]]></category>
		<category><![CDATA[Microsoft Binder]]></category>
		<category><![CDATA[obvious solution]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[possible solution]]></category>
		<category><![CDATA[RME]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[unbind]]></category>
		<category><![CDATA[User interface]]></category>
		<category><![CDATA[Widgets]]></category>

		<guid isPermaLink="false">/?p=2240</guid>
		<description><![CDATA[Binding Problems Typically in jQuery when you bind an HTML element with some event this event is fired every time the user (client) triggers it. Thus when you&#8217;ve a click event attached on a button you can click it as many times as you want. In some rare cases this can be tricky. Let&#8217;s imagine &#8230; <a href="/2011/04/05/jquery-unbind/" class="more-link">Continue reading <span class="screen-reader-text">jQuery.unbind()</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2009/12/30/jquery-live-vs-bind-performance/" rel="bookmark" title="jQuery live() vs bind() performance">jQuery live() vs bind() performance </a></li>
<li><a href="/2009/05/25/remove-dom-element-with-jquery/" rel="bookmark" title="Remove DOM Element with JQuery">Remove DOM Element with JQuery </a></li>
<li><a href="/2009/10/21/event-driven-programming-with-jquery-part-2-events-in-jquery/" rel="bookmark" title="Event driven programming with jQuery &#8211; (part 2). Events in jQuery.">Event driven programming with jQuery &#8211; (part 2). Events in jQuery. </a></li>
<li><a href="/2009/10/22/event-driven-programming-with-jquery-part-3-what-is-a-module/" rel="bookmark" title="Event driven programming with jQuery &#8211; (part 3). What is a module?">Event driven programming with jQuery &#8211; (part 3). What is a module? </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Binding Problems</h2>
<figure id="attachment_2254" style="width: 460px" class="wp-caption aligncenter"><a href="http://jquery.com/" target="_blank"><img class="size-full wp-image-2254" title="jquery applications" src="/wp-content/uploads/2011/04/jquery_applications.jpg" alt="jQuery JavaScript Library" width="460" height="240" srcset="/wp-content/uploads/2011/04/jquery_applications.jpg 460w, /wp-content/uploads/2011/04/jquery_applications-300x156.jpg 300w" sizes="(max-width: 460px) 100vw, 460px" /></a><figcaption class="wp-caption-text">jQuery JavaScript Library</figcaption></figure>
<p>Typically in <a title="jQuery on stoimen.com/blog" href="/tag/jquery/">jQuery</a> when you bind an <a title="HTML on stoimen.com/blog" href="/tag/html/">HTML</a> element with some event this event is fired every time the user (client) triggers it. Thus when you&#8217;ve a click event attached on a button you can click it as many times as you want. In some rare cases this can be tricky. Let&#8217;s imagine the following scenario.</p>
<ol>
<li>The user clicks on a &#8220;vote&#8221; button.</li>
<li>Some AJAX calls are performed.</li>
<li>After a successful AJAX call you setup a cookie to deny further votes from this machine.</li>
</ol>
<p>This seems to be pretty well known scenario, but as the click event is attached to the button there is enough time to click several times on the &#8220;vote&#8221; button and to vote several times. In this case before the cookie is set the user can vote more than once.<span id="more-2240"></span></p>
<h3>Demo</h3>
<p><iframe frameborder="0" scrolling="no" src="http://stoimen.com/projects/jquery.unbind/example1.php" style="border:1px solid #ccc;width:500px"></iframe></p>
<p>So one possible solution is to unbind the click event. In jQuery terms this is done by &#8230;</p>
<h2>$.unbind()</h2>
<p>With this method you can simply unbind the &#8220;click&#8221; event or whatever event there is attached on the desired HTML element. So now in the first case we had:</p>
<pre lang="javascript">
$(document).ready(function() {
    $('input').click(function() {
        $.ajax({ url : 'http://stoimen.com/projects/jquery.unbind/example1.php'
               , success : function() {
                    $('p').append('You\'ve voted successfully!<br />');
               }    
        }); 
    }); 
});
</pre>
<p>Now you can replace the code above with:</p>
<pre lang="javascript">
$(document).ready(function() {
    $('input').click(function() {
        $('input').unbind();
        $.ajax({ url : 'http://stoimen.com/projects/jquery.unbind/example1.php'
               , success : function() {
                    $('p').append('You\'ve voted successfully!<br />');
               }    
        }); 
    }); 
});
</pre>
<p>As you can see on the demo here now the button cannot be clicked more than once.</p>
<h3>Demo</h3>
<p><iframe frameborder="0" scrolling="no" src="http://stoimen.com/projects/jquery.unbind/example2.php" style="border:1px solid #ccc;width:500px"></iframe></p>
<h2>Cookies + unbind()</h2>
<p>There is a problem however. After reloading the page the event is attached again, so the user can click on the button once more. The obvious solution is to combine both the cookie and the unbind() as shown in the following pseudo code.</p>
<pre lang="javascript">
$(document).ready(function() {
    if (cookie is not set) { 
        $('input').click(function() {
            $('input').unbind();
            $.ajax({ url : 'http://stoimen.com/projects/jquery.unbind/example1.php'
                   , success : function() {
                        $('p').append('You\'ve voted successfully!<br />');
                        set cookie
                   }    
            }); 
        });
    } 
});
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2009/12/30/jquery-live-vs-bind-performance/" rel="bookmark" title="jQuery live() vs bind() performance">jQuery live() vs bind() performance </a></li>
<li><a href="/2009/05/25/remove-dom-element-with-jquery/" rel="bookmark" title="Remove DOM Element with JQuery">Remove DOM Element with JQuery </a></li>
<li><a href="/2009/10/21/event-driven-programming-with-jquery-part-2-events-in-jquery/" rel="bookmark" title="Event driven programming with jQuery &#8211; (part 2). Events in jQuery.">Event driven programming with jQuery &#8211; (part 2). Events in jQuery. </a></li>
<li><a href="/2009/10/22/event-driven-programming-with-jquery-part-3-what-is-a-module/" rel="bookmark" title="Event driven programming with jQuery &#8211; (part 3). What is a module?">Event driven programming with jQuery &#8211; (part 3). What is a module? </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/04/05/jquery-unbind/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Collect the Images and Meta Tags from a Webpage with PHP</title>
		<link>/2011/02/25/how-to-collect-the-images-and-meta-tags-from-a-webpage-with-php/</link>
		<comments>/2011/02/25/how-to-collect-the-images-and-meta-tags-from-a-webpage-with-php/#comments</comments>
		<pubDate>Fri, 25 Feb 2011 09:23:50 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Facebook Inc]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[HTML element]]></category>
		<category><![CDATA[LinkedIn Corporation]]></category>
		<category><![CDATA[Meta element]]></category>
		<category><![CDATA[Meta Tags]]></category>
		<category><![CDATA[Online social networking]]></category>
		<category><![CDATA[Search engine optimization]]></category>
		<category><![CDATA[search engines]]></category>
		<category><![CDATA[social services]]></category>
		<category><![CDATA[tag]]></category>
		<category><![CDATA[Technical communication]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Web 2.0]]></category>
		<category><![CDATA[Web page]]></category>
		<category><![CDATA[World Wide Web]]></category>

		<guid isPermaLink="false">/?p=2216</guid>
		<description><![CDATA[Meta Tags and the Facebook Example You&#8217;ve definitely seen the &#8220;share a link&#8221; screen in Facebook. When you paste a link into the box (fig. 1) and press the &#8220;Attach&#8221; button you&#8217;ll get the prompted cite parsed with a title, description and possibly thumb (fig. 2). This functionality is well known in Facebook, but it &#8230; <a href="/2011/02/25/how-to-collect-the-images-and-meta-tags-from-a-webpage-with-php/" class="more-link">Continue reading <span class="screen-reader-text">How to Collect the Images and Meta Tags from a Webpage with PHP</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/04/20/how-to-sanitize-user-input-in-php/" rel="bookmark" title="How to Sanitize User Input in PHP?">How to Sanitize User Input in PHP? </a></li>
<li><a href="/2010/08/03/html-tags/" rel="bookmark" title="HTML Tags: &lt;base&gt;">HTML Tags: &lt;base&gt; </a></li>
<li><a href="/2010/09/10/automatically-upload-images-with-php-directly-from-the-uri/" rel="bookmark" title="Automatically Upload Images with PHP Directly from the URI">Automatically Upload Images with PHP Directly from the URI </a></li>
<li><a href="/2011/01/18/download-images-with-php/" rel="bookmark" title="Download Images with PHP">Download Images with PHP </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Meta Tags and the Facebook Example</h2>
<p>You&#8217;ve definitely seen the &#8220;share a link&#8221; screen in <a title="Facebook Homepage" href="http://www.facebook.com/" target="_blank">Facebook</a>. When you paste a link into the box (fig. 1) and press the &#8220;Attach&#8221; button you&#8217;ll get the prompted cite parsed with a title, description and possibly thumb (fig. 2). This functionality is well known in Facebook, but it appears to be well known also in various social services. In fact <a title="LinkedIn Homepage" href="http://www.linkedin.com/" target="_blank">Linkedin</a>, <a title="Reddit Homepage" href="http://www.reddit.com/" target="_blank">Reddit</a>, <a title="DZone Homepage" href="http://www.dzone.com/" target="_blank">Dzone</a>&#8216;s <a title="DZone Bookmarklet" href="http://www.dzone.com/links/add.html" target="_blank">bookmarklet</a> use it.</p>
<figure id="attachment_2222" style="width: 518px" class="wp-caption aligncenter"><a href="/wp-content/uploads/2011/02/facebook_attach_prompt_screen.png"><img class="size-full wp-image-2222" title="facebook_attach_prompt_screen" src="/wp-content/uploads/2011/02/facebook_attach_prompt_screen.png" alt="Facebook Attach a Link Prompt Screen" width="518" height="155" srcset="/wp-content/uploads/2011/02/facebook_attach_prompt_screen.png 518w, /wp-content/uploads/2011/02/facebook_attach_prompt_screen-300x89.png 300w" sizes="(max-width: 518px) 100vw, 518px" /></a><figcaption class="wp-caption-text">fig. 1 - Facebook Attach a Link Prompt Screen</figcaption></figure>
<p>Fist thing to notice is that this information, prompted by Facebook, is the same as the meta tag information. However there is a slight difference.</p>
<figure id="attachment_2224" style="width: 526px" class="wp-caption aligncenter"><a href="/wp-content/uploads/2011/02/facebook_attached_link_screen.png"><img class="size-full wp-image-2224" title="facebook_attached_link_screen" src="/wp-content/uploads/2011/02/facebook_attached_link_screen.png" alt="Facebook Attached Link Screen" width="526" height="362" srcset="/wp-content/uploads/2011/02/facebook_attached_link_screen.png 526w, /wp-content/uploads/2011/02/facebook_attached_link_screen-300x206.png 300w" sizes="(max-width: 526px) 100vw, 526px" /></a><figcaption class="wp-caption-text">fig. 2 - Facebook Attached Link Screen</figcaption></figure>
<p>Facebook prefers for the thumb the image set into the &lt;meta property=&#8221;og:image&#8221; &#8230; /&gt;. In the case above this tag appears to be:</p>
<pre lang="html4strict" escaped="true">
<meta property="og:image" content="http://b.vimeocdn.com/ts/572/975/57297584_200.jpg" />
</pre>
<p>And the image pointed in the SRC attribute is exactly the same as the one prompted by Facebook (fig. 3).</p>
<figure id="attachment_2229" style="width: 200px" class="wp-caption aligncenter"><a href="/wp-content/uploads/2011/02/vimeo_thumb.jpg"><img class="size-full wp-image-2229" title="vimeo_thumb" src="/wp-content/uploads/2011/02/vimeo_thumb.jpg" alt="Vimeo Thumb" width="200" height="150" /></a><figcaption class="wp-caption-text">fig. 3 - Vimeo Thumb</figcaption></figure>
<p>First thing to note is that the real thumb is bigger than the thumb shown in Facebook, so Facebook resizes it and the second thing to note is that there are more meta tags of the og:&#8230; format.<span id="more-2216"></span></p>
<h2>Meta Tags and The Open Graph Protocol</h2>
<p>By default meta tags contain various information about the web page. They are not visible in the webpage, but contain some info about it. The most common meta tags are the title, description and keywords tags. They of course contain the title of the page, not that this can be different from the &lt;title&gt; tag, a short description of the page and some keywords describing the content of the page. They are well known also because the search engines make use of them when trying to collect information about the page and the process of SEO passes through it.</p>
<p>However the <a title="Default HTML Meta Tags Specification" href="http://www.w3schools.com/tags/tag_meta.asp" target="_blank">default HTML meta tags</a> cannot contain everything. Thus for example you cannot point the preferable thumbnail for a webpage. The solution is the <a title="The Open Graph Protocol Homepage" href="http://ogp.me/" target="_blank">Open Graph Protocol</a>. It comes with meta tags that can contain more and more valuable info. Such a tag is the og:image meta tag. Note that all the Open Graph (og) meta tags are defined by the og: prefix before the entity name. Thus og:image comes for images, while og:longitude for geo positioning.</p>
<p>That&#8217;s really useful, but how you can read them?</p>
<h2>PHP, Meta Tags and Regexps</h2>
<p>When you try to read information from a webpage source the first possible path is by using <a title="Regular Expressions Explained on Wikipedia" href="http://en.wikipedia.org/wiki/Regular_expression" target="_blank">regular expressions</a>. However PHP is smart enough to offer you some useful functions. Such a function is <a title="PHP get_meta_tags Function Documentation" href="http://php.net/manual/en/function.get-meta-tags.php" target="_blank">get_meta_tags()</a>. As you may guess this method reads the meta tags by given URL.</p>
<pre lang="php" escaped="true">
$a = get_meta_tags('http://vimeo.com/10758212');
var_dump($a);
</pre>
<p>However this method can&#8217;t read Open Graph tags. So finally you&#8217;ve to use some regexps.</p>
<pre lang="php" escaped="true">
preg_match('/<meta property="og:image" content="(.*?)" \/>/', $source, $matches);
</pre>
<p>Now you can grab the og:image tag. And even more &#8211; grab every image (&lt;img&gt;) from that page.</p>
<pre lang="php" escaped="true">
preg_match_all('/<img src="(.*?)"/', $source, $m);
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/04/20/how-to-sanitize-user-input-in-php/" rel="bookmark" title="How to Sanitize User Input in PHP?">How to Sanitize User Input in PHP? </a></li>
<li><a href="/2010/08/03/html-tags/" rel="bookmark" title="HTML Tags: &lt;base&gt;">HTML Tags: &lt;base&gt; </a></li>
<li><a href="/2010/09/10/automatically-upload-images-with-php-directly-from-the-uri/" rel="bookmark" title="Automatically Upload Images with PHP Directly from the URI">Automatically Upload Images with PHP Directly from the URI </a></li>
<li><a href="/2011/01/18/download-images-with-php/" rel="bookmark" title="Download Images with PHP">Download Images with PHP </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/02/25/how-to-collect-the-images-and-meta-tags-from-a-webpage-with-php/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>From SVG to Geo Coordinates &#8211; A Complete Guide</title>
		<link>/2011/02/11/from-svg-to-geo-coordinates-a-complete-guide/</link>
		<comments>/2011/02/11/from-svg-to-geo-coordinates-a-complete-guide/#comments</comments>
		<pubDate>Fri, 11 Feb 2011 08:30:11 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Algebraic curves]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[approaching algorithm]]></category>
		<category><![CDATA[Bézier curve]]></category>
		<category><![CDATA[Coordinates]]></category>
		<category><![CDATA[Cubic plane curve]]></category>
		<category><![CDATA[Curves]]></category>
		<category><![CDATA[Differential geometry of curves]]></category>
		<category><![CDATA[Geometry]]></category>
		<category><![CDATA[Graphic design]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[Interpolation]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Splines]]></category>
		<category><![CDATA[text editor]]></category>

		<guid isPermaLink="false">/?p=2174</guid>
		<description><![CDATA[Why This Task Is Not Trivial? First of all what do we have? There is a vector shape, which may represent a map, which we&#8217;d like to convert into a GEO map. In other words there is a SVG file containing the source shape, that you&#8217;d like to convert in geoJSON or whatever collection of &#8230; <a href="/2011/02/11/from-svg-to-geo-coordinates-a-complete-guide/" class="more-link">Continue reading <span class="screen-reader-text">From SVG to Geo Coordinates &#8211; A Complete Guide</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2009/04/14/you-should-not-insert-an-tag-in-another-tag-ie-breaks/" rel="bookmark" title="You should not insert an &#8220;a&#8221; tag in another &#8220;a&#8221; tag! &#8230;">You should not insert an &#8220;a&#8221; tag in another &#8220;a&#8221; tag! &#8230; </a></li>
<li><a href="/2012/01/30/computer-algorithms-data-compression-with-relative-encoding/" rel="bookmark" title="Computer Algorithms: Data Compression with Relative Encoding">Computer Algorithms: Data Compression with Relative Encoding </a></li>
<li><a href="/2011/03/29/scroll-an-iframe-content-to-a-predefined-position/" rel="bookmark" title="Scroll an IFRAME Content to a Predefined Position">Scroll an IFRAME Content to a Predefined Position </a></li>
<li><a href="/2011/02/25/how-to-collect-the-images-and-meta-tags-from-a-webpage-with-php/" rel="bookmark" title="How to Collect the Images and Meta Tags from a Webpage with PHP">How to Collect the Images and Meta Tags from a Webpage with PHP </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Why This Task Is Not Trivial?</h2>
<p>First of all what do we have? There is a vector shape, which may represent a map, which we&#8217;d like to convert into a GEO map. In other words there is a SVG file containing the source shape, that you&#8217;d like to convert in geoJSON or whatever collection of geo points. This is not trivial, of course, first of all because there&#8217;s no an algorithm or automation that can do this, and because everybody knows that the resulting map will be only approached, but will never be so accurate as the vector shape. This is because in a vector shape you may contain <a title="Bézier curve" href="http://en.wikipedia.org/wiki/B%C3%A9zier_curve" target="_blank">Bézier Curves</a>, which I&#8217;ll show a little bit later in this post, that are difficult to represent in geo coordinates.</p>
<p>So the first task will be to find an approaching algorithm. However there are two things that are optimistic:</p>
<ol>
<li>You can&#8217;t effectively represent Bézier curves in geo coordinates, but even if you could do it there&#8217;s no practical need, because the collection of geo coordinates will be huge and this will slow down you&#8217;re application. Remember that geoJSON is yet again possibly used by your browser and the amount of geo points will be proportionally slowing down the app.</li>
<li>As you may know Google&#8217;s visualization map is representing the World&#8217;s countries again with quite approached maps. Take a look at the following image &#8211; the country borders are quite sharpened:</li>
</ol>
<p style="text-align: center;"><a href="/wp-content/uploads/2011/02/Google_visualization_map.png"><img class="size-full wp-image-2183 aligncenter" title="Google_visualization_map" src="/wp-content/uploads/2011/02/Google_visualization_map.png" alt="Google Visualization Map" width="562" height="351" srcset="/wp-content/uploads/2011/02/Google_visualization_map.png 562w, /wp-content/uploads/2011/02/Google_visualization_map-300x187.png 300w" sizes="(max-width: 562px) 100vw, 562px" /></a></p>
<p>So far we know that we need an approaching algorithm that will convert vector lines and possibly curves in geo coordinates, but before we proceed we&#8217;ve to understand the SVG format.<span id="more-2174"></span></p>
<h2>SVG Format and Possible Approaches</h2>
<p>Let me give you an example of a SVG file. By opening such file with a text editor you can see the file format:</p>
<pre lang="svg">
M76.484,153.703c-0.86-0.27-3.816,0.199-4.002-1.055
	c-0.049-0.328-1.109-1.799-1.512-1.824c-0.647-0.04-5.182,1.896-5.219,2.013c-0.051,0.161,0.928,0.627,0.319,0.832
	c-0.028-0.003-0.716-0.36-0.816-0.334c-0.13,0.034-0.262,0.834-0.363,0.771c-0.176-0.24-0.179-0.48-0.007-0.719
	c-0.986-0.764-0.345,0.764-0.526,0.764c-0.771,0,0.184-0.849-0.675,0.037c-2.13-0.433-2.267-0.756-4.129-2.754
	c-1.329-1.42,0.383-8.158-1.571-8.535c-2.223-3.153-6.413-3.037-8.806-5.037c0.107-0.278,0.201-0.563,0.282-0.853
	c0.369,0.372-4.136-0.768-4.373-0.862c-1.498-0.604-3.574-0.133-4.656,0.355c-0.028-1.22,0.171-2.329-0.593-3.027
	c0.588-2.548-0.652-4.752-2.98-4.199c-0.443-1.354,0.142-0.868-1.171-1.483c-0.154-2.069,2.703-7.729-2.239-8.185
	c2.009-3.137-6.201-0.985-6.627-3.125c-0.14-0.703-5.574,1.563-5.752,0.192c-0.39-2.987-4.411-2.185-5.371-3.372
	c0.518,0.641-6.067,1.078-5.645,0.85c-0.118,0.063-1.38,5.328-1.307,4.994c-0.457,2.076,3.774,9.01,2.893,9.625
	c-0.495,0.347-3.159,6.972-1.295,8.588c-0.59-0.512,2.894-0.209,3.163-0.129c1.333,0.396,3.091,1.66,3.469,3.176
	c0.52,2.486-0.793,2.359-2.357,3.013c-0.658,0.274-2.289,6.779-2.368,7.461c-0.319,2.747-1.413,4.468-1.264,7.483
	c0.098,1.984-0.368,2.326,0.891,4.287c1.933,3.004,2.018,2.57,2.178,5.813c-0.127,1.556-1.742,3.565-1.568,5.388
	c0.074,0.77,0.485,2.028,0.235,2.795c-0.286,0.875-1.936,1.557-1.573,2.627c3.253-2.051,4.522,3.092,8.168,2.83
	c0.958-0.069,3.996,1.508,4.536,1.938c1.437,1.12,2.39-1.618,3.232-1.618c-1.037,0,3.461,2.315,2.95,1.774
	c0.828,0.873,3.852-0.123,5.026,0.304c2.416,0.881,6.011-1.386,7.227-2.174c1.616-1.048,3.15-1.38,4.396-2.816
	c0.639-0.737,2.375-4.448,3.254-4.366c0.354,0.032,0.59,1.01,1.06,0.652c0.163-0.126-1.075-2.664,0.689-2.664
	c0.935,0,4.283-2.113,3.799-3.344c-1.384-3.521,4.799-1.365,6.683-1.926c0.335-0.1,7.816-8.23,7.865-8.641
	C70.216,157.721,77.5,154.025,76.484,153.703C76.247,153.629,76.721,153.777,76.484,153.703z
</pre>
<p>This text represent this shape:</p>
<p><a href="/wp-content/uploads/2011/02/svg_map.png"><img class="aligncenter size-full wp-image-2190" title="svg_map" src="/wp-content/uploads/2011/02/svg_map.png" alt="SVG Map" width="84" height="92" /></a></p>
<p>First thing to notice is there are things quite different from the HTML markup. Of course this things are noticeable. There is a M, c and z where the first two are followed by some numbers. What are these characters? Well lets see at <a title="W3.org SVG" href="http://www.w3.org/TR/SVG/" target="_blank">W3.org</a>. There are lots of SVG specific commands, but we need to understand only few of them.</p>
<p>1.<strong> M</strong> (absolute) <strong>m</strong> (relative) &#8211; moveto</p>
<blockquote><p>Start a new sub-path at the given (x,y) coordinate.           <strong>M</strong> (uppercase) indicates that absolute           coordinates will follow; <strong>m</strong> (lowercase)           indicates that relative coordinates will follow. If a moveto  is           followed by multiple pairs of coordinates, the subsequent  pairs           are treated as implicit lineto commands. Hence, implicit  lineto           commands will be relative if the moveto is relative, and           absolute if the moveto is absolute. If a relative moveto           (<strong>m</strong>) appears as the first element of the path,           then it is treated as a pair of absolute coordinates. In this           case, subsequent pairs of coordinates are treated as relative           even though the initial moveto is interpreted as an absolute  moveto.</p></blockquote>
<p>2. <strong>Z</strong> or <strong>z</strong> &#8211; closepath</p>
<blockquote><p>Close the current subpath by drawing a straight line from the         current point to current subpath&#8217;s initial point. Since the Z  and z         commands take no parameters, they have an identical effect.</p></blockquote>
<p>3. <strong>C</strong> (absolute) <strong>c</strong> (relative) &#8211; curveto (x1 y1 x2 y2 x y)+</p>
<blockquote><p>Draws a cubic Bézier curve from the current         point to (x,y) using (x1,y1) as the control point at the         beginning of the curve and (x2,y2) as the control point at         the end of the curve. <strong>C</strong> (uppercase)         indicates that absolute coordinates will follow;         <strong>c</strong> (lowercase) indicates that relative         coordinates will follow. Multiple sets of coordinates may         be specified to draw a polybézier. At the end of the         command, the new current point becomes the final (x,y)         coordinate pair used in the polybézier.</p></blockquote>
<p>4. <strong>L</strong> (absolute) <strong>l</strong> (relative) &#8211; lineto (x y)+</p>
<blockquote><p>Draw a line from the current point to the given (x,y)         coordinate which becomes the new current point.         <strong>L</strong> (uppercase) indicates that absolute         coordinates will follow; <strong>l</strong> (lowercase)         indicates that relative coordinates will follow. A number         of coordinates pairs may be specified to draw a polyline.         At the end of the command, the new current point is set to         the final set of coordinates provided.</p></blockquote>
<h3>1. Bézier Curves</h3>
<p>So far we know that the especially the c command draws a curve &#8211; a Bézier curve. But what is a Bézier curve anyway? Here&#8217;s a little explanation you can find at <a title="Bézier curve" href="http://en.wikipedia.org/wiki/B%C3%A9zier_curve" target="_blank">Wikipedia</a>. Here&#8217;s an example of a cubic Bézier curve:</p>
<p><a href="/wp-content/uploads/2011/02/Cubic_Bezier_Curve.png"><img class="aligncenter size-full wp-image-2192" title="Cubic_Bezier_Curve" src="/wp-content/uploads/2011/02/Cubic_Bezier_Curve.png" alt="Cubic Bezier Curve" width="300" height="192" /></a>First thing we see is that here there are four points &#8211; p0, p1, p2, p3. The curve between p0 and p3 is defined by them and the two control points &#8211; p1 and p2. To be more precise let me show you an animation of how this curve is constructed:</p>
<p><a href="/wp-content/uploads/2011/02/Bezier_3_big.gif"><img class="aligncenter size-full wp-image-2193" title="Bezier_3_big" src="/wp-content/uploads/2011/02/Bezier_3_big.gif" alt="Cubic Bezier Curve" width="360" height="150" srcset="/wp-content/uploads/2011/02/Bezier_3_big.gif 360w, /wp-content/uploads/2011/02/Bezier_3_big-300x125.gif 300w" sizes="(max-width: 360px) 100vw, 360px" /></a></p>
<p>This is the way the red line is constructed with the help of the green and blue lines, and of course the points p1 and p2. The result is a smooth line that&#8217;s often used in animation and shapes. This is our case. As you&#8217;ve seen the example above is constructed only with curves.</p>
<p>Before we proceed let me say that there are lots of types of Bézier curves not only cubic. Here they are:</p>
<h4>1.1. Linear Bézier curve</h4>
<p><a href="/wp-content/uploads/2011/02/Bezier_1_big.gif"><img class="aligncenter size-full wp-image-2194" title="Bezier_1_big" src="/wp-content/uploads/2011/02/Bezier_1_big.gif" alt="Linear Bezier Curve" width="360" height="150" srcset="/wp-content/uploads/2011/02/Bezier_1_big.gif 360w, /wp-content/uploads/2011/02/Bezier_1_big-300x125.gif 300w" sizes="(max-width: 360px) 100vw, 360px" /></a></p>
<h4>1.2. Quadratic Bézier curve</h4>
<h4><a href="/wp-content/uploads/2011/02/Bezier_2_big.gif"><img class="aligncenter size-full wp-image-2195" title="Bezier_2_big" src="/wp-content/uploads/2011/02/Bezier_2_big.gif" alt="Quadratic Bezier Curve" width="360" height="150" srcset="/wp-content/uploads/2011/02/Bezier_2_big.gif 360w, /wp-content/uploads/2011/02/Bezier_2_big-300x125.gif 300w" sizes="(max-width: 360px) 100vw, 360px" /></a>1.3. And event Higher-order  curves:</h4>
<p><a href="/wp-content/uploads/2011/02/Bezier_4_big.gif"><img class="aligncenter size-full wp-image-2196" title="Bezier_4_big" src="/wp-content/uploads/2011/02/Bezier_4_big.gif" alt="Bezier Curves" width="360" height="150" srcset="/wp-content/uploads/2011/02/Bezier_4_big.gif 360w, /wp-content/uploads/2011/02/Bezier_4_big-300x125.gif 300w" sizes="(max-width: 360px) 100vw, 360px" /></a></p>
<h3>2. What You Need To Know About The SVG Format</h3>
<p>However the most important thing to know is the heart of SVG curve format. First you MOVE to a point with the <strong>M</strong> command. Lets see how does it look in our case:</p>
<p><strong>M76.484,153.703</strong> &#8211; This simply means: move to the point with coordinates (76.484, 153.703)</p>
<p>The next step is to draw a curve. We&#8217;ve seen that a cubic curve is defined by four points &#8211; p0, p1, p2 and p3, but what we have in the SVG format:</p>
<p><strong>c-0.86-0.27-3.816,0.199-4.002-1.055</strong></p>
<p>Here there are only three points (-0.86, -0.27), (-3.816, 0.199), (-4.002, -1.055) &#8211; not four. Well these are only the last three points, the first one is the one defined by the <strong>M</strong> command. Next thing to know is that the <strong>M</strong> command defines an absolute point in the canvas of the SVG file, while the <strong>c</strong> (shouldn&#8217;t be mistaken with <strong>C</strong>) is a relative curve. This means that if the p0 is the point (76.484, 153.703), the p1 point is relative to it with small difference in coordinates &#8211; (-0.86, -0.27).</p>
<p>However the most <em><strong>important</strong></em> thing to know is that after drawing the curve:</p>
<p><em>At the end of the         command, the new current point becomes the final (x,y)         coordinate pair used in the polybézier.</em></p>
<p>This is extremely important! This means that the last point of this curve becomes the p0 of the next curve, and the three points of the second curve, in our case:</p>
<p><strong>c-0.049-0.328-1.109-1.799-1.512-1.824</strong></p>
<p>are relative to the last (x, y) of the previous curve &#8211; (-4.002, -1.055). Without this you cannot convert any SVG shape into geo coordinates.</p>
<h3>3. Lines Instead of Curves</h3>
<p>OK, I said about an approaching algorithm, that is only approaching the curved vector shape. This means that I can simply replace the curves with lines. Thus I&#8217;ve to change to SVG file format from using the <strong>c</strong> command to the <strong>l</strong> command, as<strong> l</strong> is also relative. The curve:</p>
<p><strong>M76.484,153.703 c-0.86-0.27-3.816,0.199-4.002-1.055</strong></p>
<p>will become</p>
<p><strong>M76.484,153.703 l-4.002-1.055</strong></p>
<p>and so forth. This will result into the more sharpened shape:<a href="/wp-content/uploads/2011/02/SVG_Line_Map.png"><img class="aligncenter size-full wp-image-2199" title="SVG_Line_Map" src="/wp-content/uploads/2011/02/SVG_Line_Map.png" alt="SVG Line Map" width="86" height="90" /></a></p>
<p>However this is again a SVG file and no Geo coordinates are present. To end up with this there&#8217;s a tiny PHP script that collects all the last pairs from the c commands and converts them to Geo Coordinates relative to the World&#8217;s center &#8211; (Lat, Lon) = (0, 0).</p>
<pre lang="php">
<?php

$str = '-0.86-0.27-3.816,0.199-4.002-1.055
c-0.049-0.328-1.109-1.799-1.512-1.824c-0.647-0.04-5.182,1.896-5.219,2.013
c-0.051,0.161,0.928,0.627,0.319,0.832c-0.028-0.003-0.716-0.36-0.816-0.334
c-0.13,0.034-0.262,0.834-0.363,0.771c-0.176-0.24-0.179-0.48-0.007-0.719
c-0.986-0.764-0.345,0.764-0.526,0.764c-0.771,0,0.184-0.849-0.675,0.037
c-2.13-0.433-2.267-0.756-4.129-2.754c-1.329-1.42,0.383-8.158-1.571-8.535
c-2.223-3.153-6.413-3.037-8.806-5.037c0.107-0.278,0.201-0.563,0.282-0.853
c0.369,0.372-4.136-0.768-4.373-0.862c-1.498-0.604-3.574-0.133-4.656,0.355
c-0.028-1.22,0.171-2.329-0.593-3.027c0.588-2.548-0.652-4.752-2.98-4.199
c-0.443-1.354,0.142-0.868-1.171-1.483c-0.154-2.069,2.703-7.729-2.239-8.185
c2.009-3.137-6.201-0.985-6.627-3.125c-0.14-0.703-5.574,1.563-5.752,0.192
c-0.39-2.987-4.411-2.185-5.371-3.372c0.518,0.641-6.067,1.078-5.645,0.85
c-0.118,0.063-1.38,5.328-1.307,4.994c-0.457,2.076,3.774,9.01,2.893,9.625
c-0.495,0.347-3.159,6.972-1.295,8.588c-0.59-0.512,2.894-0.209,3.163-0.129
c1.333,0.396,3.091,1.66,3.469,3.176c0.52,2.486-0.793,2.359-2.357,3.013
c-0.658,0.274-2.289,6.779-2.368,7.461c-0.319,2.747-1.413,4.468-1.264,7.483
c0.098,1.984-0.368,2.326,0.891,4.287c1.933,3.004,2.018,2.57,2.178,5.813
c-0.127,1.556-1.742,3.565-1.568,5.388c0.074,0.77,0.485,2.028,0.235,2.795
c-0.286,0.875-1.936,1.557-1.573,2.627c3.253-2.051,4.522,3.092,8.168,2.83
c0.958-0.069,3.996,1.508,4.536,1.938c1.437,1.12,2.39-1.618,3.232-1.618
c-1.037,0,3.461,2.315,2.95,1.774c0.828,0.873,3.852-0.123,5.026,0.304
c2.416,0.881,6.011-1.386,7.227-2.174c1.616-1.048,3.15-1.38,4.396-2.816
c0.639-0.737,2.375-4.448,3.254-4.366c0.354,0.032,0.59,1.01,1.06,0.652
c0.163-0.126-1.075-2.664,0.689-2.664c0.935,0,4.283-2.113,3.799-3.344
c-1.384-3.521,4.799-1.365,6.683-1.926c0.335-0.1,7.816-8.23,7.865-8.641';


$matches = explode('c', $str);

$temp = array();
foreach ($matches as $k => $v) {
    // get the new relative point
    $s = $v;

    // a really dummy regex to find the last pair
    preg_match('/(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?/', $s, $m);


    // if the array is not empty
    if (!empty($temp)) {
        $m[5] = $temp[0]+$m[5];
        $m[6] = $temp[1]+$m[6];
    }

    echo '[', ($m[5]), ', ', ($m[6]), '],<br />';

    // save the last point
    $temp = array($m[5], $m[6]);
}
</pre>
<h2>Final Adjustments</h2>
<p>I guess you don&#8217;t need any shape in the World&#8217;s center as you may not need it in the same scale. There are two things to do. First you can scale the map by multiplying by a factor every point:</p>
<pre lang="php">
// a really dummy regex to find the last pair
    preg_match('/(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?/', $s, $m);

    // multiply by some factor
    $m[5] *= 0.05;
    $m[6] *= -0.05;

    // if the array is not empty
    if (!empty($temp)) {
        $m[5] = $temp[0]+$m[5];
        $m[6] = $temp[1]+$m[6];
    }
</pre>
<p>and than you can move the shape all over the world by adding some offset to the resulting point:</p>
<pre lang="php">
echo '[', ($m[5]-38.9), ', ', ($m[6]+10.7), '],<br />';
</pre>
<h2>Further Researches</h2>
<p>Here we just substitute the curves with lines, which is the most primitive approach. What about substituting each curve with corresponding two lines, or three lines. The resulting shape will be far more accurate in compare with the source &#8220;curved&#8221; shape. So there are still lots of things to be done. However this may help you do the job.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2009/04/14/you-should-not-insert-an-tag-in-another-tag-ie-breaks/" rel="bookmark" title="You should not insert an &#8220;a&#8221; tag in another &#8220;a&#8221; tag! &#8230;">You should not insert an &#8220;a&#8221; tag in another &#8220;a&#8221; tag! &#8230; </a></li>
<li><a href="/2012/01/30/computer-algorithms-data-compression-with-relative-encoding/" rel="bookmark" title="Computer Algorithms: Data Compression with Relative Encoding">Computer Algorithms: Data Compression with Relative Encoding </a></li>
<li><a href="/2011/03/29/scroll-an-iframe-content-to-a-predefined-position/" rel="bookmark" title="Scroll an IFRAME Content to a Predefined Position">Scroll an IFRAME Content to a Predefined Position </a></li>
<li><a href="/2011/02/25/how-to-collect-the-images-and-meta-tags-from-a-webpage-with-php/" rel="bookmark" title="How to Collect the Images and Meta Tags from a Webpage with PHP">How to Collect the Images and Meta Tags from a Webpage with PHP </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/02/11/from-svg-to-geo-coordinates-a-complete-guide/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Wanted &#8211; onfocus/onblur. Why They Don&#8217;t Work Always!</title>
		<link>/2011/02/09/wanted-onfocusonblur-why-they-dont-work-always/</link>
		<comments>/2011/02/09/wanted-onfocusonblur-why-they-dont-work-always/#respond</comments>
		<pubDate>Wed, 09 Feb 2011 14:11:48 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[DOM events]]></category>
		<category><![CDATA[Focus]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[Markup languages]]></category>
		<category><![CDATA[Tag soup]]></category>
		<category><![CDATA[Technical communication]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[World Wide Web]]></category>

		<guid isPermaLink="false">/?p=2160</guid>
		<description><![CDATA[On Focus Perhaps you think of onfocus and onblur events as a default behavior existing in any web page. This is not quite true! Onfocurs and onblur are well known in web developing (js) and are fired, of course, when the user tries to point something or leaves some element. Onfocurs is fired when the &#8230; <a href="/2011/02/09/wanted-onfocusonblur-why-they-dont-work-always/" class="more-link">Continue reading <span class="screen-reader-text">Wanted &#8211; onfocus/onblur. Why They Don&#8217;t Work Always!</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/04/20/how-to-sanitize-user-input-in-php/" rel="bookmark" title="How to Sanitize User Input in PHP?">How to Sanitize User Input in PHP? </a></li>
<li><a href="/2009/12/30/jquery-live-vs-bind-performance/" rel="bookmark" title="jQuery live() vs bind() performance">jQuery live() vs bind() performance </a></li>
<li><a href="/2010/09/24/2xjquery-select-a-selector/" rel="bookmark" title="2xjQuery: Select a Selector">2xjQuery: Select a Selector </a></li>
<li><a href="/2010/01/20/google-closure-compiler-doesnt-work/" rel="bookmark" title="Google Closure Compiler doesn&#8217;t work?!">Google Closure Compiler doesn&#8217;t work?! </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>On Focus</h2>
<p><a href="/wp-content/uploads/2011/02/onfocus.jpg"><img class="alignleft size-full wp-image-2177" title="onfocus" src="/wp-content/uploads/2011/02/onfocus.jpg" alt="onfocus" width="450" height="253" srcset="/wp-content/uploads/2011/02/onfocus.jpg 450w, /wp-content/uploads/2011/02/onfocus-300x168.jpg 300w" sizes="(max-width: 450px) 100vw, 450px" /></a></p>
<p>Perhaps you think of onfocus and onblur events as a default behavior existing in any web page. This is not quite true! Onfocurs and onblur are well known in web developing (js) and are fired, of course, when the user tries to point something or leaves some element. Onfocurs is fired when the user either goes to an element with the Tab button on with the mouse. When the element is on focus, evidently, the onfocus event is fired. Actually you can see which element is on focus, like an anchor or input, when the element is outlined by the browser by default. In the same scenario, when some element has been on focus and than the user switches to another element, the onblur event is fired. Thus you may guess that this element is no longer on focus.<span id="more-2160"></span></p>
<p>This in general are the onfocus/onblur events. The interesting part is that by default are known as built-in events in every page, but that is wrong.</p>
<h2>Where Are My Events?</h2>
<p>A typical use case is to take the code from some web page, where you&#8217;re sure this works perfectly, you can focus and blur element with no obstacle, but once you paste it inside your markup everything&#8217;s going wrong. Imagine you&#8217;ve the following markup (a typical search box text):</p>
<pre lang="html4strict">
<input type="text" onblur="if(this.value=='') this.value = 'Search in This Site';" onfocus="if(this.value=='Search in This Site') this.value = '';" />
</pre>
<p>This works perfectly until you paste it to your site. And than nothing works correctly. Why? Where&#8217;s the problem?</p>
<h2>Make Them Work</h2>
<p>Actually the problem isn&#8217;t hiding somewhere in the markup above. It&#8217;s in the HTML definition. There are lots of web pages using strict HTML definition, that looks like this:</p>
<pre lang="html4strict">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	...
</head>
<body>
	...
</pre>
<p>while you&#8217;ve to use the pure HTML definition to make onfocus/onblur work.</p>
<p>You&#8217;ve to switch to:</p>
<pre lang="html4strict">
<html>
<head>
	...
</head>
<body>
	...
</pre>
<p>Than you can continue using these useful events!</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/04/20/how-to-sanitize-user-input-in-php/" rel="bookmark" title="How to Sanitize User Input in PHP?">How to Sanitize User Input in PHP? </a></li>
<li><a href="/2009/12/30/jquery-live-vs-bind-performance/" rel="bookmark" title="jQuery live() vs bind() performance">jQuery live() vs bind() performance </a></li>
<li><a href="/2010/09/24/2xjquery-select-a-selector/" rel="bookmark" title="2xjQuery: Select a Selector">2xjQuery: Select a Selector </a></li>
<li><a href="/2010/01/20/google-closure-compiler-doesnt-work/" rel="bookmark" title="Google Closure Compiler doesn&#8217;t work?!">Google Closure Compiler doesn&#8217;t work?! </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/02/09/wanted-onfocusonblur-why-they-dont-work-always/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Few Thoughts on Web Video</title>
		<link>/2011/01/27/few-thoughts-on-web-video/</link>
		<comments>/2011/01/27/few-thoughts-on-web-video/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 09:19:00 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Adobe Flash]]></category>
		<category><![CDATA[Application software]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Container formats]]></category>
		<category><![CDATA[FFmpeg]]></category>
		<category><![CDATA[Flash Video]]></category>
		<category><![CDATA[Google Inc.]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[M&A]]></category>
		<category><![CDATA[MPEG-4]]></category>
		<category><![CDATA[Ogg]]></category>
		<category><![CDATA[On2 Technologies]]></category>
		<category><![CDATA[On2 Technologies Inc]]></category>
		<category><![CDATA[player]]></category>
		<category><![CDATA[Plex]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Theora]]></category>
		<category><![CDATA[Video codecs]]></category>
		<category><![CDATA[VP8]]></category>
		<category><![CDATA[web video field]]></category>

		<guid isPermaLink="false">/?p=2145</guid>
		<description><![CDATA[On Air It&#8217;s really full of video sharing websites out there, but in fact almost all of them use Flash player to display their video files. This is the reality now, but with the coming of HTML5, perhaps the things are changing a bit! First of all if you start dealing with video sharing platforms &#8230; <a href="/2011/01/27/few-thoughts-on-web-video/" class="more-link">Continue reading <span class="screen-reader-text">Few Thoughts on Web Video</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/03/05/video-sites-must-use-mp4-and-only-mp4/" rel="bookmark" title="Video sites must use &#8230; mp4 and only mp4!">Video sites must use &#8230; mp4 and only mp4! </a></li>
<li><a href="/2010/03/04/does-firefox-play-mp4-h-264-within-the-html5-video-tag/" rel="bookmark" title="Does Firefox play mp4 h.264 within the HTML5 video tag?">Does Firefox play mp4 h.264 within the HTML5 video tag? </a></li>
<li><a href="/2010/11/12/how-to-make-mp4-progressive-with-qt-faststart/" rel="bookmark" title="How to Make MP4 Progressive with qt-faststart">How to Make MP4 Progressive with qt-faststart </a></li>
<li><a href="/2011/02/04/reading-gps-latitude-and-longitude-from-image-and-video-files/" rel="bookmark" title="Reading GPS Latitude and Longitude from Image and Video Files">Reading GPS Latitude and Longitude from Image and Video Files </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>On Air</h2>
<p><a href="/wp-content/uploads/2011/01/975857_64336474.jpg"><img src="/wp-content/uploads/2011/01/975857_64336474.jpg" alt="" title="Few Thoughts on Web Video" width="450" height="368" class="alignleft size-full wp-image-2156" srcset="/wp-content/uploads/2011/01/975857_64336474.jpg 450w, /wp-content/uploads/2011/01/975857_64336474-300x245.jpg 300w" sizes="(max-width: 450px) 100vw, 450px" /></a><br />
It&#8217;s really full of video sharing websites out there, but in fact almost all of them use Flash player to display their video files. This is the reality now, but with the coming of HTML5, perhaps the things are changing a bit!</p>
<p>First of all if you start dealing with video sharing platforms the first thing to do maybe is to find a good Flash (.flv) player and to convert all your video content in FLV.</p>
<p>Few things to know with Flash video players:</p>
<ol>
<li>The user can play those videos only after installing the Flash Plugin to his browser;</li>
<li>The video must be encoded either in FLV (or FLash Video) or in MPEG-4 with h.264 codec. Only than the Flash player can play it;</li>
</ol>
<p>The HTML5, which can be described as variety of new &#8220;things&#8221; in the HTML comes with a native &lt;video&gt; tag. Something like the &lt;img&gt; tag where you can just point the source of the image in the src attribute.<span id="more-2145"></span></p>
<h2>First Question &#8211; How Does it Look Like?</h2>
<p>To get more fluent with the &lt;video&gt; tag you must simply thing of it as a &#8220;complex&#8221; &lt;img&gt; tag. You just point to the video and the browser will do the rest. To be more exact let me send you to the <a title="HTML5 Video" href="http://www.w3schools.com/html5/html5_video.asp" target="_blank">w3schools page</a>, but the important thing as they say:</p>
<blockquote><p>Until now, there has never been a standard for showing video on a web  page.</p></blockquote>
<p>However to start from somewhere here&#8217;s a little snippet showing you the basic, but very basic usage of the HTML5 video tag:</p>
<pre lang="html4stict" escaped="true">
&lt;video src="path_to_the_video" controls="controls"&gt;&lt;/video&gt;
</pre>
<h2>Second Question &#8211; Is Every Video Playable?</h2>
<p>But of course not! There are different browser &#8211; different video formats. Currently there are three formats, supported by the different browsers.</p>
<ol>
<li>H.264</li>
<li>VP8</li>
<li>Theora</li>
</ol>
<p>Note that these are <span style="text-decoration: underline;">video</span> codecs and a video files does not consists of only a video codec, but also of an audio codec. This makes the video file a container of several things. As you may know already H.264 is used with MPEG-4 video format, which results in .mp4 extended files, Theora is used in OGG, which are all those .ogg files out in the web, and finaly VP8 is used in WebM &#8230; or in .webm videos.</p>
<p>First thing to know FLV files (.flv) can also be encoded with H.264. Actually Flash player has a h.264 decoder so this makes it possible to play both .mp4 and .flv files encoded with H.264.</p>
<p>Second thing to notice &#8211; where are those .webm files? I&#8217;ve never seen them!</p>
<h2>Third Question &#8211; What is WebM and VP8?</h2>
<p>Behind <a title="The WebM Project" href="http://www.webmproject.org/" target="_blank">WebM</a> and VP8 is Google. This is a new video format based again on Theora. Actually it pretends to be as good in compressing as H.264, which by the way is meant to be the best. But let me take a look at <a title="The state of the video on the web" href="http://net.tutsplus.com/articles/the-state-of-video-on-the-web/" target="_blank">Nettuts+</a>:</p>
<blockquote><p>VP8, a relatively new codec, is the brainchild of On2 — the same guys  behind Theora. Google acquired On2 in 2010, and opened up all the  underlying patents for the codec into the public domain.</p>
<p>WebM, the container of choice for most current browsers, utilizes VP8  for compressing its video content and Vorbis for its audio. It produces  content similar in quality to H.264.</p>
<p>It is completely royalty free, now and for the future. On the  downside, though, it has limited hardware decoding support as well as  third party device/mobile support.</p></blockquote>
<h2>Final Question &#8211; How Can I Convert a Video in WebM?</h2>
<p>This answer is given by <a title="FFMPEG and WebM" href="http://paulrouget.com/e/funwithwebm" target="_blank">this article</a>, where as it is said after having the ffmpeg version &gt;= 0.6 you simply can use this command:</p>
<pre lang="bash">ffmpeg -i girltalk.mp4 -f webm -vcodec libvpx -acodec libvorbis -aq 90 -ac 2 girltalk.webm
</pre>
<h2>Conclusion</h2>
<p>It&#8217;s interesting to see what will happen one day. There is going to be either a harmonious life in the web video field or the codecs/formats will be as much as the browser? Who knows?</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/03/05/video-sites-must-use-mp4-and-only-mp4/" rel="bookmark" title="Video sites must use &#8230; mp4 and only mp4!">Video sites must use &#8230; mp4 and only mp4! </a></li>
<li><a href="/2010/03/04/does-firefox-play-mp4-h-264-within-the-html5-video-tag/" rel="bookmark" title="Does Firefox play mp4 h.264 within the HTML5 video tag?">Does Firefox play mp4 h.264 within the HTML5 video tag? </a></li>
<li><a href="/2010/11/12/how-to-make-mp4-progressive-with-qt-faststart/" rel="bookmark" title="How to Make MP4 Progressive with qt-faststart">How to Make MP4 Progressive with qt-faststart </a></li>
<li><a href="/2011/02/04/reading-gps-latitude-and-longitude-from-image-and-video-files/" rel="bookmark" title="Reading GPS Latitude and Longitude from Image and Video Files">Reading GPS Latitude and Longitude from Image and Video Files </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2011/01/27/few-thoughts-on-web-video/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Overcome Zend_Cache_Frontend_Page&#8217;s Problem with Cookies</title>
		<link>/2010/11/03/how-to-overcome-zend_cache_frontend_pages-problem-with-cookies/</link>
		<comments>/2010/11/03/how-to-overcome-zend_cache_frontend_pages-problem-with-cookies/#comments</comments>
		<pubDate>Wed, 03 Nov 2010 14:19:25 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Apache Corporation]]></category>
		<category><![CDATA[Cache]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[Computer memory]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[CPU cache]]></category>
		<category><![CDATA[Cross-platform software]]></category>
		<category><![CDATA[database server]]></category>
		<category><![CDATA[Google Inc.]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[HTTP cookie]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[MySQL Americas Inc.]]></category>
		<category><![CDATA[Page cache]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[script interpreter]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[web app]]></category>
		<category><![CDATA[web application caching]]></category>
		<category><![CDATA[Web application frameworks]]></category>
		<category><![CDATA[web server]]></category>
		<category><![CDATA[Zend Technologies]]></category>

		<guid isPermaLink="false">/?p=2024</guid>
		<description><![CDATA[Zend_Cache_Frontend_Page First of all there are several things to know about Zend Framework and caching. Whenever you work on a big web application caching is one of the mostly used mechanisms of speeding up the app and improve user performance. In general the task and the solution are pretty simple and natural. As the application &#8230; <a href="/2010/11/03/how-to-overcome-zend_cache_frontend_pages-problem-with-cookies/" class="more-link">Continue reading <span class="screen-reader-text">How to Overcome Zend_Cache_Frontend_Page&#8217;s Problem with Cookies</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2011/01/17/a-memcached-zend_cache/" rel="bookmark" title="A Memcached Zend_Cache">A Memcached Zend_Cache </a></li>
<li><a href="/2010/01/27/theory-of-caching-zend_cache-zend-optimizer/" rel="bookmark" title="Theory of caching. Zend_Cache &#038; Zend Optimizer.">Theory of caching. Zend_Cache &#038; Zend Optimizer. </a></li>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2010/07/19/zend-framework-cache-database-table-schemes/" rel="bookmark" title="Zend Framework: Cache Database Table Schemes">Zend Framework: Cache Database Table Schemes </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Zend_Cache_Frontend_Page</h2>
<p>First of all there are several things to know about Zend Framework and caching. Whenever you work on a big web application caching is one of the mostly used mechanisms of speeding up the app and improve user performance. In general the task and the solution are pretty simple and natural.</p>
<p>As the application grows up the visitors become more and more impatient about what they receive. A single page is becoming slower and slower and the result is painful. First of all every time a user hits a page the application server uses the web server, a script interpreter, a database server and potentially the file system. But that&#8217;s not all. After all this output is generated on the server, as HTML in the most cases, it is sent to the client where again CSS and JavaScript engines parse and execute them.</p>
<p>In this scenario it&#8217;s easy to imagine how many time is spent. While there are several techniques to optimize the client side by optimizing JavaScript, CSS and the static images used for the design of the site, here I&#8217;m going to talk more about the backend.</p>
<h2>Beside the Optimization</h2>
<p>Let&#8217;s assume we&#8217;ve one of the very used combination between Apache (as a webserver), PHP (as server scripting language) and MySQL (as database server). Here you can choose to optimize all three of them. However beside the optimization of them one of the most simple steps you can do is to cache the output generated by these three branches of your web app.</p>
<h2>Caching the Content</h2>
<p>In fact you can cache only single parts of the whole process. For instance you can cache only the result returned by some slow database query. Let&#8217;s imagine a query takes about 2 seconds to execute. Now you can cache the result into a file and during the cache is active, i.e. it has not expired, the application takes it from a file stored somewhere in the file system.</p>
<p>In a typical Zend Framework scenario you can first setup the frontend and backend options of the cache.</p>
<pre lang="php">
$frontendOptions = array(
	'lifetime'                => 600, // in seconds - this is 10 seconds
	'automatic_serialization' => true,
);
$backendOptions = array('cache_dir' => 'cache/');
$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
		
$cacheKey = md5('mykey');

if (!$cache->load($cacheKey)) {
	$slowQueryResult = $article->fetchAll();
	$cache->save($slowQueryResult, $cacheKey);
} else {
	$slowQueryResult = $cache->load($cacheKey);
}
</pre>
<p>You can setup different options here by setting up the cache directory, lifetime, etc.</p>
<p><em>Note that the cache directory must exists and with write permissions and Zend Framework doesn&#8217;t create it for you and will throw error.</em></p>
<p>The problem here is that now you cache only part of the generated content and in many cases this is still too slow for most of the users. However all the work (in most of the cases) of the web server, the script interpreter and the database server result in a simple HTML output. What if you have this output generated or cached for you and when the user hit the page the server will return this pre-generated code?</p>
<p>This is indeed very fast, because it&#8217;s similar to return a text file, as the HTML is simply formatted text.</p>
<h2>Caching the Entire Page</h2>
<p>Before I proceed, I&#8217;d like to say that I work with Zend Framework 1.9.x. Now in the latest versions of ZF there are new mechanisms of caching the output even Zend_Cache_Frontend_Page works fine on them.</p>
<p>You can simply setup the page cache within few simple lines of code:</p>
<pre lang="php">
$fo = array(
    'lifetime' => 600,
    'regexps' => array(
        '^/' => array(
	'cache' => true,
         'cache_with_cookie_variables' => true,
        ),
    )
);

$bo = array(
    'cache_dir' => 'cache/'
);

$cache = Zend_Cache::factory('Page', 'File', $fo, $bo);
$cache->start();
</pre>
<p>However my advise is to place this code as high as possible, because this will cache everything generated as output. It&#8217;s a good practice if you place this even in the bootstrap before you make the connection with the database. Actually you don&#8217;t need a database connection when you&#8217;ve to return a simple text(html) file.</p>
<p>This will improve your app&#8217;s performance a lot!</p>
<p>However there are few things to know. When you setup the cache to work even with cookie variables, you can see that hitting the page with different browsers Zend Framework will generated different cache pages. This is quite useless because than you don&#8217;t have any benefit of caching the content.</p>
<p>First of all let me say that THIS IS NOT A BUG! of ZF. Simply the framework will use the cookie variables to generate the cache key. It&#8217;s obvious that different browsers, even more different users, will have different cookie set and the framework will generate different cache keys for them.</p>
<p>Thus you&#8217;ve to change the setting to generate the cache key from cookie variable by explicitly set this option to false:</p>
<pre lang="php">
$fo = array(
    'lifetime' => 600,
    'regexps' => array(
        '^/' => array(
		 'cache' => true,
         'cache_with_cookie_variables' => true,
         'make_id_with_cookie_variables' => false,
        ),
    )
);

$bo = array(
    'cache_dir' => 'cache/'
);

$cache = Zend_Cache::factory('Page', 'File', $fo, $bo);
$cache->start();
</pre>
<p>Note that in this example we cache every single page generated by the framework explained in the regexps. This is not so good especially when the users have the possibility to login and to see customized content for them, so you can be careful what you cache.</p>
<p>A typical problem that this solution solves is when your application uses Google Analytics. As you may know Google Analytics sets up a cookie every time when an user hits the page, so every time the framework will generate a different cache for him and in result he won&#8217;t see any benefit and performance improvement from your site.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2011/01/17/a-memcached-zend_cache/" rel="bookmark" title="A Memcached Zend_Cache">A Memcached Zend_Cache </a></li>
<li><a href="/2010/01/27/theory-of-caching-zend_cache-zend-optimizer/" rel="bookmark" title="Theory of caching. Zend_Cache &#038; Zend Optimizer.">Theory of caching. Zend_Cache &#038; Zend Optimizer. </a></li>
<li><a href="/2010/07/21/setting-up-global-cache-in-zend-framework/" rel="bookmark" title="Setting Up Global Cache in Zend Framework">Setting Up Global Cache in Zend Framework </a></li>
<li><a href="/2010/07/19/zend-framework-cache-database-table-schemes/" rel="bookmark" title="Zend Framework: Cache Database Table Schemes">Zend Framework: Cache Database Table Schemes </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/11/03/how-to-overcome-zend_cache_frontend_pages-problem-with-cookies/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>2xjQuery: Select a Selector</title>
		<link>/2010/09/24/2xjquery-select-a-selector/</link>
		<comments>/2010/09/24/2xjquery-select-a-selector/#comments</comments>
		<pubDate>Fri, 24 Sep 2010 08:23:58 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[Comparison of JavaScript frameworks]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[Ext]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jquery javascript library]]></category>
		<category><![CDATA[jquery selectors]]></category>
		<category><![CDATA[Markup languages]]></category>
		<category><![CDATA[openlayers]]></category>
		<category><![CDATA[openlayers library]]></category>
		<category><![CDATA[Span and div]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[World Wide Web]]></category>

		<guid isPermaLink="false">/?p=1989</guid>
		<description><![CDATA[Selectors in jQuery If you&#8217;re familiar with jQuery you should already know what are selectors and how they work. However we&#8217;re used to get some DOM element with a specific selector, but actually sometimes you cannot select directly what you need. This is especially true when mixing more than one JavaScript libraries. In my case &#8230; <a href="/2010/09/24/2xjquery-select-a-selector/" class="more-link">Continue reading <span class="screen-reader-text">2xjQuery: Select a Selector</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/01/08/jquery-vs-pure-javascript/" rel="bookmark" title="jQuery vs. pure JavaScript">jQuery vs. pure JavaScript </a></li>
<li><a href="/2009/07/29/cancel-bubbling-on-element-click-with-jquery/" rel="bookmark" title="cancel bubbling on element click with jQuery">cancel bubbling on element click with jQuery </a></li>
<li><a href="/2009/11/02/ajax-in-jquery/" rel="bookmark" title="$.ajax in jQuery">$.ajax in jQuery </a></li>
<li><a href="/2009/04/01/jquery-accessing-a-child-element/" rel="bookmark" title="jQuery &#8211; accessing a child element">jQuery &#8211; accessing a child element </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Selectors in jQuery</h2>
<p><a href="/wp-content/uploads/2010/09/jquery.png"><img class="alignleft size-full wp-image-1999" title="jquery" src="/wp-content/uploads/2010/09/jquery.png" alt="jQuery JavaScript Library" width="259" height="65" /></a></p>
<p>If you&#8217;re familiar with <a title="jQuery" href="http://jquery.com/" target="_blank">jQuery</a> you should already know what are selectors and how they work. However we&#8217;re used to get some DOM element with a specific selector, but actually sometimes you cannot select directly what you need. This is especially true when mixing more than one JavaScript libraries. In my case <a title="OpenLayers" href="http://openlayers.org/" target="_blank">OpenLayers</a> generated a vector layer and jQuery was supposed to change the vectors fill-color property.</p>
<p><a href="/wp-content/uploads/2010/09/openlayers.png"><img class="alignleft size-full wp-image-2000" title="openlayers" src="/wp-content/uploads/2010/09/openlayers.png" alt="OpenLayers" width="195" height="37" /></a></p>
<p>With the simple $(&#8216;path&#8217;) I easily got all vectors in the current document, but the problem was that I took them as text. Thus it came to me to use nested jQuery &#8211; $($(&#8216;selector&#8217;));</p>
<p>To describe what actually happened in my case, let me show you a breve example.</p>
<h2>Example</h2>
<p>Here&#8217;s a short HTML markup:</p>
<pre lang="html4strict">
<div><span>text text</span></div>

</pre>
<p>With jQuery I can select both the DIV and SPAN with the simple $(&#8216;div&#8217;) and $(&#8216;span&#8217;), but just for the example lets assume I&#8217;ve only the inner text of the DIV tag:</p>
<pre lang="javascript">$('div').html();
</pre>
<p>That was the case in the OpenLayers/jQuery problem. Now I can easily use another jQuery selector:</p>
<pre lang="javascript">$($('div').html());
</pre>
<p>This will return the same as $(&#8216;span&#8217;) &#8211; a jQuery object.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/01/08/jquery-vs-pure-javascript/" rel="bookmark" title="jQuery vs. pure JavaScript">jQuery vs. pure JavaScript </a></li>
<li><a href="/2009/07/29/cancel-bubbling-on-element-click-with-jquery/" rel="bookmark" title="cancel bubbling on element click with jQuery">cancel bubbling on element click with jQuery </a></li>
<li><a href="/2009/11/02/ajax-in-jquery/" rel="bookmark" title="$.ajax in jQuery">$.ajax in jQuery </a></li>
<li><a href="/2009/04/01/jquery-accessing-a-child-element/" rel="bookmark" title="jQuery &#8211; accessing a child element">jQuery &#8211; accessing a child element </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/09/24/2xjquery-select-a-selector/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
