<?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/tag/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>http_build_query implemented in Python</title>
		<link>http://www.codigomanso.com/en/2010/04/http_build_query-para-python/</link>
		<comments>http://www.codigomanso.com/en/2010/04/http_build_query-para-python/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 21:00:04 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[Programacion]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[http_build_query]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=854</guid>
		<description><![CDATA[I implemented a function in Python that mimics http_build_query function.

##
# Mimics the behaviour of http_build_query PHP function
# This method can be useful for sending data to flash applications
##################################################
def http_build_query&#40;params, topkey = ''&#41;:
  from urllib import quote
&#160;
  if len&#40;params&#41; == 0:
    return &#34;&#34;
&#160;
  result = &#34;&#34;
&#160;
  # is a [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">I implemented a function in Python that mimics http_build_query function.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">##</span>
<span style="color: #808080; font-style: italic;"># Mimics the behaviour of http_build_query PHP function</span>
<span style="color: #808080; font-style: italic;"># This method can be useful for sending data to flash applications</span>
<span style="color: #808080; font-style: italic;">##################################################</span>
<span style="color: #ff7700;font-weight:bold;">def</span> http_build_query<span style="color: black;">&#40;</span>params, topkey = <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">urllib</span> <span style="color: #ff7700;font-weight:bold;">import</span> quote
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>params<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
  result = <span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;"># is a dictionary?</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">type</span> <span style="color: black;">&#40;</span>params<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">dict</span>:
    <span style="color: #ff7700;font-weight:bold;">for</span> key <span style="color: #ff7700;font-weight:bold;">in</span> params.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
      newkey = quote <span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">if</span> topkey <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">''</span>:
        newkey = topkey + quote<span style="color: black;">&#40;</span><span style="color: #483d8b;">'['</span> + key + <span style="color: #483d8b;">']'</span><span style="color: black;">&#41;</span>
&nbsp;
      <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">type</span><span style="color: black;">&#40;</span>params<span style="color: black;">&#91;</span>key<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">dict</span>:
        result += http_build_query <span style="color: black;">&#40;</span>params<span style="color: black;">&#91;</span>key<span style="color: black;">&#93;</span>, newkey<span style="color: black;">&#41;</span>
&nbsp;
      <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #008000;">type</span><span style="color: black;">&#40;</span>params<span style="color: black;">&#91;</span>key<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">list</span>:
        i = <span style="color: #ff4500;">0</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> val <span style="color: #ff7700;font-weight:bold;">in</span> params<span style="color: black;">&#91;</span>key<span style="color: black;">&#93;</span>:
          result += newkey + quote<span style="color: black;">&#40;</span><span style="color: #483d8b;">'['</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">']'</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;=&quot;</span> + quote<span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>val<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&amp;&quot;</span>
          i = i + <span style="color: #ff4500;">1</span>
&nbsp;
      <span style="color: #808080; font-style: italic;"># boolean should have special treatment as well</span>
      <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #008000;">type</span><span style="color: black;">&#40;</span>params<span style="color: black;">&#91;</span>key<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">bool</span>:
        result += newkey + <span style="color: #483d8b;">&quot;=&quot;</span> + quote <span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>params<span style="color: black;">&#91;</span>key<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&amp;&quot;</span>
&nbsp;
      <span style="color: #808080; font-style: italic;"># assume string (integers and floats work well)</span>
      <span style="color: #ff7700;font-weight:bold;">else</span>:
        result += newkey + <span style="color: #483d8b;">&quot;=&quot;</span> + quote <span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>params<span style="color: black;">&#91;</span>key<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&amp;&quot;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;"># remove the last '&amp;'</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>result<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: black;">&#40;</span>topkey == <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: black;">&#40;</span>result<span style="color: black;">&#91;</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'&amp;'</span><span style="color: black;">&#41;</span>:
    result = result<span style="color: black;">&#91;</span>:-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">return</span> result</pre></div></div>

<p>If you find the code useful, feel free to use it in any kind of software product, be it commercial or not <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2010/04/http_build_query-para-python/feed/</wfw:commentRss>
		<slash:comments>45</slash:comments>
		</item>
		<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>11</slash:comments>
		</item>
		<item>
		<title>PHP amazingly improves itself</title>
		<link>http://www.codigomanso.com/en/2009/11/php-se-supera-a-si-mismo/</link>
		<comments>http://www.codigomanso.com/en/2009/11/php-se-supera-a-si-mismo/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 09:20:29 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[goto]]></category>
		<category><![CDATA[karmic]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[php 5.3]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=748</guid>
		<description><![CDATA[This was me, looking how to install PHP 5.3 in the new Ubuntu Karmic, because using the new functionality of late static binding will simplify my code, and I&#8217;ve found something amazing. A new feature that was missing in PHP, but they have included in PHP 5.3&#8230;
the&#8230;
wait for it&#8230;.
wait for it&#8230;.
wait for it&#8230;.
GOTO
It&#8217;s amazing how [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">This was me, looking how to install PHP 5.3 in the new Ubuntu Karmic, because using the new functionality of late static binding will simplify my code, and I&#8217;ve found something amazing. A new feature that was missing in PHP, but they have included in PHP 5.3&#8230;</p>
<p style="text-align: center;height: 3em;">the&#8230;</p>
<p style="text-align: center;height: 3em;">wait for it&#8230;.</p>
<p style="text-align: center;height: 3em;">wait for it&#8230;.</p>
<p style="text-align: center;height: 3em;">wait for it&#8230;.</p>
<p style="text-align: center;font-size: 2em; height: 2em;"><strong>GOTO</strong></p>
<p style="text-align: justify;">It&#8217;s amazing how PHP team is including in the same release high-level features, as well as a low-level feature like that (BTW most of the people will consider this feature harmful). They should have felt a great pressure from people or something, because I cannot find any reasonable reason for them to include it&#8230;</p>
<p style="text-align: justify;"><a href="http://us.php.net/goto" target="_blank">In the PHP <b>goto</b> documentation</a> you will see that at least, they are funny about it:</p>
<p style="text-align: justify;"><a href="http://us.php.net/goto" target="_blank"><img class="aligncenter" title="Take care, hungry dinosaur approaching" src="http://us.php.net/manual/en/images/0baa1b9fae6aec55bbb73037f3016001-xkcd-goto.png" alt="" width="516" height="140" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2009/11/php-se-supera-a-si-mismo/feed/</wfw:commentRss>
		<slash:comments>10</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>Keeping aspect ratio when scaling an image</title>
		<link>http://www.codigomanso.com/en/2009/03/escalar-una-imagen-manteniendo-el-aspecto/</link>
		<comments>http://www.codigomanso.com/en/2009/03/escalar-una-imagen-manteniendo-el-aspecto/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 20:10:52 +0000</pubDate>
		<dc:creator>Pau Sanchez</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[aspect ratio]]></category>
		<category><![CDATA[aspecto]]></category>
		<category><![CDATA[escalar]]></category>
		<category><![CDATA[imagenes]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.codigomanso.com/es/?p=621</guid>
		<description><![CDATA[Scaling an image to keep it under specific bounds, for visualizing purposes is a common task, but it&#8217;s even better if we can scale the image without deforming it. In this cases we have to scale the image and keep it&#8217;s original aspect ratio.
Nowadays there are thousands of libraries for any general purpose language that [...]]]></description>
			<content:encoded><![CDATA[<p>Scaling an image to keep it under specific bounds, for visualizing purposes is a common task, but it&#8217;s even better if we can scale the image without deforming it. In this cases we have to scale the image and keep it&#8217;s original aspect ratio.</p>
<p>Nowadays there are thousands of libraries for any general purpose language that can perform the scaling of an image. The question is ¿which are the new width and height for the image?</p>
<p>Following there is this simple code, that have been tested using javascript, but should work in PHP, Python, Java, Perl, C, or whatever language you want to try <img src='http://www.codigomanso.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>w <span style="color: #339933;">&lt;</span> h<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  h <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">*</span> maxWidth<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> w<span style="color: #339933;">;</span>
  w <span style="color: #339933;">=</span> maxWidth<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
  w <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>w <span style="color: #339933;">*</span> maxHeight<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> h<span style="color: #339933;">;</span>
  h <span style="color: #339933;">=</span> maxHeight<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><b>w</b> represents the image width</p>
<p><b>h</b> represents the image height</p>
<p><b>maxWidth</b> represents the maximum width boundary where the image is going to be embedded</p>
<p><b>maxHeight</b> represents the maximum height boundary where the image is going to be embedded</p>
<p>As a final example, although is a copy-paste, imagine you have an image of size (w, h) and you have to fit it in an area of 640&#215;480:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">var maxWidth <span style="color: #339933;">=</span> <span style="color: #cc66cc;">640</span>, maxHeight <span style="color: #339933;">=</span> <span style="color: #cc66cc;">480</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>w <span style="color: #339933;">&gt;</span> maxWidth <span style="color: #339933;">||</span> h <span style="color: #339933;">&gt;</span> maxHeight<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>w <span style="color: #339933;">&lt;</span> h<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    h <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">*</span> maxWidth<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> w<span style="color: #339933;">;</span>
    w <span style="color: #339933;">=</span> maxWidth<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    w <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>w <span style="color: #339933;">*</span> maxHeight<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> h<span style="color: #339933;">;</span>
    h <span style="color: #339933;">=</span> maxHeight<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codigomanso.com/en/2009/03/escalar-una-imagen-manteniendo-el-aspecto/feed/</wfw:commentRss>
		<slash:comments>17</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>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>

