1<?php
2
3namespace Vanderlee\Sentence;
4
5/**
6 * Multibyte-safe utility functions
7 */
8class 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        $offset_capture = (bool)($flags & PREG_SPLIT_OFFSET_CAPTURE);
75
76        $lengths = self::getSplitLengths($pattern, $string);
77
78        // Substrings
79        $parts = [];
80        $position = 0;
81        $count = 1;
82        foreach ($lengths as $length) {
83            if (self::isLastPart($length, $flags, $limit, $count)) {
84                $parts[] = self::makePart($string, $position, null, $offset_capture);
85                return $parts;
86            }
87
88            if (self::isPart($length, $flags)) {
89                $parts[] = self::makePart($string, $position, $length[0], $offset_capture);
90            }
91
92            $position += $length[0];
93        }
94
95        return $parts;
96    }
97
98    /**
99     * @param $length
100     * @param $flags
101     * @param $limit
102     * @param $count
103     * @return bool
104     */
105    private static function isLastPart($length, $flags, $limit, &$count)
106    {
107        $split_empty = !($flags & PREG_SPLIT_NO_EMPTY) || $length[0];
108        $is_delimiter = $length[1];
109
110        return $limit > 0
111            && !$is_delimiter
112            && $split_empty
113            && ++$count > $limit;
114    }
115
116    /**
117     * @param $length
118     * @param $flags
119     * @return bool
120     */
121    private static function isPart($length, $flags)
122    {
123        $split_empty = !($flags & PREG_SPLIT_NO_EMPTY) || $length[0];
124        $is_delimiter = $length[1];
125        $is_captured = ($flags & PREG_SPLIT_DELIM_CAPTURE) && $length[2];
126
127        return (!$is_delimiter
128                || $is_captured)
129            && $split_empty;
130    }
131
132    /**
133     * Make part
134     * @param string $string
135     * @param integer $position
136     * @param integer|null $length
137     * @param bool $offset_capture
138     * @return array|string
139     */
140    private static function makePart($string, $position, $length = null, $offset_capture = false)
141    {
142        $cut = mb_strcut($string, $position, $length);
143
144        return $offset_capture
145            ? [$cut, $position]
146            : $cut;
147    }
148
149    /**
150     * Splits the string by pattern and for each element (part or split) returns:
151     *  [ 0 => length, 1 => is_delimiter?, 2 =>
152     *
153     * @param $pattern
154     * @param $string
155     * @return array
156     */
157    private static function getSplitLengths($pattern, $string)
158    {
159        $strlen = strlen($string); // bytes!
160        $lengths = [];
161
162        mb_ereg_search_init($string);
163
164        $position = 0;
165        while ($position < $strlen
166            && ($array = mb_ereg_search_pos($pattern, '')) !== false) {
167            // capture split
168            $lengths[] = [$array[0] - $position, false, null];
169
170            // move position
171            $position = $array[0] + $array[1];
172
173            // capture delimiter
174            $regs = mb_ereg_search_getregs();
175            $lengths[] = [$array[1], true, isset($regs[1]) && $regs[1]];
176        }
177
178        // Add last bit, if not ending with split
179        $lengths[] = [$strlen - $position, false, null];
180
181        return $lengths;
182    }
183}
184