<?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/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>想当摄影师的程序员 &#187; php</title>
	<atom:link href="http://blog.iworm.net/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.iworm.net</link>
	<description>争取不惑之年成为一名自由摄影师</description>
	<lastBuildDate>Tue, 10 Jan 2012 04:09:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by-nc-nd/2.5/cn/</creativeCommons:license>
		<item>
		<title>PHP substr 截取 UTF8字符串乱码的解决</title>
		<link>http://blog.iworm.net/2010/03/20/php-substr-%e6%88%aa%e5%8f%96-utf8%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b9%b1%e7%a0%81%e7%9a%84%e8%a7%a3%e5%86%b3/</link>
		<comments>http://blog.iworm.net/2010/03/20/php-substr-%e6%88%aa%e5%8f%96-utf8%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b9%b1%e7%a0%81%e7%9a%84%e8%a7%a3%e5%86%b3/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 17:18:33 +0000</pubDate>
		<dc:creator>iworm</dc:creator>
				<category><![CDATA[编程]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[utf8]]></category>

		<guid isPermaLink="false">http://blog.iworm.net/?p=681</guid>
		<description><![CDATA[PHP的substr方法可以按照字节来截取字符串, 对于英文来说是没问题的. 因为一个英文字符只占一个字节. 但是对于截取UTF8编码的中文就会存在问题. 比如: var $str = &#8216;我爱twitter哈哈&#8217;; echo substr($str, 0, 1); 就会出现一个乱字符, 因为在php中, utf8编码的中文是占3个字节, 单单只取一个字节肯定是不行啦. 如果: echo substr($str, 0, 3)就会出现&#8221;我&#8221; echo substr($str, 0, 9) 会出现&#8221;我爱twi&#8221; 在中英文混排的时候, 这个函数就显得无能为力了. google了一下, 发现很多人写了替代方法, 比如自己去实现一个substr, 里面写循环. 这样肯定效率低下, 直接用substr的复杂度是1, 而自己写循环, 复杂度就是n了. 后来找到了Wikipedia的条目, 发现原来3字节的utf8编码是有规律的. 一个非英文字符的3-bytes &#8230; <a href="http://blog.iworm.net/2010/03/20/php-substr-%e6%88%aa%e5%8f%96-utf8%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b9%b1%e7%a0%81%e7%9a%84%e8%a7%a3%e5%86%b3/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>PHP的substr方法可以按照字节来截取字符串, 对于英文来说是没问题的. 因为一个英文字符只占一个字节.<br />
但是对于截取UTF8编码的中文就会存在问题.<br />
比如:<br />
var $str = &#8216;我爱twitter哈哈&#8217;;<br />
echo substr($str, 0, 1);<br />
就会出现一个乱字符, 因为在php中, utf8编码的中文是占3个字节, 单单只取一个字节肯定是不行啦.<br />
如果: echo substr($str, 0, 3)就会出现&#8221;我&#8221;<br />
echo substr($str, 0, 9) 会出现&#8221;我爱twi&#8221;</p>
<p>在中英文混排的时候, 这个函数就显得无能为力了.</p>
<p>google了一下, 发现很多人写了替代方法, 比如自己去实现一个substr, 里面写循环.<br />
这样肯定效率低下, 直接用substr的复杂度是1, 而自己写循环, 复杂度就是n了.</p>
<p>后来找到了<a href="http://en.wikipedia.org/wiki/UTF-8">Wikipedia的条目</a>, 发现原来3字节的utf8编码是有规律的.<br />
一个非英文字符的3-bytes utf8 编码, 总是以224-239之间的char code开头的, 这就好办了.<br />
思路是这样的:<br />
先用substr截取, 判断截取结果的最后一位是不是在224-239之间, 如果是, 就说明目前取到了3-byte utf8编码的第一位, 那么只需要把计划截取的长度-1即可.<br />
如果截取结果的最后一位之前一位在224-239之间, 就说明目前取到了3-byte utf8编码的第二位, 只需要将计划截取的长度-2即可.</p>
<p>下面是代码:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> csubstr<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #000088;">$beginIndex</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</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;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #000088;">$beginIndex</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$char</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ord</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$beginIndex</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: #009900;">&#93;</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;">$char</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">224</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$char</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">239</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #000088;">$beginIndex</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: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$str</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$char</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ord</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$beginIndex</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: #009900;">&#93;</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;">$char</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">224</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$char</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">239</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #000088;">$beginIndex</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: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$str</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #000088;">$beginIndex</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.iworm.net%2F2010%2F03%2F20%2Fphp-substr-%25e6%2588%25aa%25e5%258f%2596-utf8%25e5%25ad%2597%25e7%25ac%25a6%25e4%25b8%25b2%25e4%25b9%25b1%25e7%25a0%2581%25e7%259a%2584%25e8%25a7%25a3%25e5%2586%25b3%2F&amp;title=PHP%20substr%20%E6%88%AA%E5%8F%96%20UTF8%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B9%B1%E7%A0%81%E7%9A%84%E8%A7%A3%E5%86%B3" id="wpa2a_2"><img src="http://blog.iworm.net/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.iworm.net/2010/03/20/php-substr-%e6%88%aa%e5%8f%96-utf8%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b9%b1%e7%a0%81%e7%9a%84%e8%a7%a3%e5%86%b3/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-nd/2.5/cn/</creativeCommons:license>
	</item>
		<item>
		<title>How to: Implement Dynamic Route in CakePHP</title>
		<link>http://blog.iworm.net/2009/12/17/how-to-implement-dynamic-route-in-cakephp/</link>
		<comments>http://blog.iworm.net/2009/12/17/how-to-implement-dynamic-route-in-cakephp/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 16:53:30 +0000</pubDate>
		<dc:creator>iworm</dc:creator>
				<category><![CDATA[编程]]></category>
		<category><![CDATA[cakephp]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[route]]></category>

		<guid isPermaLink="false">http://blog.iworm.net/?p=668</guid>
		<description><![CDATA[I&#8217;ve found a method to implement Dynamic Route in CakePHP. If we develop a CMS program using CakePHP, we often let people to define their menu code. The menu code basically is the URL path. For example: An admin create &#8230; <a href="http://blog.iworm.net/2009/12/17/how-to-implement-dynamic-route-in-cakephp/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve found a method to implement Dynamic Route in CakePHP.<br />
If we develop a CMS program using CakePHP, we often let people to define their menu code. The menu code basically is the URL path.<br />
For example: An admin create a menu named &#8216;Milk Product&#8217;, and give this menu a code &#8216;product&#8217;.<br />
He wish to access this menu via /product</p>
<p>So we can add a static route in routes.php<br />
Router::connect(&#8216;/product&#8217;, array(&#8216;controller&#8217; => &#8216;product&#8217;, &#8216;action&#8217; => &#8216;index&#8217;));</p>
<p>But one day, he want to change the menu code from &#8216;product&#8217; to &#8216;milkproduct&#8217;? What to do then? Ask the site admin to edit the routes.php? Of course not.</p>
<p>We can use the code below to resolve this problem.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$menus</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
Cache<span style="color: #339933;">::</span><span style="color: #004000;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'routemenus'</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;">$menus</span> <span style="color: #339933;">=</span> Cache<span style="color: #339933;">::</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'routemenus'</span><span style="color: #009900;">&#41;</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;">echo</span> <span style="color: #0000ff;">'load from db'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$menusModel</span> <span style="color: #339933;">=</span> ClassRegistry<span style="color: #339933;">::</span><span style="color: #004000;">init</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Menu'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$menus</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$menusModel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'all'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'conditions'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'parent_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'1'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Cache<span style="color: #339933;">::</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'routemenus'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$menus</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$menus</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$menuitem</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    Router<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$menuitem</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Menu'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'code'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/:action/*'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$menuitem</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'MenuType'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'code'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
Router<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'controller'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'homepage'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'action'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'index'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We have 2 menu related tables: menus and menu_types.<br />
Their structure as belows:</p>
<pre>menus
    id
    menu_type_id
    parent_id
    lft
    rght
    code
    name
    created
    modified

menu_types
    id
    code
    name
    created
    modified</pre>
<p>The code in menu_types means the controller name, and the code in menus means the path.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.iworm.net%2F2009%2F12%2F17%2Fhow-to-implement-dynamic-route-in-cakephp%2F&amp;title=How%20to%3A%20Implement%20Dynamic%20Route%20in%20CakePHP" id="wpa2a_4"><img src="http://blog.iworm.net/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.iworm.net/2009/12/17/how-to-implement-dynamic-route-in-cakephp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-nd/2.5/cn/</creativeCommons:license>
	</item>
		<item>
		<title>Ubuntu中完全删除Apache2的办法</title>
		<link>http://blog.iworm.net/2007/12/26/ubuntu%e4%b8%ad%e5%ae%8c%e5%85%a8%e5%88%a0%e9%99%a4apache2%e7%9a%84%e5%8a%9e%e6%b3%95/</link>
		<comments>http://blog.iworm.net/2007/12/26/ubuntu%e4%b8%ad%e5%ae%8c%e5%85%a8%e5%88%a0%e9%99%a4apache2%e7%9a%84%e5%8a%9e%e6%b3%95/#comments</comments>
		<pubDate>Tue, 25 Dec 2007 16:14:41 +0000</pubDate>
		<dc:creator>iworm</dc:creator>
				<category><![CDATA[操作系统]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[ftp]]></category>
		<category><![CDATA[LAMP]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[XAMPP]]></category>

		<guid isPermaLink="false">http://iworm.net/2007/12/26/ubuntu%e4%b8%ad%e5%ae%8c%e5%85%a8%e5%88%a0%e9%99%a4apache2%e7%9a%84%e5%8a%9e%e6%b3%95/</guid>
		<description><![CDATA[Ubuntu安装Apache2很简单，只需要sudo apt-get install apache2 但是我使用sudo apt-get remove apache2之后，说只能释放零点几k的空间，而且删除之后仍然能访问到apache服务器，可见没删掉。 后来我看到apt-get 还有个参数是autoremove，即sudo apt-get autoremove 这下就删掉了，释放了37M空间。 自己安装LAMP麻烦，推荐安装XAMPP，装好之后LAMP都有了，而且PHP还带有很多常用的库，Apache也是带SSL的，还有FTP服务。]]></description>
			<content:encoded><![CDATA[<p>Ubuntu安装Apache2很简单，只需要sudo apt-get install apache2</p>
<p>但是我使用sudo apt-get remove apache2之后，说只能释放零点几k的空间，而且删除之后仍然能访问到apache服务器，可见没删掉。</p>
<p>后来我看到apt-get 还有个参数是autoremove，即sudo apt-get autoremove</p>
<p>这下就删掉了，释放了37M空间。</p>
<p>自己安装LAMP麻烦，推荐安装<a href="http://www.apachefriends.org/en/xampp.html" target="_blank">XAMPP</a>，装好之后LAMP都有了，而且PHP还带有很多常用的库，Apache也是带SSL的，还有FTP服务。</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.iworm.net%2F2007%2F12%2F26%2Fubuntu%25e4%25b8%25ad%25e5%25ae%258c%25e5%2585%25a8%25e5%2588%25a0%25e9%2599%25a4apache2%25e7%259a%2584%25e5%258a%259e%25e6%25b3%2595%2F&amp;title=Ubuntu%E4%B8%AD%E5%AE%8C%E5%85%A8%E5%88%A0%E9%99%A4Apache2%E7%9A%84%E5%8A%9E%E6%B3%95" id="wpa2a_6"><img src="http://blog.iworm.net/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.iworm.net/2007/12/26/ubuntu%e4%b8%ad%e5%ae%8c%e5%85%a8%e5%88%a0%e9%99%a4apache2%e7%9a%84%e5%8a%9e%e6%b3%95/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-nd/2.5/cn/</creativeCommons:license>
	</item>
	</channel>
</rss>

