-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnicode.class.php
More file actions
55 lines (41 loc) · 1.23 KB
/
Unicode.class.php
File metadata and controls
55 lines (41 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
namespace Addcn\Model\Tools\Filter;
/**
*
* +------------------------------------------------
* unicode 變化轉換類
* +------------------------------------------------
* @author gaosongwang <songwanggao@gmail.com>
* +-------------------------------------------------
* @version 2016/6/2
* +-------------------------------------------------
*/
class Unicode {
/**
*
* 將字符轉為unicode碼
* @param unknown_type $str
* @param unknown_type $encoding
*/
public static function charCodeAt($str, $encoding = false ){
$encoding = $encoding ? $encoding : 'utf-8';
if(strlen($str) == 1) return ord($str);
$str = mb_substr($str, 0,1, $encoding);
$convert = mb_convert_encoding($str, 'UCS-4BE', $encoding);
$tmp = unpack('N', $convert);
return $tmp[1];
}
/**
*
* 將unicode碼轉為字符
* @param unknown_type $code
* @param unknown_type $encoding
*/
public static function fromCharCodeAt($code, $encoding = false){
$encoding = $encoding ? $encoding : 'utf-8';
if($code < 128) return chr($code);
$tmp = pack('N', $code);
$convert = mb_convert_encoding($tmp, $encoding, 'UCS-4BE');
return $convert;
}
}