xref: /plugin/aichat/vendor/vanderlee/php-sentence/src/Multibyte.php (revision 5d8f40498f9655eb57abe905d9f229b955d5b45e)
18817535bSAndreas Gohr<?php
28817535bSAndreas Gohr
38817535bSAndreas Gohrnamespace Vanderlee\Sentence;
48817535bSAndreas Gohr
58817535bSAndreas Gohr/**
68817535bSAndreas Gohr * Multibyte-safe utility functions
78817535bSAndreas Gohr */
88817535bSAndreas Gohrclass Multibyte
98817535bSAndreas Gohr{
108817535bSAndreas Gohr    //https://stackoverflow.com/questions/20025030/convert-all-types-of-smart-quotes-with-php
118817535bSAndreas Gohr    private static $unicodeCharacterMap = [
128817535bSAndreas Gohr        // Windows codepage 1252
138817535bSAndreas Gohr        "\xC2\x82" => "'", // U+0082⇒U+201A single low-9 quotation mark
148817535bSAndreas Gohr        "\xC2\x84" => '"', // U+0084⇒U+201E double low-9 quotation mark
158817535bSAndreas Gohr        "\xC2\x8B" => "'", // U+008B⇒U+2039 single left-pointing angle quotation mark
168817535bSAndreas Gohr        "\xC2\x91" => "'", // U+0091⇒U+2018 left single quotation mark
178817535bSAndreas Gohr        "\xC2\x92" => "'", // U+0092⇒U+2019 right single quotation mark
188817535bSAndreas Gohr        "\xC2\x93" => '"', // U+0093⇒U+201C left double quotation mark
198817535bSAndreas Gohr        "\xC2\x94" => '"', // U+0094⇒U+201D right double quotation mark
208817535bSAndreas Gohr        "\xC2\x9B" => "'", // U+009B⇒U+203A single right-pointing angle quotation mark
218817535bSAndreas Gohr        // Regular Unicode     // U+0022 quotation mark (")
228817535bSAndreas Gohr        // U+0027 apostrophe     (')
238817535bSAndreas Gohr        "\xC2\xAB" => '"', // U+00AB left-pointing double angle quotation mark
248817535bSAndreas Gohr        "\xC2\xBB" => '"', // U+00BB right-pointing double angle quotation mark
258817535bSAndreas Gohr        "\xE2\x80\x98" => "'", // U+2018 left single quotation mark
268817535bSAndreas Gohr        "\xE2\x80\x99" => "'", // U+2019 right single quotation mark
278817535bSAndreas Gohr        "\xE2\x80\x9A" => "'", // U+201A single low-9 quotation mark
288817535bSAndreas Gohr        "\xE2\x80\x9B" => "'", // U+201B single high-reversed-9 quotation mark
298817535bSAndreas Gohr        "\xE2\x80\x9C" => '"', // U+201C left double quotation mark
308817535bSAndreas Gohr        "\xE2\x80\x9D" => '"', // U+201D right double quotation mark
318817535bSAndreas Gohr        "\xE2\x80\x9E" => '"', // U+201E double low-9 quotation mark
328817535bSAndreas Gohr        "\xE2\x80\x9F" => '"', // U+201F double high-reversed-9 quotation mark
338817535bSAndreas Gohr        "\xE2\x80\xB9" => "'", // U+2039 single left-pointing angle quotation mark
348817535bSAndreas Gohr        "\xE2\x80\xBA" => "'", // U+203A single right-pointing angle quotation mark
358817535bSAndreas Gohr    ];
368817535bSAndreas Gohr
378817535bSAndreas Gohr    /**
388817535bSAndreas Gohr     * Replace
398817535bSAndreas Gohr     *
408817535bSAndreas Gohr     * @staticvar array $chr_map
418817535bSAndreas Gohr     * @param string $string
428817535bSAndreas Gohr     * @return string
438817535bSAndreas Gohr     */
448817535bSAndreas Gohr    public static function cleanUnicode($string)
458817535bSAndreas Gohr    {
468817535bSAndreas Gohr        $character = array_keys(self::$unicodeCharacterMap); // but: for efficiency you should
478817535bSAndreas Gohr        $replace = array_values(self::$unicodeCharacterMap); // pre-calculate these two arrays
488817535bSAndreas Gohr        return str_replace($character, $replace, html_entity_decode($string, ENT_QUOTES, "UTF-8"));
498817535bSAndreas Gohr    }
508817535bSAndreas Gohr
518817535bSAndreas Gohr    /**
528817535bSAndreas Gohr     * Multibyte.php safe version of standard trim() function.
538817535bSAndreas Gohr     *
548817535bSAndreas Gohr     * @param string $string
558817535bSAndreas Gohr     * @return string
568817535bSAndreas Gohr     */
578817535bSAndreas Gohr    public static function trim($string)
588817535bSAndreas Gohr    {
59*5d8f4049SAndreas Gohr        $string = mb_ereg_replace('^\s*', '', $string); // left trim
60*5d8f4049SAndreas Gohr        $string = mb_ereg_replace('\s*$', '', $string); // right trim
61*5d8f4049SAndreas Gohr        return $string;
628817535bSAndreas Gohr    }
638817535bSAndreas Gohr
648817535bSAndreas Gohr    /**
658817535bSAndreas Gohr     * A cross between mb_split and preg_split, adding the preg_split flags
668817535bSAndreas Gohr     * to mb_split.
678817535bSAndreas Gohr     *
688817535bSAndreas Gohr     * @param string $pattern
698817535bSAndreas Gohr     * @param string $string
708817535bSAndreas Gohr     * @param int $limit
718817535bSAndreas Gohr     * @param int $flags
728817535bSAndreas Gohr     * @return array
738817535bSAndreas Gohr     */
748817535bSAndreas Gohr    public static function split($pattern, $string, $limit = -1, $flags = 0)
758817535bSAndreas Gohr    {
768817535bSAndreas Gohr        $offset_capture = (bool)($flags & PREG_SPLIT_OFFSET_CAPTURE);
778817535bSAndreas Gohr
788817535bSAndreas Gohr        $lengths = self::getSplitLengths($pattern, $string);
798817535bSAndreas Gohr
808817535bSAndreas Gohr        // Substrings
818817535bSAndreas Gohr        $parts = [];
828817535bSAndreas Gohr        $position = 0;
838817535bSAndreas Gohr        $count = 1;
848817535bSAndreas Gohr        foreach ($lengths as $length) {
858817535bSAndreas Gohr            if (self::isLastPart($length, $flags, $limit, $count)) {
868817535bSAndreas Gohr                $parts[] = self::makePart($string, $position, null, $offset_capture);
878817535bSAndreas Gohr                return $parts;
888817535bSAndreas Gohr            }
898817535bSAndreas Gohr
908817535bSAndreas Gohr            if (self::isPart($length, $flags)) {
918817535bSAndreas Gohr                $parts[] = self::makePart($string, $position, $length[0], $offset_capture);
928817535bSAndreas Gohr            }
938817535bSAndreas Gohr
948817535bSAndreas Gohr            $position += $length[0];
958817535bSAndreas Gohr        }
968817535bSAndreas Gohr
978817535bSAndreas Gohr        return $parts;
988817535bSAndreas Gohr    }
998817535bSAndreas Gohr
1008817535bSAndreas Gohr    /**
1018817535bSAndreas Gohr     * @param $length
1028817535bSAndreas Gohr     * @param $flags
1038817535bSAndreas Gohr     * @param $limit
1048817535bSAndreas Gohr     * @param $count
1058817535bSAndreas Gohr     * @return bool
1068817535bSAndreas Gohr     */
1078817535bSAndreas Gohr    private static function isLastPart($length, $flags, $limit, &$count)
1088817535bSAndreas Gohr    {
1098817535bSAndreas Gohr        $split_empty = !($flags & PREG_SPLIT_NO_EMPTY) || $length[0];
1108817535bSAndreas Gohr        $is_delimiter = $length[1];
1118817535bSAndreas Gohr
1128817535bSAndreas Gohr        return $limit > 0
1138817535bSAndreas Gohr            && !$is_delimiter
1148817535bSAndreas Gohr            && $split_empty
1158817535bSAndreas Gohr            && ++$count > $limit;
1168817535bSAndreas Gohr    }
1178817535bSAndreas Gohr
1188817535bSAndreas Gohr    /**
1198817535bSAndreas Gohr     * @param $length
1208817535bSAndreas Gohr     * @param $flags
1218817535bSAndreas Gohr     * @return bool
1228817535bSAndreas Gohr     */
1238817535bSAndreas Gohr    private static function isPart($length, $flags)
1248817535bSAndreas Gohr    {
1258817535bSAndreas Gohr        $split_empty = !($flags & PREG_SPLIT_NO_EMPTY) || $length[0];
1268817535bSAndreas Gohr        $is_delimiter = $length[1];
1278817535bSAndreas Gohr        $is_captured = ($flags & PREG_SPLIT_DELIM_CAPTURE) && $length[2];
1288817535bSAndreas Gohr
1298817535bSAndreas Gohr        return (!$is_delimiter
1308817535bSAndreas Gohr                || $is_captured)
1318817535bSAndreas Gohr            && $split_empty;
1328817535bSAndreas Gohr    }
1338817535bSAndreas Gohr
1348817535bSAndreas Gohr    /**
1358817535bSAndreas Gohr     * Make part
1368817535bSAndreas Gohr     * @param string $string
1378817535bSAndreas Gohr     * @param integer $position
1388817535bSAndreas Gohr     * @param integer|null $length
1398817535bSAndreas Gohr     * @param bool $offset_capture
1408817535bSAndreas Gohr     * @return array|string
1418817535bSAndreas Gohr     */
1428817535bSAndreas Gohr    private static function makePart($string, $position, $length = null, $offset_capture = false)
1438817535bSAndreas Gohr    {
1448817535bSAndreas Gohr        $cut = mb_strcut($string, $position, $length);
1458817535bSAndreas Gohr
1468817535bSAndreas Gohr        return $offset_capture
1478817535bSAndreas Gohr            ? [$cut, $position]
1488817535bSAndreas Gohr            : $cut;
1498817535bSAndreas Gohr    }
1508817535bSAndreas Gohr
1518817535bSAndreas Gohr    /**
1528817535bSAndreas Gohr     * Splits the string by pattern and for each element (part or split) returns:
1538817535bSAndreas Gohr     *  [ 0 => length, 1 => is_delimiter?, 2 =>
1548817535bSAndreas Gohr     *
1558817535bSAndreas Gohr     * @param $pattern
1568817535bSAndreas Gohr     * @param $string
1578817535bSAndreas Gohr     * @return array
1588817535bSAndreas Gohr     */
1598817535bSAndreas Gohr    private static function getSplitLengths($pattern, $string)
1608817535bSAndreas Gohr    {
1618817535bSAndreas Gohr        $strlen = strlen($string); // bytes!
1628817535bSAndreas Gohr        $lengths = [];
1638817535bSAndreas Gohr
1648817535bSAndreas Gohr        mb_ereg_search_init($string);
1658817535bSAndreas Gohr
1668817535bSAndreas Gohr        $position = 0;
1678817535bSAndreas Gohr        while ($position < $strlen
1688817535bSAndreas Gohr            && ($array = mb_ereg_search_pos($pattern, '')) !== false) {
1698817535bSAndreas Gohr            // capture split
1708817535bSAndreas Gohr            $lengths[] = [$array[0] - $position, false, null];
1718817535bSAndreas Gohr
1728817535bSAndreas Gohr            // move position
1738817535bSAndreas Gohr            $position = $array[0] + $array[1];
1748817535bSAndreas Gohr
1758817535bSAndreas Gohr            // capture delimiter
1768817535bSAndreas Gohr            $regs = mb_ereg_search_getregs();
1778817535bSAndreas Gohr            $lengths[] = [$array[1], true, isset($regs[1]) && $regs[1]];
1788817535bSAndreas Gohr        }
1798817535bSAndreas Gohr
1808817535bSAndreas Gohr        // Add last bit, if not ending with split
1818817535bSAndreas Gohr        $lengths[] = [$strlen - $position, false, null];
1828817535bSAndreas Gohr
1838817535bSAndreas Gohr        return $lengths;
1848817535bSAndreas Gohr    }
1858817535bSAndreas Gohr}
186