<?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>Luin&#039;s Blog &#187; LAMP</title>
	<atom:link href="http://luinlee.com/tag/lamp/feed/" rel="self" type="application/rss+xml" />
	<link>http://luinlee.com</link>
	<description>A long river</description>
	<lastBuildDate>Sat, 07 Aug 2010 04:55:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Tips:PHP中截取中文字符串避免乱码</title>
		<link>http://luinlee.com/261/tips-phpstring/</link>
		<comments>http://luinlee.com/261/tips-phpstring/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 03:20:00 +0000</pubDate>
		<dc:creator>Luin</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://luinlee.com/?p=261</guid>
		<description><![CDATA[使用substr截取中文字符串可能会出现意外的乱码，而不同编码下中文字符串长度是不同的，特殊情况比较多。下面的csubstr()函数是我在开发博客巢系统时编写的，它用了一个小方法：strlen(&#8216;汉&#8217;)比较取巧的解决了这个问题。 (2010-01-23：根据Gill的回复似乎这样做并不稳妥，回来再好好研究研究，有更好的方法欢迎与我交流) /*截取字符串(避免中文乱码)*/ function csubstr&#40;$string,$sublength&#41; &#123; $len = strlen&#40;$string&#41;; if &#40;$len &#38;lt;= $sublength&#41;&#123; $string = $string; &#125;else&#123; $string = substr&#40;$string,0,$sublength&#41;; $parity= 0; for&#40;$j=0;$j&#38;lt;$sublength;$j++&#41;&#123; $temp_str=substr&#40;$string,$j,1&#41;; if&#40;Ord&#40;$temp_str&#41;&#38;gt;127&#41; $parity+=1; &#125; $n = $parity % strlen&#40;'汉'&#41;; if&#40;$n == 0&#41; &#123; $string=substr&#40;$string,0,$sublength&#41;; &#125; else &#123; $string=substr&#40;$string,0,$sublength - $n&#41;; &#125; &#125; return $string; &#125; 更新：可以在设定编码的前提下使用mb_substr避免乱码： string mb_substr &#40; string $str , [...]]]></description>
			<content:encoded><![CDATA[<p>使用substr截取中文字符串可能会出现意外的乱码，而不同编码下中文字符串长度是不同的，特殊情况比较多。下面的csubstr()函数是我在开发博客巢系统时编写的，它用了一个小方法：strlen(&#8216;汉&#8217;)比较取巧的<del datetime="2010-01-23T15:49:56+00:00">解决了这个问题。</del></p>
<p><strong>(2010-01-23：根据Gill的回复似乎这样做并不稳妥，回来再好好研究研究，有更好的方法欢迎与我交流)</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*截取字符串(避免中文乱码)*/</span>
<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;">$sublength</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;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</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;">$len</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span> <span style="color: #000088;">$sublength</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: #000088;">$string</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$string</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: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$sublength</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$parity</span><span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$j</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$j</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #000088;">$sublength</span><span style="color: #339933;">;</span><span style="color: #000088;">$j</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$temp_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;">$j</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;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">Ord</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$temp_str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #cc66cc;">127</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$parity</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: #000088;">$n</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$parity</span> <span style="color: #339933;">%</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'汉'</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;">$n</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</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;">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;">$sublength</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$string</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: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$sublength</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$n</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;">return</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>更新：可以在设定编码的前提下使用mb_substr避免乱码：</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">string <span style="color: #990000;">mb_substr</span> <span style="color: #009900;">&#40;</span> string <span style="color: #000088;">$str</span> <span style="color: #339933;">,</span> int <span style="color: #000088;">$start</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span> int <span style="color: #000088;">$length</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span> string <span style="color: #000088;">$encoding</span> <span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span></pre></div></div>

<p><strong>详见：<cite><a href="http://php.net/manual/en/function.mb-substr.php">http://php.net/manual/en/function.mb-substr.php</a></cite></strong></p>
<p>感谢Gill的回复</p>
]]></content:encoded>
			<wfw:commentRss>http://luinlee.com/261/tips-phpstring/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
