<?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>Codigo Manso &#187; PHP</title>
	<atom:link href="http://www.codigomanso.com/en/category/programacion/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codigomanso.com</link>
	<description>Programación, informática y tecnología</description>
	<lastBuildDate>Sun, 21 Aug 2011 10:54:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Manso Trick: Detect the Operating System in PHP</title>
		<link>http://www.codigomanso.com/en/2010/04/truco-manso-detectar-el-sistema-operativo-en-php/</link>
		<comments>http://www.codigomanso.com/en/2010/04/truco-manso-detectar-el-sistema-operativo-en-php/#comments</comments>
		<pubDate>Thu, 08 Apr 2010 09:36:39 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programacion]]></category>
		<category><![CDATA[truco manso]]></category>
		<category><![CDATA[sistema operativo]]></category>
		<category><![CDATA[so]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=779</guid>
		<description><![CDATA[There could be a lot of reasons to detect the OS in PHP, and I&#8217;m pretty aware that these reasons could open a discussion by themselves. Anyway, it could be useful to do some tweaks or optimizations, to run one commands or anothers, or simply  it can be useful as additional information for webmasters, developers [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">There could be a lot of reasons to detect the OS in PHP, and I&#8217;m pretty aware that these reasons could open a discussion by themselves. Anyway, it could be useful to do some tweaks or optimizations, to run one commands or anothers, or simply  it can be useful as additional information for webmasters, developers or operations department.</p>
<p style="text-align: justify;">Anyway, we are all grown ups, so you know what you are doing. Now, imagine you need to know this. There are several ways (the more creative you are, the more ways you will find). The first way, and most simple and complete, is by using the <a href=" http://php.net/manual/en/function.php-uname.php" target="_blank"><em>php_uname</em></a> function. The php_uname is similar to the unix command uname, and with this function we can know the  operating system, the version of the OS, the release number, the host name, and the hardware architecture  (i386, amd64, &#8230;). It has all that you need <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p style="text-align: justify;">On the other hand, if you don&#8217;t want to use this pretty command, you can directly use the PHP_OS constant, which only includes the operating system name, but in most cases will be enough (it is equivalent to call <em><strong>php_uname(&#8217;s&#8217;)</strong></em>).</p>
<p style="text-align: justify;">Now you can try these two examples:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">php_uname</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #009900; font-weight: bold;">PHP_OS</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p style="text-align: justify;">The funny thing is that probably you would never need to know the OS, so you can skip the whole post <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2010/04/truco-manso-detectar-el-sistema-operativo-en-php/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>TCPDF: excelent library for generating PDF in PHP</title>
		<link>http://www.codigomanso.com/en/2009/12/tcpdf-excelente-libreria-para-generar-pdfs-en-php/</link>
		<comments>http://www.codigomanso.com/en/2009/12/tcpdf-excelente-libreria-para-generar-pdfs-en-php/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 09:10:55 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[php-pdf]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=797</guid>
		<description><![CDATA[The other day I was looking how to create PDF files in PHP.
The first thing that came to my mind was the PDFlib extension included in PHP since version 4. The problem with this library is that  I thought it was kind of low-level, and I didn&#8217;t had any idea on where to start.
In fact, [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">The other day I was looking how to create PDF files in PHP.</p>
<p style="text-align: justify;">The first thing that came to my mind was the <a href="http://php.net/manual/en/book.pdf.php">PDFlib extension</a> included in PHP since version 4. The problem with this library is that  I thought it was kind of low-level, and I didn&#8217;t had any idea on where to start.</p>
<p style="text-align: justify;">In fact, <a href="http://www.php.net/manual/en/ref.pdf.php">the list of functions of PDFlib</a> is quite big, and probably you can do about anything, but I did not want to be writing a hundred of lines to do a simple PDF, I just wanted to put a background image and write some lines in there.</p>
<p style="text-align: justify;">Anyway, I decided to continue looking more information about this library and I end up finding a library called <a href="http://www.tcpdf.org">TCPDF</a>, which is totally free, and seemed a lot more complete and simple. I really don&#8217;t know if it&#8217;s better or not than the PDFlib, but at least <a href="http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf_examples">TCPDF has lots of examples.</a> There are super-simple examples of about any aspect of the PDF, so doing your first PDF is a matter of copy-pasting from several examples. Really easy!</p>
<p style="text-align: justify;">The truth is, that I did my FIRST prototype in about 15 minutes. Not bad at all. With this TCPDF library you can generate the PDF as if it was like a HTML file. You can pass the text using HTML tags (even CSS), and then the library internally converts that to the PDF, so you don&#8217;t have to think much about generating the PDF.</p>
<p style="text-align: justify;">The only &#8220;problem&#8221; I see with this library is that the size of the zip file you have to download is around 10MB, which are around 16 MB once uncompressed. Anyway, almost all the size of the library comes from several fonts it includes, documentation and examples. If you remove the documentation and you keep only those fonts you are going to use, this library could be minified to 1 MB only.  Although for web development I think it does not worth to make it smaller. The size does not matter.</p>
<p style="text-align: justify;">For more information about this library you can take a look at <a href="http://www.tcpdf.org">www.tcpdf.org</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2009/12/tcpdf-excelente-libreria-para-generar-pdfs-en-php/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Emulate short tags in PHP when short_open_tag = off</title>
		<link>http://www.codigomanso.com/en/2009/11/emulate-short-tags-php-when-short_open_tag-is-disabled/</link>
		<comments>http://www.codigomanso.com/en/2009/11/emulate-short-tags-php-when-short_open_tag-is-disabled/#comments</comments>
		<pubDate>Sat, 21 Nov 2009 08:00:44 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programacion]]></category>
		<category><![CDATA[emulate short tags]]></category>
		<category><![CDATA[php parser]]></category>
		<category><![CDATA[php tokenizer]]></category>
		<category><![CDATA[short tags]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=781</guid>
		<description><![CDATA[Yesterday I was playing with PHP 5.3. It seems that the default configuration comes with short_open_tag disabled  
The short open tags is what allows to write code like this &#60;?=&#8221;algo&#8221;?&#62;  instead of having to write &#60;?php echo &#8220;algo&#8221;?&#62;. Something REALLY INTERESTING, it makes PHP code really clear when looking at templates from my [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Yesterday I was playing with PHP 5.3. It seems that the default configuration comes with <strong>short_open_tag</strong> disabled <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p style="text-align: justify;">The short open tags is what allows to write code like this &lt;?=&#8221;algo&#8221;?&gt;  instead of having to write &lt;?php echo &#8220;algo&#8221;?&gt;. Something REALLY INTERESTING, it makes PHP code really clear when looking at templates from my point of view.</p>
<p style="text-align: justify;">I think PHP people has not made the right choice here. From what I understood, they disabled this because of the ambiguity when parsing XML documents, which starts by &lt;?xml. I&#8217;ll better stop talking.</p>
<p style="text-align: justify;">Anyway, everything is because I have a class called <strong>jview</strong> which loads the PHP view templates in my system, and lots of the templates (if not all) use this tags, so I wanted to keep writing the code like that without changing the short tags in the template files. You can think on WordPress templates to get an idea of how my templates look like.</p>
<p style="text-align: justify;">Just to make an example of the whole problem, have a look at the following PHP code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;hello&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span> <span style="color: #000000; font-weight: bold;">&lt;?=</span> <span style="color: #0000ff;">&quot;world&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p style="text-align: justify;">When short_open_tags is enabled, the PHP output is:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">hello world</pre></div></div>

<p style="text-align: justify;">But when short_open_tags is disabled, then PHP produces this ugly output:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">hello &lt;?= &quot;world&quot;?&gt;</pre></div></div>

<p style="text-align: justify;">BTW, I had two alternatives for solving this problem:</p>
<ul style="text-align: justify;">
<li>Copy the template file into a temporal file, replace all &#8216;&lt;?=&#8217; by &#8216;&lt;?php &#8216;, include the temporal file as a PHP file, and finally remove the file</li>
<li>Make a PHP interpreter in PHP where I replace &#8216;&lt;?=&#8217; by &#8216;&lt;?php &#8216;</li>
</ul>
<p style="text-align: justify;">If you know me, you know I&#8217;ve chosen the second option ;D</p>
<p style="text-align: justify;">It only took me 20 minutes!! Less than I expected!</p>
<p style="text-align: justify;">Here is the code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  function include_emulating_short_tags ($viewFile)
  {
    // Alternate method for making <span style="color: #000000; font-weight: bold;">&lt;?=</span> valid
    <span style="color: #000088;">$raw</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$viewFile</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$raw</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;?='</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;?php echo '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$raw</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$phptokens</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">token_get_all</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$raw</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$mode</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">'text'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$phpcode</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$phptokens</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$ptoken</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mode</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'text'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">is_array</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ptoken</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ptoken</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> T_OPEN_TAG<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$mode</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'php'</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mode</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'php'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">is_array</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ptoken</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ptoken</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> T_CLOSE_TAG<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">ob_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">eval</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$phpcode</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">';'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$html</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">ob_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">ob_end_clean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$mode</span>    <span style="color: #339933;">=</span> <span style="color: #0000ff;">'text'</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$phpcode</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$mode</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'text'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
        <span style="color: #000088;">$html</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">is_array</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ptoken</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$ptoken</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$ptoken</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$phpcode</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">is_array</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ptoken</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$ptoken</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$ptoken</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mode</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'php'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$phpcode</span> <span style="color: #339933;">!==</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #990000;">ob_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #990000;">eval</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$phpcode</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$html</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">ob_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #990000;">ob_end_clean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$html</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p style="text-align: justify;">At the end, the variable <b>$html</b> returned, contains the HTML code that will otherwise be flushed to standard output. This way, you can do with the output whatever you want.</p>
<p style="text-align: justify;">Finally, if we use include_emulating_short_tags when short_open_tags=0 with the original example, the output will be &#8220;hello world&#8221;, what I wanted <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> . I can see a couple of inconveniences (that could be work-arounded by writing something more elaborate), but for practical uses, it works perfectly well.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2009/11/emulate-short-tags-php-when-short_open_tag-is-disabled/feed/</wfw:commentRss>
		<slash:comments>323</slash:comments>
		</item>
		<item>
		<title>Multilingual support for WordPress: improved!</title>
		<link>http://www.codigomanso.com/en/2009/10/improved-multi-lingual-support-for-wordpress/</link>
		<comments>http://www.codigomanso.com/en/2009/10/improved-multi-lingual-support-for-wordpress/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 09:03:15 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programacion]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[zdmultilang]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=738</guid>
		<description><![CDATA[I&#8217;ve been using wordpress ZdMultilang plugin to  write articles in several languages.
The truth is that the plugin is awesome. It works great! The only thing I missed was the lack of support for keeping separate comments, so the users reading the post in English only see the comments that the English-readers version made, and so [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">I&#8217;ve been using wordpress <a href="http://blog.zen-dreams.com/en/wordpress/zdmultilang/" target="_blank">ZdMultilang</a> plugin to  <a href="http://www.codigomanso.com/en/2008/12/wordpress-27-y-soporte-multi-idioma/" target="_self">write articles in several languages</a>.</p>
<p style="text-align: justify;">The truth is that the plugin is <em><strong>awesome</strong></em>. It works great! The only thing I missed was the lack of support for keeping separate comments, so the users reading the post in English only see the comments that the English-readers version made, and so for the Spanish readers/commenters. To me it seemed logic to keep separate comments since an English person will probably not understand Spanish, and he could get discouraged to leave a comment if he sees that all the comments are in Spanish. The opposite case is also true.</p>
<p style="text-align: justify;">Some time ago <a href="http://blog.zen-dreams.com/en/wordpress/zdmultilang/#comment-2488" target="_blank"> I asked Anthony Petitbois</a>, the original writer of the plugin (awesome work), if he could make a couple of improvements to the plugin.</p>
<p style="text-align: justify;">This request was around 10 months ago, and I wanted this functionality of keeping separate comments for a new blog, so I though:</p>
<p style="padding-left: 30px; text-align: justify;">&#8220;Hey! I never did a WordPress  plugin, the ZdMultilang is Open Source&#8230; it should not be that hard! Let&#8217;s contribute to the project!&#8221;</p>
<p style="text-align: justify;">Around 4 hours and a half later, the plugin was completely written and this blog was updated. After several tests it seems that everything works like a charm! Then, hopefully it should work perfectly for any version of WordPress &gt;= 2.8.x</p>
<p style="text-align: justify;">What improvements are included?</p>
<ul>
<li><strong>zd_multilang_get_locale():</strong> it&#8217;s a function to get the current language(Inglés, Español, &#8230;), before that, you had to use the global $locale</li>
<li>The ability to <strong>keep separate comments</strong> for each language</li>
<li>The ability to <strong>keep all comments in the same thread</strong>, to behave as the plugin was already behaving</li>
</ul>
<p style="text-align: justify;">Please note that ALL existing comments, prior to the installation and activation of this plugin improvement, will be shown for ALL languages. From my point of view it makes sense to show all comments since I have no idea of which language where those comments written.</p>
<p style="text-align: justify;">Please note as well that I added a new option to the translation preferences, so the user (you) can control whether you want to enable this functionality or you want the blog to behave as always (with all the comments for all the languages).  Anyway, whatever option you choose, ALL comments will be visible from the administration panel, so you could<strong> easily </strong>administer comments in ALL languages <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p style="text-align: justify;">The only additional thing you will have to do, to get this new functionality working, is to update the comments.php file in the current theme you are using, and change the form for posting comments:</p>
<p style="text-align: justify; padding-left: 30px;">From this:</p>
<p style="text-align: justify; padding-left: 30px;"><em>action=&#8221;&lt;?php echo get_option(&#8217;siteurl&#8217;); ?&gt;/wp-comments-post.php&#8221;</em></p>
<p style="padding-left: 30px;">to this:</p>
<p style="text-align: justify; padding-left: 30px;"><em>action=&#8221;&lt;?php echo get_option(&#8217;siteurl&#8217;); ?&gt;/wp-comments-post.php<strong>?lang=&lt;?=zd_multilang_get_locale()?&gt;</strong>&#8220;</em></p>
<p style="text-align: justify;">In the following link you could download a TGZ that contains the DIFF of the zdmultilang.php changes, the zdmultilang.php with the changes, and the readme.txt file:</p>
<ul>
<li><a href="http://www.codigomanso.com/archives/zd_multilang-1.2.5-keep_separate_comments.tgz" target="_self"><strong>Download the improved ZdMultilang that keeps separate comments</strong></a></li>
</ul>
<p style="text-align: justify;">Finally, I updated the version to 1.2.5, although this can produce &#8220;future&#8221; problems, unless I convice Anthony to include these improvements on the next version of the plugin <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p style="text-align: justify;">
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2009/10/improved-multi-lingual-support-for-wordpress/feed/</wfw:commentRss>
		<slash:comments>1866</slash:comments>
		</item>
		<item>
		<title>Detecting an animated GIF in PHP</title>
		<link>http://www.codigomanso.com/en/2009/06/detect-an-animated-gif-in-php/</link>
		<comments>http://www.codigomanso.com/en/2009/06/detect-an-animated-gif-in-php/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 19:50:33 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programacion]]></category>
		<category><![CDATA[animado]]></category>
		<category><![CDATA[gif]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=679</guid>
		<description><![CDATA[Following function detects if the GIF contains an animation (it has more than 1 frame) or is just a static file.

  function isAnimatedGif&#40;$filename&#41;
  &#123;
    $filecontents=file_get_contents&#40;$filename&#41;;
&#160;
    $str_loc=0;
    $count=0;
&#160;
    // There is no point in continuing after we find a 2nd frame
  [...]]]></description>
			<content:encoded><![CDATA[<p>Following function detects if the GIF contains an animation (it has more than 1 frame) or is just a static file.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">function</span> isAnimatedGif<span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$filecontents</span><span style="color: #339933;">=</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$str_loc</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$count</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// There is no point in continuing after we find a 2nd frame</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$where1</span><span style="color: #339933;">=</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filecontents</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\x00</span><span style="color: #660099; font-weight: bold;">\x21</span><span style="color: #660099; font-weight: bold;">\xF9</span><span style="color: #660099; font-weight: bold;">\x04</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$str_loc</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$where1</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #000088;">$str_loc</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$where1</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$where2</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filecontents</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\x00</span><span style="color: #660099; font-weight: bold;">\x2C</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$str_loc</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$where2</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$where1</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">8</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$where2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$count</span><span style="color: #339933;">++;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$str_loc</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$where2</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// gif is animated when it has two or more frames</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Following function does the same but in a more compact way:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> isAnimatedGif<span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#(\x00\x21\xF9\x04.{4}\x00\x2C.*){2,}#s'</span><span style="color: #339933;">,</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I have modified slightly the first function, but both of them come originally from the comments at <a href="http://es2.php.net/manual/en/function.imagecreatefromgif.php">http://es2.php.net/manual/en/function.imagecreatefromgif.php</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2009/06/detect-an-animated-gif-in-php/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Convert UTF-8 string into an array of chars in PHP</title>
		<link>http://www.codigomanso.com/en/2009/02/convert-utf8-string-to-array-in-php/</link>
		<comments>http://www.codigomanso.com/en/2009/02/convert-utf8-string-to-array-in-php/#comments</comments>
		<pubDate>Sun, 22 Feb 2009 10:19:10 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programacion]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[internacionalización]]></category>
		<category><![CDATA[utf-8]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=578</guid>
		<description><![CDATA[I hope PHP 6 will solve all my problems with UTF-8, but it has not been released yet, and one has to keep coding with the tools it has available.
I think I will probably be sharing the class for string manipulation that I&#8217;ve been coding (which of course is 100% compatible with UTF-8).
For now, I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">I hope PHP 6 will solve all my problems with UTF-8, but it has not been released yet, and one has to keep coding with the tools it has available.</p>
<p style="text-align: justify;">I think I will probably be sharing the class for string manipulation that I&#8217;ve been coding (which of course is 100% compatible with UTF-8).</p>
<p style="text-align: justify;">For now, I&#8217;m going to share a simple function to split a UTF-8 string into an array of strings. This is not something you want to do all  over your code, because arrays consume a HUGE amount of memory in PHP, but sometimes it an be useful for specific purposes.</p>
<p style="text-align: justify;">Converting a UTF-8 string into an array, in any version before PHP 6 has two big advantages:</p>
<ul style="text-align: justify;">
<li>each element of the array represents a unique character (the same that when you do $string[$i])</li>
<li><strong>count</strong> would be equivalent to <strong>mb_strlen</strong> over the original string</li>
</ul>
<p style="text-align: justify;">The bad news are the time spent on conversion and the memory consumption (as you will be using an array instead of a native string).</p>
<p style="text-align: justify;">Yesterday I wrote a couple of functions, and <a href="http://uk3.php.net/manual/en/function.mb-split.php#80046" target="_blank">I borrowed this one</a> that a user had shared on PHP manuals</p>
<p style="text-align: justify;">These are the three versions in PHP:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> getCharArray1 <span style="color: #009900;">&#40;</span><span style="color: #000088;">$jstring</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$len</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mb_strlen</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$jstring</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mb_strlen</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$jstring</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$len</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$char</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mb_substr</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$jstring</span><span style="color: #339933;">,</span> <span style="color: #000088;">$i</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">array_push</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ret</span><span style="color: #339933;">,</span> <span style="color: #000088;">$char</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$ret</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// code from: http://uk3.php.net/manual/en/function.mb-split.php#80046</span>
<span style="color: #000000; font-weight: bold;">function</span> getCharArray2 <span style="color: #009900;">&#40;</span><span style="color: #000088;">$jstring</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$len</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mb_strlen</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$jstring</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mb_strlen</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$jstring</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$len</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$ret</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">mb_substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$jstring</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$jstring</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mb_substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$jstring</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #000088;">$len</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$len</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mb_strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$jstring</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$ret</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// using mb_check_encoding instead of mb_substr ;)</span>
<span style="color: #000000; font-weight: bold;">function</span> getCharArray3 <span style="color: #009900;">&#40;</span><span style="color: #000088;">$jstring</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mb_strlen</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$jstring</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$ret</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$alen</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$jstring</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$char</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$alen</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$char</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$jstring</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mb_check_encoding</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$char</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #990000;">array_push</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ret</span><span style="color: #339933;">,</span> <span style="color: #000088;">$char</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$char</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$ret</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p style="text-align: justify;">The fastest function is the latter, that uses a small trick I thought (and in fact worked great).</p>
<p style="text-align: justify;">The following chart represents the execution times of each function for a set of iterations over the functions on a huge string.
</p>
<p><script type="text/javascript" src="http://www.codigomanso.com/archives/open-flash-chart/swfobject.js"></script><br />
<script type="text/javascript">
  swfobject.embedSWF(
    "http://www.codigomanso.com/archives/open-flash-chart/open-flash-chart.swf",
    "performance_chart", 
    "512", "384",
    "9.0.0",
    "expressInstall.swf",
    {"data-file":"http://www.codigomanso.com/archives/phpscripts/bench_getCharArray.txt"}
  );
</script></p>
<div id="performance_chart"></div>
<p><strong> Leyenda:</strong></p>
<ul>
<li><strong style="color:red;">getCharArray1:</strong> in red</li>
<li><strong style="color:green;">getCharArray2:</strong> in green</li>
<li><strong style="color:blue;">getCharArray3:</strong> in blue</li>
</ul>
<p style="text-align: justify;">As you can see,  <b>getCharArray3</b> (blue) is the fastest (around 4x faster than getCharArray1, and 6x faster than getCharArray2).</p>
<p style="text-align: justify;">Finally, you can check the <a href="http://www.codigomanso.com/archives/phpscripts/bench_getCharArray.php.txt">benchmark script</a> I&#8217;ve used for generating the chart.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2009/02/convert-utf8-string-to-array-in-php/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Manso Trick: Updating the order of a row in a table</title>
		<link>http://www.codigomanso.com/en/2009/01/update-order-of-a-row-keeping-the-rest-untouched/</link>
		<comments>http://www.codigomanso.com/en/2009/01/update-order-of-a-row-keeping-the-rest-untouched/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 10:55:24 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programacion]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[fila]]></category>
		<category><![CDATA[orden]]></category>
		<category><![CDATA[tabla]]></category>
		<category><![CDATA[truco manso]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=506</guid>
		<description><![CDATA[This time, the article is not about javascript or PHP, but SQL. The idea of this article is to review different methods for updating the order of a row in a table (trying to touch only the row we want to change, keeping intact the rest of rows).
To start with a simple example, let&#8217;s say [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">This time, the article is not about javascript or PHP, but SQL. The idea of this article is to review different methods for updating the order of a row in a table (trying to touch only the row we want to change, keeping intact the rest of rows).</p>
<p style="text-align: justify;">To start with a simple example, let&#8217;s say I&#8217;m doing a web site related to the motor industry, and as any other good website, I use a database. In that database there are two tables: <strong>brand</strong> and <strong>model</strong>. In the table <strong>brand</strong> there is a row for each car brand (Toyota, Honda, Hyunday, Mercedes, Chrysler, BMW, Audi, Lexus,&#8230;), and in the table <strong>model</strong> there is one row for each car model. For example, some models for Audi will be A6 2.0 TDIe , A5 2.0 TDI, A4, TT &#8230;, and the list grows and grows for each brand.</p>
<p style="text-align: justify;">The think is, that in the front-end of my website I want to list the models of a brand in a specific order (the order I defined).</p>
<p style="text-align: justify;">You will think, well, that&#8217;s easy, it&#8217;s just adding a field called <strong>sort_order</strong> in the <strong>model</strong> table, or if you love to have the tables normalized, then you will create an extra table with two foreign keys to <strong>model</strong> and <strong>brand</strong>, and then a field called <strong>sort_order</strong>.</p>
<p style="text-align: justify;">It does not matter which solution you choose, the final concept is that there is a field name <b>sort_order</b> that is set manually on the backend, which defines the order used to list models in the frontend. Let&#8217;s see an example of these tables:</p>
<table style="float: left; width: auto;">
<tr>
<th>Brand</td>
</tr>
<tr>
<td>name</td>
</tr>
<tr>
<td>description</td>
</tr>
<tr>
<td>image</td>
</tr>
<tr>
<td>&#8230;</td>
</tr>
</table>
<table style="float: left; margin-left: 2em;  width: auto;">
<tr>
<th>model</td>
</tr>
<tr>
<td>name</td>
</tr>
<tr>
<td>description</td>
</tr>
<tr>
<td>image</td>
</tr>
<tr>
<td>&#8230;</td>
</tr>
<tr>
<td><b>sort_order</b></td>
</tr>
<tr>
<td>&#8230;</td>
</tr>
</table>
<div style="clear:both;"></div>
<p style="text-align: justify;">This was just an informal introduction, let&#8217;s get started!</p>
<p style="text-align: justify;">In order to simplify things even more, imagine we are always talking about Audi (just to name one), and imagine we are working only with 4 models in the database: A3, A4, A5 y TT. From now on, we will forget about brands. So at the end, our table <b>model</b> looks like this:</p>
<table style="width: auto;">
<tr>
<th>name</th>
<th>description</th>
<th>&#8230;</th>
<th><b>sort_order</b></th>
<th>&#8230;</th>
</tr>
<tr>
<td>A3</td>
<td>Audi A3 is cool</td>
<td>&#8230;</td>
<td><b>1</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>A4</td>
<td>Audi A4 is cooler</td>
<td>&#8230;</td>
<td><b>2</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>A5</td>
<td>Audi A5 is even cooler</td>
<td>&#8230;</td>
<td><b>3</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>TT</td>
<td>No doubt this is the best</td>
<td>&#8230;</td>
<td><b>4</b></td>
<td>&#8230;</td>
</tr>
</table>
<p style="text-align: justify;">Now, when I do a SELECT in my website, and I sort using the <b>sort_order</b>, then the models will be listed this way: A3, A4, A5 and finally Audi TT.</p>
<h5>The problem:</h5>
<p style="text-align: justify;"><b>¿What do I do if I want Audi TT to be the first in the website?</b></p>
<p style="text-align: justify;">Please note that I want to move Audi TT to the first position, but I don&#8217;t want the A3, A4 and A5 change their respective orders; also note I said the first position, but I could have said I want TT to be placed between A3 and A4.</p>
<p style="text-align: justify;">
<h5>Solution 1: the easy onel</h5>
<p style="text-align: justify;">Well, so the first that comes to mind  (at least to me) is basically to increment all rows whose sort_order is greater or equal to the position where I want to place the element I want to move, and then update the element I want to move. Let&#8217;s see in SQL:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">UPDATE</span> model <span style="color: #993333; font-weight: bold;">SET</span> sort_order <span style="color: #66cc66;">=</span> sort_order <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">WHERE</span> sort_order <span style="color: #66cc66;">&gt;=</span> <span style="color: #cc66cc;">1</span>;
<span style="color: #993333; font-weight: bold;">UPDATE</span> model <span style="color: #993333; font-weight: bold;">SET</span> sort_order <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">WHERE</span> name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'TT'</span>;</pre></div></div>

<p style="text-align: justify;">Above you can see two queries: the first one has a linear complexity because it has to update every single row in the table whose sort order is greater than the position (but all rows should be checked); the other query is just to update the row of the TT.</p>
<p style="text-align: justify;">The problem with this solution is the part where ALL rows should be checked and lots of them updated. Then I though of another solution.</p>
<p style="text-align: justify;">
<h5>Solution 2: the magic of floats</h5>
<p style="text-align: justify;">An idea came to my mind: <b>¿What if instead of integers I use real numbers?</b></p>
<p style="text-align: justify;">If the field <b>sort_order</b> was a real number, then maybe I could somehow update just the row I want, and leave the rest untouched. Please keep in mind that I use an interface on the backend as well for listing the products and updating the order, so for all elements I know which is the sort_order for the previous and the next elements, as well as for the element itself.</p>
<p style="text-align: justify;">Now, imagine our initial configuration is (A3, A4, A5, TT), and we want to move the TT between A3 and A4. Then I only have to choose a sort_order value between A3 and A4, and magically the TT will be listed between both values. Let&#8217;s see the table after choose the  middle value:</p>
<table style="width: auto;">
<tr>
<th>name</th>
<th>description</th>
<th>&#8230;</th>
<th><b>sort_order</b></th>
<th>&#8230;</th>
</tr>
<tr>
<td>A3</td>
<td>Audi A3 is cool</td>
<td>&#8230;</td>
<td><b>1</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>TT</td>
<td>No doubt this is the best</td>
<td>&#8230;</td>
<td><b>1.5</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>A4</td>
<td>Audi A4 is cooler</td>
<td>&#8230;</td>
<td><b>2</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>A5</td>
<td>Audi A5 is even cooler</td>
<td>&#8230;</td>
<td><b>3</b></td>
<td>&#8230;</td>
</tr>
</table>
<p style="text-align: justify;">Basically I&#8217;ve done (1.0 + 2.0) / 2 to get the intermediate value between A3 and A4.</p>
<p style="text-align: justify;">Notice you can also do this as many times as you want, and not only the order will always be preserved, but you will <em>&#8220;never find&#8221;</em> two rows having the same sort_order.</p>
<p style="text-align: justify;">Let&#8217;s see another example, imagine now we want the A4 to be placed between A3 and TT, then the sort_order for A4 will be <b>(1.0 + 1.5)/2 = 1.25</b>.</p>
<table style="width: auto;">
<tr>
<th>name</th>
<th>description</th>
<th>&#8230;</th>
<th><b>sort_order</b></th>
<th>&#8230;</th>
</tr>
<tr>
<td>A3</td>
<td>Audi A3 is cool</td>
<td>&#8230;</td>
<td><b>1</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>A4</td>
<td>Audi A4 is cooler</td>
<td>&#8230;</td>
<td><b>1.25</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>TT</td>
<td>No doubt this is the best</td>
<td>&#8230;</td>
<td><b>1.5</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>A5</td>
<td>Audi A5 is even cooler</td>
<td>&#8230;</td>
<td><b>3</b></td>
<td>&#8230;</td>
</tr>
</table>
<p style="text-align: justify;">It&#8217;s easy, ¿isn&#8217;t it? And you just have to update a single row.</p>
<p style="text-align: justify;">Now there are lots of questions. This is so easy and cool, but <b>¿what happens if you want to place an element in the first position?</b> If we asume the sort_order starts at 1 for the first element, then the previous element should be choosen as 0.00 (and the same rules will work).</p>
<p style="text-align: justify;"><b>¿And what happens if you want to insert the A6 model?</b> As there are 4 elements, the sort_order for A6 will be 5.0 (to avoid colision, just use <b>number of rows + 1</b>).</p>
<p style="text-align: justify;">And the very good question is: &#8220;<i>floating point numbers had limitations, ¿didn&#8217;t they?</i> <b>¿what happens if there is an overflow?</b>&#8221; Yes man! this is the question!</p>
<p style="text-align: justify;">Making sort_order to overflow is as easy as keep moving all elements to the same position, over and over again. For example, if we move the element with sort order 3, between the elements with sort order 1 and 2, then the order will be 1.0, 1.5, 2.0, then if we move the 2.0 between 1 and 1.5, we will have 1.0, 1.25, 1.5, if we do repeat the last operation we will have 1.0, 1.125, 1.25, and if you repeat this several times you will get lots of decimals.</p>
<p style="text-align: justify;">This gets us to the third solution</p>
<h5>Solution 3: the workaround</h5>
<h6>Solution 3.1: the background process</h6>
<p style="text-align: justify;">One solution could be to have a process once a day,week or month to sort all elements and update the sort_order by their index once sorted, that will remove all decimals for all rows. But if you are going to do this, better implement the first solution.</p>
<h6>Solution 3.2: updating to integer when possible</h6>
<p style="text-align: justify;">This solution is the same as the solution 2, but rounding numbers to integers before updating, and checking that the number is between (but not equal) to the previous and the next position where we want to be placed.</p>
<p style="text-align: justify;">Let&#8217;s see an example starting with following set of rows:</p>
<table style="width: auto;">
<tr>
<th>name</th>
<th>description</th>
<th>&#8230;</th>
<th><b>sort_order</b></th>
<th>&#8230;</th>
</tr>
<tr>
<td>A3</td>
<td>Audi A3 is cool</td>
<td>&#8230;</td>
<td><b>1.125</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>TT</td>
<td>No doubt this is the best</td>
<td>&#8230;</td>
<td><b>1.0394</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>A4</td>
<td>Audi A4 is cooler</td>
<td>&#8230;</td>
<td><b>1.7628</b></td>
<td>&#8230;</td>
</tr>
<tr>
<td>A5</td>
<td>Audi A5 is even cooler</td>
<td>&#8230;</td>
<td><b>3.78</b></td>
<td>&#8230;</td>
</tr>
</table>
<p style="text-align: justify;">Then if I move A3, between A4 and A5, using solution two I will do the following: (1.7628 + 3.78)/2 = 2.7714. But, if I round 2.7714 down I get 2, and 2 is still between 1.7628 and 3.78, so instead of updating to 2.7714, it will be updated to 2. This way, if I move TT again between A3 and A5, this time instead of computing <b>(2.7714 + 3.78) / 2</b>, the TT position would be computed as <b>(2 + 3.78) / 2</b>, which contains less decimals.</p>
<p style="text-align: justify;">Just notice that in this solution one only has to check if the number is still between the two numbers around, which are already known, and keeps the performance high. Just an update, as before, and all the computations are made on the client-side</p>
<p style="text-align: justify;">If we assume that the movement of all elements will be almost random, using this way of computing number is less probable (but still possible) that an overflow happens.</p>
<p style="text-align: justify;">By the way, I presented this solution using floating point numbers, but you can use integers (and I don&#8217;t mean using 10, 20, 30, and then getting intermediate values).</p>
<h5>Conclussion</h5>
<p style="text-align: justify;">If you want a robust way, use the first solution; but if you want to update just an element each time, because there will be thousands of elements and thousand of users updating the sort order at the same time, then probably the second solution is better, although there can be problems when dealing with concurrent requests.
<p style="text-align: justify; font-weight: bold;">Finally although I&#8217;ve presented a couple of solutions (with their workarounds), I would be pleased to hear of another solutions or improvements.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2009/01/update-order-of-a-row-keeping-the-rest-untouched/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The PHP empty function is wrong!!</title>
		<link>http://www.codigomanso.com/en/2009/01/la-funcion-empty-de-php-esta-mal/</link>
		<comments>http://www.codigomanso.com/en/2009/01/la-funcion-empty-de-php-esta-mal/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 16:45:01 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programacion]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=440</guid>
		<description><![CDATA[After 20  minutes debugging code (BTW, what a great tool are debuggers), and you realize that the bug that manifests on A, does not come from A, not B, C or D, but it comes from far far away, something like Z you realize, as may other times, that a &#8220;bug&#8221; in a remote place [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">After 20  minutes debugging code (BTW, what a great tool are debuggers), and you realize that the bug that manifests on A, does not come from A, not B, C or D, but it comes from far far away, something like Z you realize, as may other times, that a &#8220;bug&#8221; in a remote place could have lots of side-efects.</p>
<p style="text-align: justify;">Anyway, after 20 minutes debugging I have discovered a but on a website in PHP I&#8217;m developing, and when I realiced it was because a conceptual problem in a PHP function I&#8217;ve was using, then I got angry (but it was my fault).</p>
<p style="text-align: justify;">The PHP function I&#8217;m talking about is <strong>empty</strong>, which <a href="http://uk3.php.net/manual/en/function.empty.php">as the manual states</a>, determines whether a variable is empty (the concept is quite easy, I don&#8217;t understand why is not well implemented).</p>
<p style="text-align: justify;">The problem was that I have a conditional saying<strong><em> if empty ($str) then do X, else do Y.</em></strong></p>
<p>The conceptual problems comes here:  <strong>empty(&#8220;0&#8243;) is true!! </strong></p>
<p style="text-align: center;"><strong> &lt;dramatism&gt;For God&#8217;s sake! What the hell was thinking the person who programed this?&lt;/dramatism&gt;</strong></p>
<p>Anyway, yesterday<a href="http://powazek.com/posts/1629" target="_blank"> I read this blog post about suggesting solutions and not only pointing out problems</a>, so let&#8217;s describe the problem, and let&#8217;s give a solution for all of this.</p>
<p>Let&#8217;s see the definition as it appears on <a href="http://uk3.php.net/manual/en/function.empty.php" target="_blank">the PHP manual</a>:</p>
<p class="refpurpose" style="padding-left: 30px;"><span class="refname">empty</span> — <span class="dc-title">Determine whether a variable is empty</span></p>
<p class="para" style="padding-left: 30px;">The following things are considered to be empty:</p>
<blockquote>
<ul class="simplelist" style="padding-left: 30px;">
<li class="member"><em>&#8220;&#8221; (an empty string)</em></li>
<li class="member"><em>0 (0 as an integer)</em></li>
<li class="member"><strong><em>&#8220;0&#8243; (0 as a string)</em></strong></li>
<li class="member"><tt>NULL</tt></li>
<li class="member"><tt>FALSE</tt></li>
<li class="member"><em>array() (an empty array)</em></li>
<li class="member"><em>var $var; (a variable declared, but without a value in a class)</em></li>
</ul>
</blockquote>
<p class="refpurpose" style="text-align: justify;"><span class="dc-title">I undestand, according to the definition, that a <strong>NULL</strong> variable is considered as empty, the same with the FALSE value, the same as an empty array <strong>array()</strong> , and the same  with the string <strong>&#8221;</strong> (which I think everyone understands why is empty). Even, I understand the number <strong>0</strong> is considered empty. Up to this point everything is consistend with the definition, <strong></strong><strong>it makes sense that each data type </strong>(boolean, string, array, reference) <strong>has a unique value considered as empty.</strong></span></p>
<p class="refpurpose" style="text-align: justify;"><span class="dc-title">What it does not make sense is the strings having two values considered as empty. The empty string is &#8221;, and that&#8217;s it.<br />
</span></p>
<p class="refpurpose" style="text-align: justify;"><span class="dc-title">I can imagine the reason why &#8220;0&#8243; is considered as empty. In PHP you could do <strong>322 + &#8220;17&#8243;</strong> and internally it will transform the string &#8220;17&#8243; in the number 17, so it will compute the numeric addition. With this in mind, when &#8220;0&#8243; is passed to that function, there is no feasible way to know if the programmer is passing the string &#8220;0&#8243; or the number 0 which forgot to cast to an integer.</span></p>
<p class="refpurpose" style="text-align: justify;"><span class="dc-title"><strong>Conclussion: </strong>the function <strong>empty</strong> should not contemplate the case of &#8216;0&#8242; as true. <em><strong>It is wrong to assume that the programmer  is stupid enough to  forget to do a cast, and forces good people</strong></em> (like  me <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> ), that understands what empty means and knows how to cast a variable in PHP, <em><strong>to avoid that implementation</strong>.</em></span></p>
<p class="refpurpose" style="text-align: justify;"><span class="dc-title">The original bug was my fault for using a function without knowing its behaviour in all cases, but IMHO that function should behave in another way.<br />
</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2009/01/la-funcion-empty-de-php-esta-mal/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Superfast tokenizer in PHP</title>
		<link>http://www.codigomanso.com/en/2008/11/tokenizador-superrapido-en-php/</link>
		<comments>http://www.codigomanso.com/en/2008/11/tokenizador-superrapido-en-php/#comments</comments>
		<pubDate>Thu, 27 Nov 2008 19:00:25 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[analizador lexico]]></category>
		<category><![CDATA[analizador sintactico]]></category>
		<category><![CDATA[lexer php]]></category>
		<category><![CDATA[parser]]></category>
		<category><![CDATA[php tokenizer]]></category>
		<category><![CDATA[tokenizador]]></category>
		<category><![CDATA[tokenizer]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/?p=162</guid>
		<description><![CDATA[I am currently working on a PHP library/framework (I think it looks more like a library than a framework, and I think it&#8217;s better this way).
The thing is that for some of the components I needed to parse some data (look at parser definition), and for that task I needed a PHP tokenizer (or PHP [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">I am currently working on a PHP library/framework (I think it looks more like a library than a framework, and I think it&#8217;s better this way).</p>
<p style="text-align: justify;">The thing is that for some of the components I needed to parse some data (<a href="http://en.wikipedia.org/wiki/Parsing" target="_blank">look at parser definition</a>), and for that task I needed a PHP tokenizer (or PHP <a href="http://en.wikipedia.org/wiki/Lexical_analysis">lexer</a> if you prefer). </p>
<p style="text-align: justify;">As PHP is a interpreted language, the faster the implementation, the best. So I have two possibilities: start from scratch, using the standard string functions that come with PHP (it seems to me that is going to be slow); or, on the other hand, use <strong>token_get_all</strong> which is available since PHP 4.2.0.</p>
<p style="text-align: justify;">Personally I&#8217;ve tried the later.  <strong>token_get_all</strong> it is intended to tokenize PHP code, but what I&#8217;ve done is to create a wrapper class that encapsulates a call to that function, and returns a normalized list of identifiers and text entries for each id.</p>
<p style="text-align: justify;">The trick to call to <strong>token_get_all</strong> is to add <strong>&#8220;&lt;?php &#8220;</strong> at the beginning of the string you want to parse, and append <strong>&#8220;?&gt;&#8221;</strong> at the end (probably the &#8220;?&gt;&#8221; at the end is optional).</p>
<p style="text-align: justify;">If the tokens you need for your personal tokenizer, are a subset of the tokens that PHP supports, then using this function is your best choice. Otherwise you will have to work a little harder on another implementation (which I think would be slower). Fortunately, I think PHP grammar and tokens are standard enough to work for most of the people and needs.</p>
<p style="text-align: justify;">The problem of <strong>token_get_all</strong> function is that it returns a weird array. Sometimes it returns a simple string item, while others return an array containing a PHP identifier and the text for that token.</p>
<p style="text-align: left;">Let&#8217;s go to see a possible implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> jtokenizer <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">const</span> TK_UNKNOWN  <span style="color: #339933;">=</span> <span style="color: #208080;">0x0000</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">const</span> TK_ID       <span style="color: #339933;">=</span> <span style="color: #208080;">0x0001</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">const</span> TK_STRING   <span style="color: #339933;">=</span> <span style="color: #208080;">0x0002</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">const</span> TK_PLUS     <span style="color: #339933;">=</span> <span style="color: #208080;">0x0003</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">const</span> TK_MINUS    <span style="color: #339933;">=</span> <span style="color: #208080;">0x0004</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// ...</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> tokenize <span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$tokens</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$phptokens</span> <span style="color: #339933;">=</span> <span style="color: #990000;">token_get_all</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;?php '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$string</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'?&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$phptokens</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$ptoken</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">TK_UNKNOWN</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_string</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ptoken</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ptoken</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ptoken</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'+'</span><span style="color: #339933;">:</span> <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">TK_PLUS</span><span style="color: #339933;">;</span>  <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
          <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'-'</span><span style="color: #339933;">:</span> <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">TK_MINUS</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #666666; font-style: italic;">////////////////////////////////////////</span>
          <span style="color: #666666; font-style: italic;">// Add more tokens here!</span>
          <span style="color: #666666; font-style: italic;">// E.g: '.', ',', ';', ':', '=', ...</span>
          <span style="color: #666666; font-style: italic;">////////////////////////////////////////</span>
&nbsp;
          <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span> <span style="color: #009933; font-style: italic;">/** handle error here! */</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// this should be an array (tokenid, text)</span>
        <span style="color: #990000;">list</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$tokenid</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ptoken</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$tokenid</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #666666; font-style: italic;">// ignore opening/closing tag</span>
          <span style="color: #b1b100;">case</span> T_OPEN_TAG<span style="color: #339933;">:</span>   <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
          <span style="color: #b1b100;">case</span> T_CLOSE_TAG<span style="color: #339933;">:</span>  <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #666666; font-style: italic;">// ignore white spaces</span>
          <span style="color: #b1b100;">case</span> T_WHITESPACE<span style="color: #339933;">:</span> <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #b1b100;">case</span> T_CONSTANT_ENCAPSED_STRING<span style="color: #339933;">:</span>
            <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">TK_STRING</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// remove ' or &quot; at the beginning and at the end</span>
            <span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #b1b100;">case</span> T_STRING<span style="color: #339933;">:</span>
            <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">TK_ID</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #666666; font-style: italic;">///////////////////////////////////////////</span>
          <span style="color: #666666; font-style: italic;">// Add more tokens here!</span>
          <span style="color: #666666; font-style: italic;">// Get a complete list from:</span>
          <span style="color: #666666; font-style: italic;">// http://uk3.php.net/manual/en/tokens.php</span>
          <span style="color: #666666; font-style: italic;">///////////////////////////////////////////</span>
&nbsp;
          <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span> <span style="color: #009933; font-style: italic;">/** handle error here! */</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>        
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// append the token</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">array_push</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$tokens</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$tokens</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p style="text-align: justify;">The code above is a really simple skeleton that exemplifies how easy is to make a parser based on that native PHP function. There are lots of things to be done on that function, of course, but you get an idea, ¿right?</p>
<p style="text-align: justify;">Then, tokenizing a string would be as easy as <strong>jtokenizer::tokenize ($string)</strong></p>
<p style="text-align: justify;">The advantage is that handling the array returned by jtokenizer::tokenize method is really easy (the first element would be an ID, while the second is always the text). And we can define our own tokens, so if you want interpret &#8220;return&#8221; as a normal ID, you could do it, by returning jtokenizer::TK_ID.</p>
<p style="text-align: justify;">I think that example is enough to anybody that wants to make it&#8217;s own parser, and anyway, if you do not want to return an array of arrays, feel free to change the code to return whatever you think is better to you <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p style="text-align: justify;">For more information, take a look at the PHP documentation:</p>
<ul style="text-align: justify;">
<li><a href="http://uk3.php.net/token_get_all" target="_blank"><strong>token_get_all</strong> function in php.net</a></li>
<li><a href="http://uk.php.net/manual/en/book.tokenizer.php" target="_blank">PHP tokenizer</a></li>
<li><a href="http://uk3.php.net/manual/en/tokens.php" target="_blank">List of tokens returned by <strong>token_get_all</strong> (very useful if you plan to extend the skeleton above)</a></li>
</ul>
<p style="text-align: justify;"><b>Note:</b>If you want a faster tokenizer, then use associative arrays instead of switches (you could take a look to <a href="http://www.codigomanso.com/es/2008/11/php-switch-vs-array-asociativo/">is it better use associative arrays or switches?</a> &#8211; unfortunately there is no english translation yet)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2008/11/tokenizador-superrapido-en-php/feed/</wfw:commentRss>
		<slash:comments>814</slash:comments>
		</item>
	</channel>
</rss>

