<?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>JACKLY.ORG &#187; 乱码</title>
	<atom:link href="http://jackly.org/tag/%e4%b9%b1%e7%a0%81/feed/" rel="self" type="application/rss+xml" />
	<link>http://jackly.org</link>
	<description>关于互联网的体验 · 设计 · 分享 · 闲聊</description>
	<lastBuildDate>Wed, 03 Aug 2011 06:48:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>彻底解决wordpress中文截断产生乱码的方法</title>
		<link>http://jackly.org/wordpress/wordpress-wordcut/</link>
		<comments>http://jackly.org/wordpress/wordpress-wordcut/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 03:37:10 +0000</pubDate>
		<dc:creator>Jackly</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[乱码]]></category>
		<category><![CDATA[截断]]></category>

		<guid isPermaLink="false">http://jackly.org/?p=368</guid>
		<description><![CDATA[wordpress的海量模板是它的亮点之一，国内众多高手设计的模板已经非常漂亮。可是残酷的现实是大部分的模板都还是出于老外之手，老外在做模板的时候一般只会考虑到显示正常即可，而我们如果用中文来写博客毕竟和英文环境有所差别，这样可能会出现很多的问题，比如字体大小，乱码，这些都要手动调整。 话说调整CSS很多人都已经是轻车熟路了，本文不讨论。 不过可能大家会遇到类似下图的现象： 产生乱码的原因 这种乱码现象一般是出现在正文截断的时候，因为Wordpress模板的字符集是UTF-8，它对于英文来说是一个字母一个字节（8位）来编码，而中文却是一个汉字三个字节（24位）来编码。所以截断的函数只会判断在多少字节的地方去截，如果到了那个地方一个汉字只显示了三分之一或者三分之二的时候，那个汉字就不能完整显示，而函数又不能很智能的多截一个字节或者少截一个，怎么办？于是就产生了乱码。这个问题相信在很多有类似功能的国外模板上都会出现，这不是数据库的错，也不是字符集的问题，只是因为制作者在编写模板的时候并没有考虑到中文字符可能产生的问题。 所以，我们要替换截断的函数。简单来说，两步就可以搞定。 第一步 将以下两个函数写进主题所附带的functions.php文件中 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 function Limit_Char&#40;$max_char = 200, $more_text = '...', $more_link_text = '', $limit_type = 'content'&#41; &#123; if &#40;$limit_type == [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Wordpress" href="http://jackly.org/category/wordpress/" target="_blank">wordpress</a>的海量模板是它的亮点之一，国内众多高手设计的模板已经非常漂亮。可是残酷的现实是大部分的模板都还是出于老外之手，老外在做模板的时候一般只会考虑到显示正常即可，而我们如果用中文来写博客毕竟和英文环境有所差别，这样可能会出现很多的问题，比如字体大小，乱码，这些都要手动调整。</p>
<p>话说调整CSS很多人都已经是轻车熟路了，本文不讨论。<br />
不过可能大家会遇到类似下图的现象：</p>
<p><a href="http://jackly.org/wp-content/uploads/2010/03/luanma.jpg"><img class="alignnone size-full wp-image-383" title="wordpress截断乱码" src="http://jackly.org/wp-content/uploads/2010/03/luanma.jpg" alt="" width="309" height="201" /></a></p>
<h3>产生乱码的原因</h3>
<p>这种乱码现象一般是出现在<strong>正文截断</strong>的时候，因为Wordpress模板的字符集是UTF-8，它对于英文来说是<strong>一个字母一个字节（8位）</strong>来编码，而中文却是<strong>一个汉字三个字节（24位）</strong>来编码。所以截断的函数只会判断在多少<strong>字节</strong>的地方去截，如果到了那个地方一个汉字只显示了三分之一或者三分之二的时候，那个汉字就不能完整显示，而函数又不能很智能的多截一个字节或者少截一个，怎么办？于是就产生了乱码。这个问题相信在很多有类似功能的国外模板上都会出现，这不是数据库的错，也不是字符集的问题，只是因为制作者在编写模板的时候并没有考虑到中文字符可能产生的问题。</p>
<p>所以，我们要替换截断的函数。简单来说，两步就可以搞定。</p>
<h3>第一步</h3>
<p><strong>将以下两个函数写进主题所附带的<code>functions.php</code>文件中</strong></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
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> Limit_Char<span style="color: #009900;">&#40;</span><span style="color: #000088;">$max_char</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span> <span style="color: #000088;">$more_text</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'...'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$more_link_text</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$limit_type</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'content'</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;">$limit_type</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000088;">$limiter</span> <span style="color: #339933;">=</span> get_the_title<span style="color: #009900;">&#40;</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;">$limiter</span> <span style="color: #339933;">=</span> get_the_content<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$limiter</span> <span style="color: #339933;">=</span> apply_filters<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'the_content'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$limiter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$limiter</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strip_tags</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">']]&gt;'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">']]&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$limiter</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: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$limiter</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$max_char</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$limiter</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$limiter</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$max_char</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: #000088;">$limiter</span> <span style="color: #339933;">=</span> utf8_conver<span style="color: #009900;">&#40;</span><span style="color: #000088;">$limiter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$limiter</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$more_text</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$more_link_text</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">' &lt;a href=&quot;'</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">echo</span> the_permalink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$more_link_text</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/a&gt;'</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;">echo</span> <span style="color: #000088;">$limiter</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> utf8_conver<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</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;">$str</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: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&gt;=</span><span style="color: #cc66cc;">0</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: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$hex</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">.</span><span style="color: #990000;">ord</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ord</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</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: #009900;">&#40;</span><span style="color: #000088;">$ch</span> <span style="color: #339933;">&amp;</span> <span style="color: #cc66cc;">128</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: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</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: #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: #009900;">&#40;</span><span style="color: #000088;">$ch</span> <span style="color: #339933;">&amp;</span> <span style="color: #cc66cc;">192</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">192</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</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: #009900;">&#41;</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: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">.</span><span style="color: #000088;">$hex</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>第二步</h3>
<p><strong>把模板文件中比如<code>index.php</code>中将截断函数替换成新的。</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> Limit_Char<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;!-- 200是你需要截断多少字节的长度，根据实际情况修改。--&gt;</pre></td></tr></table></div>

<p>修改完成，大功告成啦。理论上用这个办法，只要是截断的问题都可以得到解决。<br />
BTW：本人菜鸟一只。高手轻点拍砖。</p>
<h3  class="related_post_title">相关文章</h3><ul class="related_post"><li><a href="http://jackly.org/wordpress/write-your-robot/" title="写好你的Robots.txt，完善WordPress的SEO">写好你的Robots.txt，完善WordPress的SEO</a></li><li><a href="http://jackly.org/wordpress/wordpress-permalinks-rewrite-rule/" title="Wordpress permalinks rewrite rules for IIS">Wordpress permalinks rewrite rules for IIS</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://jackly.org/wordpress/wordpress-wordcut/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
	</channel>
</rss>

