0){ if([c"/> 0){ if([c">

Heim  >  Artikel  >  Backend-Entwicklung  >  自个儿实现php UTF8中文字符串截取

自个儿实现php UTF8中文字符串截取

WBOY
WBOYOriginal
2016-06-13 13:10:41728Durchsuche

自己实现php UTF8中文字符串截取

header("Content-type: text/html; charset=utf-8");
function my_substr($str,$begin,$length){
		$i = $begin;
		$result="";
		while($length > 0){
			if([color=red]ord($str[$i])>127[/color]){
				$result .= substr($str,$i,3);
				$i = $i+3;
			}else{
				$result .= substr($str,$i,1);
				$i++;
			}
			$length--;
		}
		return $result;
	}

	$chinese = "中a国people";
	
	echo "<br>".my_substr($chinese,0,3);



输出结果是:中a国

说明:
ord 是 对字符去assic值。
chr 是 对assic取字符。

为什么判断assic大于127。

这里是ASSIC码表
http://www.asciitable.com/

计算机中最开始只有ASSIC编码,用来表示字符。一个ASSIC字符用一个BYTE表示。所以ASSIC最多就只有256种组合。对于英文是够用了,中文,日文,韩文等亚洲语种就不够了。
那么只能考虑用多个BYTE表示一个中文汉字,比如GB2312 就是用2个字节表示一个汉字。在windows中用笔记本新建一个TXT保存为ASSIC,如果你是简体中文操作系统,TXT中的中文就是一GB2312来保存的。上面的截取字符串的程序$result .= substr($str,$i,3);中的3就要改成2.同时别忘了修改header。而无论GB2312 还是UTF8 他们表示A-Z等ASSIC 128以前的都是一样的,是一位BTYE表示,是变长编码的。所以可以用ASSIC判断他们是不是中文。

写的可能比较乱。有需要的谨慎阅读。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn