|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vanderlee\Sentence; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Multibyte-safe utility functions |
|
7
|
|
|
*/ |
|
8
|
|
|
class Multibyte |
|
9
|
|
|
{ |
|
10
|
|
|
//https://stackoverflow.com/questions/20025030/convert-all-types-of-smart-quotes-with-php |
|
11
|
|
|
private static $unicodeCharacterMap = [ |
|
12
|
|
|
// Windows codepage 1252 |
|
13
|
|
|
"\xC2\x82" => "'", // U+0082⇒U+201A single low-9 quotation mark |
|
14
|
|
|
"\xC2\x84" => '"', // U+0084⇒U+201E double low-9 quotation mark |
|
15
|
|
|
"\xC2\x8B" => "'", // U+008B⇒U+2039 single left-pointing angle quotation mark |
|
16
|
|
|
"\xC2\x91" => "'", // U+0091⇒U+2018 left single quotation mark |
|
17
|
|
|
"\xC2\x92" => "'", // U+0092⇒U+2019 right single quotation mark |
|
18
|
|
|
"\xC2\x93" => '"', // U+0093⇒U+201C left double quotation mark |
|
19
|
|
|
"\xC2\x94" => '"', // U+0094⇒U+201D right double quotation mark |
|
20
|
|
|
"\xC2\x9B" => "'", // U+009B⇒U+203A single right-pointing angle quotation mark |
|
21
|
|
|
// Regular Unicode // U+0022 quotation mark (") |
|
22
|
|
|
// U+0027 apostrophe (') |
|
23
|
|
|
"\xC2\xAB" => '"', // U+00AB left-pointing double angle quotation mark |
|
24
|
|
|
"\xC2\xBB" => '"', // U+00BB right-pointing double angle quotation mark |
|
25
|
|
|
"\xE2\x80\x98" => "'", // U+2018 left single quotation mark |
|
26
|
|
|
"\xE2\x80\x99" => "'", // U+2019 right single quotation mark |
|
27
|
|
|
"\xE2\x80\x9A" => "'", // U+201A single low-9 quotation mark |
|
28
|
|
|
"\xE2\x80\x9B" => "'", // U+201B single high-reversed-9 quotation mark |
|
29
|
|
|
"\xE2\x80\x9C" => '"', // U+201C left double quotation mark |
|
30
|
|
|
"\xE2\x80\x9D" => '"', // U+201D right double quotation mark |
|
31
|
|
|
"\xE2\x80\x9E" => '"', // U+201E double low-9 quotation mark |
|
32
|
|
|
"\xE2\x80\x9F" => '"', // U+201F double high-reversed-9 quotation mark |
|
33
|
|
|
"\xE2\x80\xB9" => "'", // U+2039 single left-pointing angle quotation mark |
|
34
|
|
|
"\xE2\x80\xBA" => "'", // U+203A single right-pointing angle quotation mark |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Replace |
|
39
|
|
|
* |
|
40
|
|
|
* @staticvar array $chr_map |
|
41
|
|
|
* @param string $string |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function cleanUnicode($string) |
|
45
|
|
|
{ |
|
46
|
|
|
$character = array_keys(self::$unicodeCharacterMap); // but: for efficiency you should |
|
47
|
|
|
$replace = array_values(self::$unicodeCharacterMap); // pre-calculate these two arrays |
|
48
|
|
|
return str_replace($character, $replace, html_entity_decode($string, ENT_QUOTES, "UTF-8")); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Multibyte.php safe version of standard trim() function. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $string |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function trim($string) |
|
58
|
|
|
{ |
|
59
|
|
|
return mb_ereg_replace('^\s*([\s\S]*?)\s*$', '\1', $string); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* A cross between mb_split and preg_split, adding the preg_split flags |
|
64
|
|
|
* to mb_split. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $pattern |
|
67
|
|
|
* @param string $string |
|
68
|
|
|
* @param int $limit |
|
69
|
|
|
* @param int $flags |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function split($pattern, $string, $limit = -1, $flags = 0) |
|
73
|
|
|
{ |
|
74
|
|
|
$split_no_empty = (bool)($flags & PREG_SPLIT_NO_EMPTY); |
|
75
|
|
|
$offset_capture = (bool)($flags & PREG_SPLIT_OFFSET_CAPTURE); |
|
76
|
|
|
$delim_capture = (bool)($flags & PREG_SPLIT_DELIM_CAPTURE); |
|
77
|
|
|
|
|
78
|
|
|
$lengths = self::getSplitLengths($pattern, $string); |
|
79
|
|
|
|
|
80
|
|
|
// Substrings |
|
81
|
|
|
$parts = []; |
|
82
|
|
|
$position = 0; |
|
83
|
|
|
$count = 1; |
|
84
|
|
|
foreach ($lengths as $length) { |
|
85
|
|
|
$split_empty = !$split_no_empty || $length[0]; |
|
86
|
|
|
$is_delimiter = $length[1]; |
|
87
|
|
|
$is_captured = $delim_capture && $length[2]; |
|
88
|
|
|
|
|
89
|
|
|
if ($limit > 0 |
|
90
|
|
|
&& !$is_delimiter |
|
91
|
|
|
&& $split_empty |
|
92
|
|
|
&& ++$count > $limit) { |
|
93
|
|
|
|
|
94
|
|
|
$parts[] = self::makePart($string, $position, null, $offset_capture); |
|
95
|
|
|
return $parts; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if ((!$is_delimiter |
|
99
|
|
|
|| $is_captured) |
|
100
|
|
|
&& $split_empty) { |
|
101
|
|
|
|
|
102
|
|
|
$parts[] = self::makePart($string, $position, $length[0], $offset_capture); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$position += $length[0]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $parts; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Make part |
|
113
|
|
|
* @param string $string |
|
114
|
|
|
* @param integer $position |
|
115
|
|
|
* @param integer|null $length |
|
116
|
|
|
* @param bool $offset_capture |
|
117
|
|
|
* @return array|string |
|
118
|
|
|
*/ |
|
119
|
|
|
private static function makePart($string, $position, $length = null, $offset_capture = false) |
|
120
|
|
|
{ |
|
121
|
|
|
$cut = mb_strcut($string, $position, $length); |
|
122
|
|
|
|
|
123
|
|
|
return $offset_capture |
|
124
|
|
|
? [$cut, $position] |
|
125
|
|
|
: $cut; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Splits the string by pattern and for each element (part or split) returns: |
|
130
|
|
|
* [ 0 => length, 1 => is_delimiter?, 2 => |
|
131
|
|
|
* |
|
132
|
|
|
* @param $pattern |
|
133
|
|
|
* @param $string |
|
134
|
|
|
* @return array |
|
135
|
|
|
*/ |
|
136
|
|
|
private static function getSplitLengths($pattern, $string) |
|
137
|
|
|
{ |
|
138
|
|
|
$strlen = strlen($string); // bytes! |
|
139
|
|
|
$lengths = []; |
|
140
|
|
|
|
|
141
|
|
|
mb_ereg_search_init($string); |
|
142
|
|
|
|
|
143
|
|
|
$position = 0; |
|
144
|
|
|
while ($position < $strlen |
|
145
|
|
|
&& ($array = mb_ereg_search_pos($pattern, '')) !== false) { |
|
146
|
|
|
// capture split |
|
147
|
|
|
$lengths[] = [$array[0] - $position, false, null]; |
|
148
|
|
|
|
|
149
|
|
|
// move position |
|
150
|
|
|
$position = $array[0] + $array[1]; |
|
151
|
|
|
|
|
152
|
|
|
// capture delimiter |
|
153
|
|
|
$regs = mb_ereg_search_getregs(); |
|
154
|
|
|
$lengths[] = [$array[1], true, isset($regs[1]) && $regs[1]]; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
// Add last bit, if not ending with split |
|
158
|
|
|
$lengths[] = [$strlen - $position, false, null]; |
|
159
|
|
|
|
|
160
|
|
|
return $lengths; |
|
161
|
|
|
} |
|
162
|
|
|
} |