php字符串处理之全角半角转换

半角全角的处理是字符串处理的常见问题,本文尝试为大家提供一个思路。

一、概念

全角字符unicode编码从65281~65374 (十六进制 0xFF01 ~ 0xFF5E)
半角字符unicode编码从33~126 (十六进制 0x21~ 0x7E)
空格比较特殊,全角为 12288(0x3000),半角为 32 (0x20)
而且除空格外,全角/半角按unicode编码排序在顺序上是对应的
所以可以直接通过用+-法来处理非空格数据,对空格单独处理

二、实现思路

1. 找到目标unicode的字符,可以使用正则表达式解决

2. 修改unicode编码

三、实现

1. 首先是两个unicode与字符的转换函数:

 1     /**
 2      * 将unicode转换成字符
 3      * @param int $unicode
 4      * @return string UTF-8字符
 5      **/
 6     function unicode2Char($unicode){
 7         if($unicode < 128)     return chr($unicode);
 8         if($unicode < 2048)    return chr(($unicode >> 6) + 192) .
 9                                       chr(($unicode & 63) + 128);
10         if($unicode < 65536)   return chr(($unicode >> 12) + 224) .
11                                       chr((($unicode >> 6) & 63) + 128) .
12                                       chr(($unicode & 63) + 128);
13         if($unicode < 2097152) return chr(($unicode >> 18) + 240) .
14                                       chr((($unicode >> 12) & 63) + 128) .
15                                       chr((($unicode >> 6) & 63) + 128) .
16                                       chr(($unicode & 63) + 128);
17         return false;
18     }
19  
20     /**
21      * 将字符转换成unicode
22      * @param string $char 必须是UTF-8字符
23      * @return int
24      **/
25     function char2Unicode($char){
26         switch (strlen($char)){
27             case 1 : return ord($char);
28             case 2 : return (ord($char{1}) & 63) |
29                             ((ord($char{0}) & 31) << 6);
30             case 3 : return (ord($char{2}) & 63) |
31                             ((ord($char{1}) & 63) << 6) |
32                             ((ord($char{0}) & 15) << 12);
33             case 4 : return (ord($char{3}) & 63) |
34                             ((ord($char{2}) & 63) << 6) |
35                             ((ord($char{1}) & 63) << 12) |
36                             ((ord($char{0}) & 7)  << 18);
37             default :
38                 trigger_error('Character is not UTF-8!', E_USER_WARNING);
39                 return false;
40         }
41     }

  2. 全角转半角

 1     /**
 2      * 全角转半角
 3      * @param string $str
 4      * @return string
 5      **/
 6     function sbc2Dbc($str){
 7         return preg_replace(
 8             // 全角字符 
 9             '/[\x{3000}\x{ff01}-\x{ff5f}]/ue',
10             // 编码转换
11             // 0x3000是空格,特殊处理,其他全角字符编码-0xfee0即可以转为半角
12             '($unicode=char2Unicode(\'\0\')) == 0x3000 ? " " : (($code=$unicode-0xfee0) > 256 ? unicode2Char($code) : chr($code))',
13             $str
14         );
15     }

3. 半角转全角

 1     /**
 2      * 半角转全角
 3      * @param string $str
 4      * @return string
 5      **/
 6     function dbc2Sbc($str){
 7         return preg_replace(
 8             // 半角字符 
 9             '/[\x{0020}\x{0020}-\x{7e}]/ue',  
10             // 编码转换
11             // 0x0020是空格,特殊处理,其他半角字符编码+0xfee0即可以转为全角
12             '($unicode=char2Unicode(\'\0\')) == 0x0020 ? unicode2Char(0x3000) : (($code=$unicode+0xfee0) > 256 ? unicode2Char($code) : chr($code))',
13             $str
14         );
15     }

四、测试

 示例代码:

1 $a = 'abc12 345';
2 $sbc = dbc2Sbc($a);
3 $dbc = sbc2Dbc($sbc);
4 
5 var_dump($a, $sbc, $dbc);

结果:

1 string(9) "abc12 345"
2 string(27) "abc12 345"
3 string(9) "abc12 345"

 

posted @ 2016-02-24 19:10  南十字  阅读(6977)  评论(0编辑  收藏  举报