<?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>Guy Rutenberg &#187; PHP</title>
	<atom:link href="http://www.guyrutenberg.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.guyrutenberg.com</link>
	<description>Keeping track of what I do</description>
	<lastBuildDate>Wed, 16 Jun 2010 19:53:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Configuring Lighttpd for CakePHP</title>
		<link>http://www.guyrutenberg.com/2009/01/24/configuring-lighttpd-for-cakephp/</link>
		<comments>http://www.guyrutenberg.com/2009/01/24/configuring-lighttpd-for-cakephp/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 21:38:09 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[Lighttpd]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=252</guid>
		<description><![CDATA[I tried a few days ago to install a CakePHP project of mine on a lighttpd web-server. As expected, it&#8217;s clean URLs didn&#8217;t work, so I set out to find a solution.
One possible solution is the one oulined in the CakePHP manual. The solution uses the mod_magnet module (which basically runs a Lua script to [...]]]></description>
			<content:encoded><![CDATA[<p>I tried a few days ago to install a <a href="http://www.cakephp.org">CakePHP</a> project of mine on a <a href="http://www.lighttpd.net">lighttpd</a> web-server. As expected, it&#8217;s clean URLs didn&#8217;t work, so I set out to find a solution.</p>
<p>One possible solution is the <a href="http://book.cakephp.org/view/782/Lighttpd-and-mod_magnet">one</a> oulined in the CakePHP manual. The solution uses the <code>mod_magnet</code> module (which basically runs a Lua script to do the rewriting), and I found it an overkill. I was looking for a simple solution based only on <code>mod_rewrite</code>, something like the <a href="/2008/05/24/clean-urls-permalinks-for-wordpress-on-lighttpd/">solution for Wordpress</a>.<br />
<span id="more-252"></span></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$HTTP[&quot;host&quot;] =~ &quot;^(www\.)?example.com&quot; {
        server.document-root = &quot;/home/guy/example.com/app/webroot/&quot;
        url.rewrite = (
                &quot;(css|files|img|js)/(.*)&quot; =&gt; &quot;/$1/$2&quot;,
                &quot;^([^\?]*)(\?(.+))?$&quot; =&gt; &quot;/index.php?url=$1&amp;$3&quot;,
        )
}</pre></div></div>

<p>I will go line by line over the solution. The first line defines a vhost for the site. This is a simple vhost that uses conditional configurations. The second line sets the server&#8217;s document root to my app&#8217;s webroot directory (you&#8217;ll need to change the path accordingly). The next section is the URL rewriting which is the most important part. The first line of the rewriting rules takes care of the static files you serve from the document root, e.g. CSS, JavaScript, image etc. You can add to the begining of the rule (the <code>css|files|img|js</code> any other directory you would like to sever static files from). The next line takes care of Cake&#8217;s clean URLs. It&#8217;s important to capture any query string explicitly, if exists, and pass it in appropriate way so GET variables will be passed correctly.</p>
<p>I hope other CakePHP users who want to develop their apps on lighttpd will find this &#8220;lightweight&#8221; solution helpful. Don&#8217;t hesitate to ask question and post your comments about this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2009/01/24/configuring-lighttpd-for-cakephp/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Question Marks Instead of Non-ASCII Chars when using Gettext in PHP</title>
		<link>http://www.guyrutenberg.com/2008/11/24/question-marks-instead-of-non-ascii-chars-when-using-gettext-in-php/</link>
		<comments>http://www.guyrutenberg.com/2008/11/24/question-marks-instead-of-non-ascii-chars-when-using-gettext-in-php/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 14:54:02 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Gettext]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=173</guid>
		<description><![CDATA[Yesterday I&#8217;ve ported a PHP website to use Gettext for localizations (l10n). After reading through the Gettext documentation and going through the documentation in the PHP site, I&#8217;ve manged to get everything working (almost). I had one problem, all the non-ASCII characters (accented Latin chars, Japanese and Chinese) where displayed as question marks (?) instead [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I&#8217;ve ported a PHP website to use <a href="http://www.gnu.org/software/gettext/">Gettext</a> for localizations (l10n). After reading through the Gettext documentation and going through the documentation in the <a href="http://www.php.net">PHP site</a>, I&#8217;ve manged to get everything working (almost). I had one problem, all the non-ASCII characters (accented Latin chars, Japanese and Chinese) where displayed as question marks (?) instead of the correct form. This happend despite me using UTF-8 encoded files.</p>
<p>While some people (e.g. <a href="http://www.php.net/manual/en/function.gettext.php#56314">this one</a>) suggested that it&#8217;s not possible to use non-ASCII characters when using a UTF-8 encoded message files, their is a soultion and it&#8217;s quiet simple one. All you have to do is to call <a href="http://www.php.net/manual/en/function.bind-textdomain-codeset.php"><code>bind_textdomain_codset</code></a> and pass it <code>UTF-8</code> as <code>charset</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2008/11/24/question-marks-instead-of-non-ascii-chars-when-using-gettext-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Trailing Whitespace Causes a Session to be Destroyed in CakePHP</title>
		<link>http://www.guyrutenberg.com/2008/03/08/trailing-whitespace-causes-a-session-to-be-destroyed-in-cakephp/</link>
		<comments>http://www.guyrutenberg.com/2008/03/08/trailing-whitespace-causes-a-session-to-be-destroyed-in-cakephp/#comments</comments>
		<pubDate>Fri, 07 Mar 2008 22:36:31 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/2008/03/08/trailing-whitespace-causes-a-session-to-be-destroyed-in-cakephp/</guid>
		<description><![CDATA[While working on a new project using CakePHP, I&#8217;ve came across a weird problem. In one of the controllers the the session always came out as invalid, as

$this-&#62;Session-&#62;valid&#40;&#41;

always returned false. I tried debugging this weird stuff, and it looked like all the session variables are unset. Using

$this-&#62;Session-&#62;error&#40;&#41;

to get the last error returned &#8220;Config doesn&#8217;t exist&#8221;. [...]]]></description>
			<content:encoded><![CDATA[<p>While working on a new project using <a href="http://www.cakephp.org">CakePHP</a>, I&#8217;ve came across a weird problem. In one of the controllers the the session always came out as invalid, as</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">valid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>always returned false. I tried debugging this weird stuff, and it looked like all the session variables are unset. Using</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Session</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>to get the last error returned &#8220;Config doesn&#8217;t exist&#8221;. After further debugging &#8220;Config&#8221; turned out to be an internal array that is saved by CakePHP to the session and holds various internal data (some of it is used for validation like user-agent hash). I kept printing more and more debugging data as well as looking at CakePHP&#8217;s trac system.</p>
<p>I found an interesting bug (<a href="https://trac.cakephp.org/ticket/4217">ticket #4217</a>) and it looked very promising, as it almost fully described my problem. Unfortunately, the solution offered didn&#8217;t seem to work for me. But it inspired me to try and start the session manually using <code>session_start()</code> instead of using Cake&#8217;s startup and activate methods of the Session Component. </p>
<p>I found out that the <code>session_id()</code> returned empty string. Luckily calling <code>session_start()</code> directly from the controller, gave me a lead. The session seemed to work well but a nasty error about headers already been sent showed up.</p>
<p>I little more investigation lead up that I had a trailing newline after my closing PHP tag in that controller file. Deleting this trailing whitespace completely solved the problem. No need anymore to manually start the session. It&#8217;s pretty annoying that such a small thing like trailing new line can cause such seemingly unrelated problems in CakePHP&#8217;s session handling.</p>
<p>Maybe CakePHP should add a little debug notice when the session doesn&#8217;t start because header were already sent. This can be done by modifying the <code>else</code> statement in the <code>__startSession()</code> method in <code>cake/lib/session.php</code> (line 557 in version 1.2.0.6311). I wonder what the reason they had not to inform the developer when such event happens, as I don&#8217;t see why someone will deliberately try to start the session after sending the headers, I think it only happens by mistake (at least most of the time).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2008/03/08/trailing-whitespace-causes-a-session-to-be-destroyed-in-cakephp/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Vim Macros for Wrapping Strings for Gettext</title>
		<link>http://www.guyrutenberg.com/2007/12/22/vim-macros-for-wrapping-strings-for-gettext/</link>
		<comments>http://www.guyrutenberg.com/2007/12/22/vim-macros-for-wrapping-strings-for-gettext/#comments</comments>
		<pubDate>Sat, 22 Dec 2007 10:52:20 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[vim]]></category>
		<category><![CDATA[Gettext]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/2007/12/22/vim-macros-for-wrapping-strings-for-gettext/</guid>
		<description><![CDATA[I&#8217;m working on a website and we decided to localize it using GNU gettext. Soon enough I found it tiring to wrap each string manually in _( and ) and also to do it in Smarty (using {t}string{/t}. So I decided that I need a macro that will let highlight the string that needs translation [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a website and we decided to localize it using <a href="http://www.gnu.org/software/gettext/">GNU gettext</a>. Soon enough I found it tiring to wrap each string manually in <code>_(</code> and <code>)</code> and also to do it in <a href="http://smarty.php.net">Smarty</a> (using <code>{t}string{/t}</code>. So I decided that I need a macro that will let highlight the string that needs translation and the macro will wrap for me. </p>
<p>I ended up writing two macros one for PHP files (but it&#8217;s also good for C/C++ etc.) and one for smarty.</p>

<div class="wp_syntax"><div class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #000000;">:</span>vmap tg di_<span style="color: #000000;">&#40;</span><span style="color: #000000;">&lt;</span>ESC<span style="color: #000000;">&gt;</span>pa<span style="color: #000000;">&#41;</span><span style="color: #000000;">&lt;</span>ESC<span style="color: #000000;">&gt;</span>
<span style="color: #000000;">:</span>vmap ts di<span style="color: #000000;">&#123;</span>t<span style="color: #000000;">&#125;</span><span style="color: #000000;">&lt;</span>ESC<span style="color: #000000;">&gt;</span>pa<span style="color: #000000;">&#123;</span><span style="color: #000000;">/</span>t<span style="color: #000000;">&#125;</span><span style="color: #000000;">&lt;</span>ESC<span style="color: #000000;">&gt;</span></pre></div></div>

<p>To use these macros just highlight the string for translation in vim&#8217;s visual mode and press <code>tg</code> (or <code>ts</code>), and your string will be wrapped for translation. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2007/12/22/vim-macros-for-wrapping-strings-for-gettext/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multibyte String Truncate Modifier for Smarty &#8211; mb_truncate</title>
		<link>http://www.guyrutenberg.com/2007/12/04/multibyte-string-truncate-modifier-for-smarty-mb_truncate/</link>
		<comments>http://www.guyrutenberg.com/2007/12/04/multibyte-string-truncate-modifier-for-smarty-mb_truncate/#comments</comments>
		<pubDate>Tue, 04 Dec 2007 12:53:36 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Smarty]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/2007/12/04/multibyte-string-truncate-modifier-for-smarty-mb_truncate/</guid>
		<description><![CDATA[When working with Smarty, a PHP templating engine, I discovered that while the regular truncate modifier works great on ASCII strings, it doesn&#8217;t work with multibyte strings, i.e. UTF-8 encoded strings. This leads to problems in internationalization (i18n), as UTF-8 is the popular encoding for non-Latin alphabets nowdays. The problem can be solved by modifying [...]]]></description>
			<content:encoded><![CDATA[<p>When working with Smarty, a PHP templating engine, I discovered that while the regular <code>truncate</code> modifier works great on ASCII strings, it doesn&#8217;t work with multibyte strings, i.e. UTF-8 encoded strings. This leads to problems in internationalization (i18n), as UTF-8 is the popular encoding for non-Latin alphabets nowdays. The problem can be solved by modifying the built-in <code>truncate</code> modifier  and create a new one that takes an additional argument, the charset of the string, and acts accordingly. The new modified modifier, <code>mb_truncate</code> is implemented below.<br />
<span id="more-31"></span></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: #009933; font-style: italic;">/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */</span>
&nbsp;
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Smarty truncate modifier plugin
 *
 * Type:     modifier&lt;br&gt;
 * Name:     mb_truncate&lt;br&gt;
 * Purpose:  Truncate a string to a certain length if necessary,
 *           optionally splitting in the middle of a word, and
 *           appending the $etc string or inserting $etc into the middle.
 *           This version also supports multibyte strings.
 * @link http://smarty.php.net/manual/en/language.modifier.truncate.php
 *          truncate (Smarty online manual)
 * @author   Guy Rutenberg &lt;guyrutenberg@gmail.com&gt; based on the original 
 *           truncate by Monte Ohrt &lt;monte at ohrt dot com&gt;
 * @param string
 * @param integer
 * @param string
 * @param string
 * @param boolean
 * @param boolean
 * @return string
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> smarty_modifier_mb_truncate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">80</span><span style="color: #339933;">,</span> <span style="color: #000088;">$etc</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'...'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$charset</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'UTF-8'</span><span style="color: #339933;">,</span>
                                  <span style="color: #000088;">$break_words</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #000088;">$middle</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;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$length</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: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$length</span> <span style="color: #339933;">-=</span> <span style="color: #990000;">min</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$length</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$etc</span><span style="color: #009900;">&#41;</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: #339933;">!</span><span style="color: #000088;">$break_words</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$middle</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/\s+?(\S+)?$/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #990000;">mb_substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$charset</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: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$middle</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #990000;">mb_substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</span><span style="color: #339933;">,</span> <span style="color: #000088;">$charset</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$etc</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;">return</span> <span style="color: #990000;">mb_substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$charset</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$etc</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mb_substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #000088;">$length</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$charset</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$string</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;">/* vim: set expandtab: */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The license for the code is LGPL (same as Smarty&#8217;s). To install to modifier just put it under <code>/smarty/plugins/modifier.mb_truncate.php</code>.</p>
<p>Using the <code>mb_truncate</code> modifier is similar to <code>truncate</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//$some_string is a string of utf-8 variables assign via php</span>
<span style="color: #009900;">&#123;</span><span style="color: #000088;">$some_string</span><span style="color: #339933;">|</span>mb_truncate<span style="color: #339933;">:</span><span style="color: #cc66cc;">13</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;...&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>The modifier also supports the break words, and truncate in the middle flags of the original <code>truncate</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2007/12/04/multibyte-string-truncate-modifier-for-smarty-mb_truncate/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 2.098 seconds -->
