Home  >  Article  >  Backend Development  >  How to convert full-width characters to half-width characters in PHP

How to convert full-width characters to half-width characters in PHP

墨辰丷
墨辰丷Original
2018-06-08 17:54:481697browse

本篇文章主要介绍PHP实现全角字符转为半角的方法,感兴趣的朋友参考下,希望对大家有所帮助。

最简单的方法

";  
$str = preg_replace('/\xa3([\xa1-\xfe])/e', 'chr(ord(\1)-0x80)', $str);  
echo $str;

这是网上看来的代码,所有的中文标点的第二个字节减去0X80(即128)所得的数字就是半角所得的数字了。而/e模式表达的是:如果设定了此修正符,preg_replace() 在替换字符串中对逆向引用作正常的替换,将其作为 PHP 代码求值,并用其结果来替换所搜索的字符串。

在非UTF-8模式下这个函数是可行的,但是UTF-8下 这个方法就似乎无效,

方法二:

$queue = Array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', 
'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E', 'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J', 
'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O', 'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T', 
'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y', 'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd', 
'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i', 'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n', 
'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's', 't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x', 
'y' => 'y', 'z' => 'z');
echo preg_replace_callback("/([\xEF][\xBC][\x90-\x99]|[\xEF][\xBD][\x81-\x9A\xA1-\xBA])/", 'next_fchar', '0');
function next_fchar($matches){
 global $queue;
 return $queue[$matches[1]];
}

方法三:

/**
* 字符串半角和全角间相互转换
* @param string $str 待转换的字符串
* @param int  $type TODBC:转换为半角;TOSBC,转换为全角
* @return string 返回转换后的字符串
*/
function convertStrType($str, $type) {
    $dbc = array( 
      '0' , '1' , '2' , '3' , '4' , 
      '5' , '6' , '7' , '8' , '9' , 
      'A' , 'B' , 'C' , 'D' , 'E' , 
      'F' , 'G' , 'H' , 'I' , 'J' , 
      'K' , 'L' , 'M' , 'N' , 'O' , 
      'P' , 'Q' , 'R' , 'S' , 'T' , 
      'U' , 'V' , 'W' , 'X' , 'Y' , 
      'Z' , 'a' , 'b' , 'c' , 'd' , 
      'e' , 'f' , 'g' , 'h' , 'i' , 
      'j' , 'k' , 'l' , 'm' , 'n' , 
      'o' , 'p' , 'q' , 'r' , 's' , 
      't' , 'u' , 'v' , 'w' , 'x' , 
      'y' , 'z' , '-' , ' ' , ':' ,
      '.' , ',' , '/' , '%' , '#' ,
      '!' , '@' , '&' , '(' , ')' ,
      '<' , '>' , '"' , ''' , '?' ,
      '[' , ']' , '{' , '}' , '\' ,
      '|' , '+' , '=' , '_' , '^' ,
      '¥' , ' ̄' , '`'
);
    $sbc = array( //半角
      '0', '1', '2', '3', '4', 
      '5', '6', '7', '8', '9', 
      'A', 'B', 'C', 'D', 'E', 
      'F', 'G', 'H', 'I', 'J', 
      'K', 'L', 'M', 'N', 'O', 
      'P', 'Q', 'R', 'S', 'T', 
      'U', 'V', 'W', 'X', 'Y', 
      'Z', 'a', 'b', 'c', 'd', 
      'e', 'f', 'g', 'h', 'i', 
      'j', 'k', 'l', 'm', 'n', 
      'o', 'p', 'q', 'r', 's', 
      't', 'u', 'v', 'w', 'x', 
      'y', 'z', '-', ' ', ':',
      '.', ',', '/', '%', ' #',
      '!', '@', '&', '(', ')',
      '<', '>', '"', '\'','?',
      '[', ']', '{', '}', '\\',
      '|', '+', '=', '_', '^',
      '¥','~', '`'
);
if($type == 'TODBC'){
return str_replace( $sbc, $dbc, $str ); //半角到全角
}elseif($type == 'TOSBC'){
return str_replace( $dbc, $sbc, $str ); //全角到半角
}else{
return $str;
}
}

方法四:

/**
* 将一个字串中含有全角的数字字符、字母、空格或'%+-()'字符转换为相应半角字符
*
* @access public
* @param string $str 待转换字串
*
* @return string $str 处理后字串
*/
function make_semiangle($str)
{
$arr = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4',
'5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9',
'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E',
'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J',
'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O',
'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T',
'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y',
'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd',
'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i',
'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n',
'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's',
't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x',
'y' => 'y', 'z' => 'z',
'(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[',
'】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']',
'‘' => '[', ''' => ']', '{' => '{', '}' => '}', '《' => '<',
'》' => '>',
'%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-',
':' => ':', '。' => '.', '、' => ',', ',' => '.', '、' => '.',
';' => ',', '?' => '?', '!' => '!', '…' => '-', '‖' => '|',
'”' => '"', ''' => '`', '‘' => '`', '|' => '|', '〃' => '"',
' ' => ' ');
return strtr($str, $arr);
}

全角与半角之区别(来自中文维基百科)

全角,又称全形、全宽,是电脑字符的一种格式,字面意思是比普通字符(或半角字符)宽的字符。

传统上,英语或拉丁字母语言使用一字节的空间来存储,而汉字、日语等常使用两字节存储,在使用固定宽度文字的地方,为了使字体看起来整齐,英文字母、数字及其他符号,也由原来只占用一个字空间,改为一概占用两个字的空间来显示,并且使用两个字节来存储。

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

PHP中的switch语句的用法

基于jQuery通过PHP与mysql实现了一个评级功能

PHP+Mysql+jQuery实现的查询和列表框选择

The above is the detailed content of How to convert full-width characters to half-width characters in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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