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         $string = mb_ereg_replace('^\s*', '', $string); // left trim
60         $string = mb_ereg_replace('\s*$', '', $string); // right trim
61         return $string;
62     }
63 
64     /**
65      * A cross between mb_split and preg_split, adding the preg_split flags
66      * to mb_split.
67      *
68      * @param string $pattern
69      * @param string $string
70      * @param int $limit
71      * @param int $flags
72      * @return array
73      */
74     public static function split($pattern, $string, $limit = -1, $flags = 0)
75     {
76         $offset_capture = (bool)($flags & PREG_SPLIT_OFFSET_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             if (self::isLastPart($length, $flags, $limit, $count)) {
86                 $parts[] = self::makePart($string, $position, null, $offset_capture);
87                 return $parts;
88             }
89 
90             if (self::isPart($length, $flags)) {
91                 $parts[] = self::makePart($string, $position, $length[0], $offset_capture);
92             }
93 
94             $position += $length[0];
95         }
96 
97         return $parts;
98     }
99 
100     /**
101      * @param $length
102      * @param $flags
103      * @param $limit
104      * @param $count
105      * @return bool
106      */
107     private static function isLastPart($length, $flags, $limit, &$count)
108     {
109         $split_empty = !($flags & PREG_SPLIT_NO_EMPTY) || $length[0];
110         $is_delimiter = $length[1];
111 
112         return $limit > 0
113             && !$is_delimiter
114             && $split_empty
115             && ++$count > $limit;
116     }
117 
118     /**
119      * @param $length
120      * @param $flags
121      * @return bool
122      */
123     private static function isPart($length, $flags)
124     {
125         $split_empty = !($flags & PREG_SPLIT_NO_EMPTY) || $length[0];
126         $is_delimiter = $length[1];
127         $is_captured = ($flags & PREG_SPLIT_DELIM_CAPTURE) && $length[2];
128 
129         return (!$is_delimiter
130                 || $is_captured)
131             && $split_empty;
132     }
133 
134     /**
135      * Make part
136      * @param string $string
137      * @param integer $position
138      * @param integer|null $length
139      * @param bool $offset_capture
140      * @return array|string
141      */
142     private static function makePart($string, $position, $length = null, $offset_capture = false)
143     {
144         $cut = mb_strcut($string, $position, $length);
145 
146         return $offset_capture
147             ? [$cut, $position]
148             : $cut;
149     }
150 
151     /**
152      * Splits the string by pattern and for each element (part or split) returns:
153      *  [ 0 => length, 1 => is_delimiter?, 2 =>
154      *
155      * @param $pattern
156      * @param $string
157      * @return array
158      */
159     private static function getSplitLengths($pattern, $string)
160     {
161         $strlen = strlen($string); // bytes!
162         $lengths = [];
163 
164         mb_ereg_search_init($string);
165 
166         $position = 0;
167         while ($position < $strlen
168             && ($array = mb_ereg_search_pos($pattern, '')) !== false) {
169             // capture split
170             $lengths[] = [$array[0] - $position, false, null];
171 
172             // move position
173             $position = $array[0] + $array[1];
174 
175             // capture delimiter
176             $regs = mb_ereg_search_getregs();
177             $lengths[] = [$array[1], true, isset($regs[1]) && $regs[1]];
178         }
179 
180         // Add last bit, if not ending with split
181         $lengths[] = [$strlen - $position, false, null];
182 
183         return $lengths;
184     }
185 }
186