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

<?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

你可能感兴趣的:(PHP)