首页 >php教程 >php手册 >php将中文转换成相应匹配的正则表达式(要求原文本为utf

php将中文转换成相应匹配的正则表达式(要求原文本为utf

WBOY
WBOY原创
2016-06-06 19:59:53999浏览

?phpfunction getUnicodeReg($word){// 拆分汉字preg_match_all('#(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+)#s', $word, $array, PREG_PATTERN_ORDER);$return = array();// 转换foreach ($array[0] as $cc){$arr = str_split($cc);$bin_str = '';foreach ($

<?php function getUnicodeReg($word)
{
// 拆分汉字
preg_match_all('#(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+)#s', $word, $array, PREG_PATTERN_ORDER);
$return  = array();
// 转换
foreach ($array[0] as $cc)
{
	$arr = str_split($cc);
	$bin_str = '';
	foreach ($arr as $value)
	{
		$bin_str .= decbin(ord($value));
	}
	$bin_str = preg_replace('/^.{4}(.{4}).{2}(.{6}).{2}(.{6})$/','$1$2$3', $bin_str);
	$return[] = '\u' . dechex(bindec($bin_str));
}
$return = '/'.implode('', $return).'/u';
return $return;
}
$word = '甲方乙方';
echo getUnicode($word);
?>

附utf-8编码表:http://www.chi2ko.com/tool/CJK.htm

方法来源于网上搜集跟自己加工,文本出自:http://blog.csdn.net/nancle/article/details/10769537

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn