Home > php教程 > php手册 > PHP实现全角字符转为半角方法汇总,全角半角

PHP实现全角字符转为半角方法汇总,全角半角

WBOY
Release: 2016-06-13 08:58:20
Original
1312 people have browsed it

PHP实现全角字符转为半角方法汇总,全角半角

最简单的方法

<&#63;php  
$str = "0123ABCDFWS\",.?<>{}[]*&^%#@!~()+-|:;";  
echo "$str";  
echo "<br />";  
$str = preg_replace('/\xa3([\xa1-\xfe])/e', 'chr(ord(\1)-0x80)', $str);  
echo $str;

Copy after login

这是网上看来的代码,所有的中文标点的第二个字节减去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]];
}
Copy after login

方法三:

/**
* 字符串半角和全角间相互转换
* @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', '-', ' ', ':',
      '.', ',', '/', '%', ' #',
      '!', '@', '&', '(', ')',
      '<', '>', '"', '\'','&#63;',
      '[', ']', '{', '}', '\\',
      '|', '+', '=', '_', '^',
      '¥','~', '`'

);
if($type == 'TODBC'){
return str_replace( $sbc, $dbc, $str ); //半角到全角
}elseif($type == 'TOSBC'){
return str_replace( $dbc, $sbc, $str ); //全角到半角
}else{
return $str;
}
}

Copy after login

方法四:

/**
* 将一个字串中含有全角的数字字符、字母、空格或'%+-()'字符转换为相应半角字符
*
* @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',
'(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[',
'】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']',
'‘' => '[', ''' => ']', '{' => '{', '}' => '}', '《' => '<',
'》' => '>',
'%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-',
':' => ':', '。' => '.', '、' => ',', ',' => '.', '、' => '.',
';' => ',', '?' => '&#63;', '!' => '!', '…' => '-', '‖' => '|',
'”' => '"', ''' => '`', '‘' => '`', '|' => '|', '〃' => '"',
' ' => ' ');
return strtr($str, $arr);
}
Copy after login

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

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

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

以上所述就是本文的全部内容了,希望大家能够喜欢。

Related labels:
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