<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[中文字符]中文字符]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Tue, 10 Apr 2007 02:22:15 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	PHP截取中文字符串方法总结:<br/>PHP截取中文字符串方法总结<br/><br/><br/>程序一：PHP截取中文字符串方法<br/>由于网站首页以及vTigerCRM里经常在截取中文字符串时出现乱码(使用substr)，今天找到一个比较好的截取中文字符串方法，在此与大家共享。<br/>function msubstr($str, $start, $len) &#123;<br/> &nbsp; &nbsp;$tmpstr = "";<br/> &nbsp; &nbsp;$strlen = $start + $len;<br/> &nbsp; &nbsp;for($i = 0; $i < $strlen; $i++) &#123;<br/> &nbsp; &nbsp; &nbsp; &nbsp;if(ord(substr($str, $i, 1)) > 0xa0) &#123;<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$tmpstr .= substr($str, $i, 2);<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$i++;<br/> &nbsp; &nbsp; &nbsp; &nbsp;&#125; else<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$tmpstr .= substr($str, $i, 1);<br/> &nbsp; &nbsp;&#125;<br/> &nbsp; &nbsp;return $tmpstr;<br/>&#125;<br/>程序二：PHP截取UTF-8字符串，解决半字符问题<br/>/******************************************************************<br/>* PHP截取UTF-8字符串，解决半字符问题。<br/>* 英文、数字（半角）为1字节（8位），中文（全角）为3字节<br/>* @return 取出的字符串, 当$len小于等于0时, 会返回整个字符串<br/>* @param $str 源字符串<br/>* $len 左边的子串的长度<br/>****************************************************************/<br/>function utf_substr($str,$len)<br/>&#123;<br/>for($i=0;$i<$len;$i++)<br/>&#123;<br/>$temp_str=substr($str,0,1);<br/>if(ord($temp_str) > 127)<br/>&#123;<br/>$i++;<br/>if($i<$len)<br/>&#123;<br/>$new_str[]=substr($str,0,3);<br/>$str=substr($str,3);<br/>&#125;<br/>&#125;<br/>else<br/>&#123;<br/>$new_str[]=substr($str,0,1);<br/>$str=substr($str,1);<br/>&#125;<br/>&#125;<br/>return join($new_str);<br/>&#125;<br/>?> <br/>php utf-8 字符串截取 <br/><?<br/>function cutstr($string, $length) &#123;<br/> &nbsp; &nbsp; &nbsp; &nbsp;preg_match_all("/[&#92;x01-&#92;x7f]&#124;[&#92;xc2-&#92;xdf][&#92;x80-&#92;xbf]&#124;&#92;xe0[&#92;xa0-&#92;xbf][&#92;x80-&#92;xbf]&#124;[&#92;xe1-&#92;xef][&#92;x80-&#92;xbf][&#92;x80-&#92;xbf]&#124;&#92;xf0[&#92;x90-&#92;xbf][&#92;x80-&#92;xbf][&#92;x80-&#92;xbf]&#124;[&#92;xf1-&#92;xf7][&#92;x80-&#92;xbf][&#92;x80-&#92;xbf][&#92;x80-&#92;xbf]/", $string, $info); &nbsp;<br/> &nbsp; &nbsp; &nbsp; &nbsp;for($i=0; $i<count($info[0]); $i++) &#123;<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$wordscut .= $info[0][$i];<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$j = ord($info[0][$i]) > 127 ? $j + 2 : $j + 1;<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ($j > $length - 3) &#123;<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return $wordscut." ...";<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&#125;<br/> &nbsp; &nbsp; &nbsp; &nbsp;&#125;<br/> &nbsp; &nbsp; &nbsp; &nbsp;return join('', $info[0]);<br/>&#125;<br/>$string="242432反对感是456犯得上广泛大使馆地方7890";<br/>for($i=0;$i<strlen($string);$i++)<br/>&#123;<br/>echo cutstr($string,$i)."<br/>";<br/>&#125;<br/>?><br/><br/>截取utf-8字符串函数<br/>为了支持多语言，数据库里的字符串可能保存为UTF-8编码，在网站开发中可能需要用php截取字符串的一部分。为了避免出现乱码现象，编写如下的UTF-8字符串截取函数<br/><br/>关于utf-8<br/><br/>UTF-8编码的字符可能由1~3个字节组成， 具体数目可以由第一个字节判断出来。(理论上可能更长，但这里假设不超过3个字节)<br/>第一个字节大于224的，它与它之后的2个字节一起组成一个UTF-8字符<br/>第一个字节大于192小于224的，它与它之后的1个字节组成一个UTF-8字符<br/>否则第一个字节本身就是一个英文字符（包括数字和一小部分标点符号）。<br/><br/>以前为某网站设计的代码(也是现在用在首页的长度截取的函数)<br/><br/>Code:<br/><?php // Cut_Str;<br/>//$sourcestr 是要处理的字符串 <br/>//$cutlength 为截取的长度(即字数) <br/>function cut_str($sourcestr,$cutlength) <br/>&#123; <br/> &nbsp; $returnstr=''; <br/> &nbsp; $i=0; <br/> &nbsp; $n=0; <br/> &nbsp; $str_length=strlen($sourcestr);//字符串的字节数 <br/> &nbsp; while (($n<$cutlength) and ($i<=$str_length)) <br/> &nbsp; &#123; <br/> &nbsp; &nbsp; &nbsp;$temp_str=substr($sourcestr,$i,1); <br/> &nbsp; &nbsp; &nbsp;$ascnum=Ord($temp_str);//得到字符串中第$i位字符的ascii码 <br/> &nbsp; &nbsp; &nbsp;if ($ascnum>=224) &nbsp; &nbsp;//如果ASCII位高与224，<br/> &nbsp; &nbsp; &nbsp;&#123; <br/> &nbsp; &nbsp; &nbsp; &nbsp; $returnstr=$returnstr.substr($sourcestr,$i,3); //根据UTF-8编码规范，将3个连续的字符计为单个字符 &nbsp; &nbsp; &nbsp; &nbsp; <br/> &nbsp; &nbsp; &nbsp; &nbsp; $i=$i+3; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//实际Byte计为3<br/> &nbsp; &nbsp; &nbsp; &nbsp; $n++; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//字串长度计1<br/> &nbsp; &nbsp; &nbsp;&#125;<br/> &nbsp; &nbsp; &nbsp;elseif ($ascnum>=192) //如果ASCII位高与192，<br/> &nbsp; &nbsp; &nbsp;&#123; <br/> &nbsp; &nbsp; &nbsp; &nbsp; $returnstr=$returnstr.substr($sourcestr,$i,2); //根据UTF-8编码规范，将2个连续的字符计为单个字符 <br/> &nbsp; &nbsp; &nbsp; &nbsp; $i=$i+2; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//实际Byte计为2<br/> &nbsp; &nbsp; &nbsp; &nbsp; $n++; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//字串长度计1<br/> &nbsp; &nbsp; &nbsp;&#125;<br/> &nbsp; &nbsp; &nbsp;elseif ($ascnum>=65 && $ascnum<=90) //如果是大写字母，<br/> &nbsp; &nbsp; &nbsp;&#123; <br/> &nbsp; &nbsp; &nbsp; &nbsp; $returnstr=$returnstr.substr($sourcestr,$i,1); <br/> &nbsp; &nbsp; &nbsp; &nbsp; $i=$i+1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//实际的Byte数仍计1个<br/> &nbsp; &nbsp; &nbsp; &nbsp; $n++; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//但考虑整体美观，大写字母计成一个高位字符<br/> &nbsp; &nbsp; &nbsp;&#125;<br/> &nbsp; &nbsp; &nbsp;else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//其他情况下，包括小写字母和半角标点符号，<br/> &nbsp; &nbsp; &nbsp;&#123; <br/> &nbsp; &nbsp; &nbsp; &nbsp; $returnstr=$returnstr.substr($sourcestr,$i,1); <br/> &nbsp; &nbsp; &nbsp; &nbsp; $i=$i+1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//实际的Byte数计1个<br/> &nbsp; &nbsp; &nbsp; &nbsp; $n=$n+0.5; &nbsp; &nbsp; &nbsp; &nbsp;//小写字母和半角标点等与半个高位字符宽...<br/> &nbsp; &nbsp; &nbsp;&#125; <br/> &nbsp; &#125; <br/> &nbsp; &nbsp; &nbsp; &nbsp; if ($str_length>$cutlength)&#123;<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$returnstr = $returnstr . "...";//超过长度时在尾处加上省略号<br/> &nbsp; &nbsp; &nbsp;&#125;<br/> &nbsp; &nbsp;return $returnstr; <br/><br/>&#125;
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [中文字符]中文字符]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>