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
|
|
|
$cut = mb_strcut($string, $position); |
95
|
|
|
|
96
|
|
|
$parts[] = $offset_capture |
97
|
|
|
? [$cut, $position] |
98
|
|
|
: $cut; |
99
|
|
|
|
100
|
|
|
break; |
101
|
|
|
} elseif ((!$is_delimiter |
102
|
|
|
|| $is_captured) |
103
|
|
|
&& $split_empty) { |
104
|
|
|
|
105
|
|
|
$cut = mb_strcut($string, $position, $length[0]); |
106
|
|
|
|
107
|
|
|
$parts[] = $offset_capture |
108
|
|
|
? [$cut, $position] |
109
|
|
|
: $cut; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$position += $length[0]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $parts; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Splits the string by pattern and for each element (part or split) returns: |
120
|
|
|
* [ 0 => length, 1 => is_delimiter?, 2 => |
121
|
|
|
* |
122
|
|
|
* @param $pattern |
123
|
|
|
* @param $string |
124
|
|
|
* @return array |
125
|
|
|
*/ |
126
|
|
|
private static function getSplitLengths($pattern, $string) |
127
|
|
|
{ |
128
|
|
|
$strlen = strlen($string); // bytes! |
129
|
|
|
$lengths = []; |
130
|
|
|
|
131
|
|
|
mb_ereg_search_init($string); |
132
|
|
|
|
133
|
|
|
$position = 0; |
134
|
|
|
while ($position < $strlen |
135
|
|
|
&& ($array = mb_ereg_search_pos($pattern, '')) !== false) { |
136
|
|
|
// capture split |
137
|
|
|
$lengths[] = [$array[0] - $position, false, null]; |
138
|
|
|
|
139
|
|
|
// move position |
140
|
|
|
$position = $array[0] + $array[1]; |
141
|
|
|
|
142
|
|
|
// capture delimiter |
143
|
|
|
$regs = mb_ereg_search_getregs(); |
144
|
|
|
$lengths[] = [$array[1], true, isset($regs[1]) && $regs[1]]; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// Add last bit, if not ending with split |
148
|
|
|
$lengths[] = [$strlen - $position, false, null]; |
149
|
|
|
|
150
|
|
|
return $lengths; |
151
|
|
|
} |
152
|
|
|
} |