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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
<?php namespace stringEncode;
class Encode {
/** * The encoding that the string is currently in. * * @var string */ protected $from; /** * The encoding that we would like the string to be in. * * @var string */ protected $to;
/** * Sets the default charsets for thie package. */ public function __construct() { // default from encoding $this->from = 'CP1252';
// default to encoding $this->to = 'UTF-8'; }
/** * Sets the charset that we will be converting to. * * @param string $charset * @chainable */ public function to($charset) { $this->to = strtoupper($charset); return $this; }
/** * Sets the charset that we will be converting from. * * @param string $charset * @chainable */ public function from($charset) { $this->from = strtoupper($charset); }
/** * Returns the to and from charset that we will be using. * * @return array */ public function charset() { return [ 'from' => $this->from, 'to' => $this->to, ]; }
/** * Attempts to detect the encoding of the given string from the encodingList. * * @param string $str * @param array $encodingList * @return bool */ public function detect($str, $encodingList = ['UTF-8', 'CP1252']) { $charset = mb_detect_encoding($str, $encodingList); if ($charset === false) { // could not detect charset return false; }
$this->from = $charset; return true; }
/** * Attempts to convert the string to the proper charset. * * @return string */ public function convert($str) { if ($this->from != $this->to) { $str = iconv($this->from, $this->to, $str); }
if ($str === false) { // the convertion was a failure throw new Exception('The convertion from "'.$this->from.'" to "'.$this->to.'" was a failure.'); }
// deal with BOM issue for utf-8 text if ($this->to == 'UTF-8') { if (substr($str, 0, 3) == "\xef\xbb\xbf") { $str = substr($str, 3); } if (substr($str, -3, 3) == "\xef\xbb\xbf") { $str = substr($str, 0, -3); } }
return $str; } }
|