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

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

WBOY
Release: 2016-06-06 19:59:53
Original
998 people have browsed it

?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);
?>
Copy after login

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

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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template