/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
common/helpers/Utf8Helper.php
614 строк
19 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace common\helpers; use Yii; use yii\base\InvalidCallException; /** * Набор вспомогательных функция для работы с текстом * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2019-2021 */ class Utf8Helper { /** * @var boolean Does the server support UTF-8 natively? */ public static $server_utf8 = null; /** * @var array List of called methods that have had their required file included. */ public static $called = array(); /** * Tests whether a string contains only 7-bit ASCII bytes. This is used to * determine when to use native functions or UTF-8 functions. * * $ascii = Utf8Helper::isAscii($str); * * @param mixed $str string or array of strings to check * @return boolean */ public static function isAscii($str) { if (is_array($str)) { $str = implode($str); } return !preg_match('/[^\x00-\x7F]/S', $str); } /** * Strips out device control codes in the ASCII range. * * $str = Utf8Helper::stripAsciiCtrl($str); * * @param string $str string to clean * @return string */ public static function stripAsciiCtrl($str) { return preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S', '', $str); } /** * Strips out all non-7bit ASCII bytes. * * $str = Utf8Helper::stripNonAscii($str); * * @param string $str string to clean * @return string */ public static function stripNonAscii($str) { return preg_replace('/[^\x00-\x7F]+/S', '', $str); } /** * Подключение реализации функции * @param $function */ private static function requireFile($function) { if (!isset(self::$called[$function])) { $path = Yii::getAlias('@common/helpers/utf8/' . $function . '.php'); if (is_file($path)) { require_once $path; // Function has been called self::$called[$function] = true; } else { throw new InvalidCallException('Реализация "' . $function . '" не найдена.'); } } } /** * Returns the length of the given string. This is a UTF8-aware version * of [strlen](http://php.net/strlen). * * $length = Utf8Helper::strlen($str); * * @param string $str string being measured for length * @return integer * @uses Utf8Helper::$server_utf8 */ public static function strlen($str) { if (Utf8Helper::$server_utf8) { return mb_strlen($str, Yii::$app->charset); } self::requireFile(__FUNCTION__); return \_strlen($str); } /** * Returns part of a UTF-8 string. This is a UTF8-aware version * of [substr](http://php.net/substr). * * $sub = Utf8Helper::substr($str, $offset); * * @param string $str input string * @param integer $offset offset * @param integer $length length limit * @return string * @author Chris Smith <chris@jalakai.co.uk> * @uses Utf8Helper::$server_utf8 * @uses Yii::$app->charset */ public static function substr($str, $offset, $length = null) { if (Utf8Helper::$server_utf8) { return ($length === null) ? mb_substr($str, $offset, mb_strlen($str), Yii::$app->charset) : mb_substr($str, $offset, $length, Yii::$app->charset); } self::requireFile(__FUNCTION__); return \_substr($str, $offset, $length); } /** * Finds position of first occurrence of a UTF-8 string. This is a * UTF8-aware version of [strpos](http://php.net/strpos). * * $position = UTF8::strpos($str, $search); * * @param string $str haystack * @param string $search needle * @param integer $offset offset from which character in haystack to start searching * @return integer position of needle * @return boolean FALSE if the needle is not found * @author Harry Fuecks <hfuecks@gmail.com> * @uses Utf8Helper::$server_utf8 * @uses Yii::$app->charset */ public static function strpos($str, $search, $offset = 0) { if (Utf8Helper::$server_utf8) { return mb_strpos($str, $search, $offset, Yii::$app->charset); } self::requireFile(__FUNCTION__); return \_strpos($str, $search, $offset); } /** * Finds position of last occurrence of a char in a UTF-8 string. This is * a UTF8-aware version of [strrpos](http://php.net/strrpos). * * $position = Utf8Helper::strrpos($str, $search); * * @param string $str haystack * @param string $search needle * @param integer $offset offset from which character in haystack to start searching * @return integer position of needle * @return boolean FALSE if the needle is not found * @author Harry Fuecks <hfuecks@gmail.com> * @uses Utf8Helper::$server_utf8 */ public static function strrpos($str, $search, $offset = 0) { if (Utf8Helper::$server_utf8) { return mb_strrpos($str, $search, $offset, Yii::$app->charset); } self::requireFile(__FUNCTION__); return \_strrpos($str, $search, $offset); } /** * Replaces text within a portion of a UTF-8 string. This is a UTF8-aware * version of [substr_replace](http://php.net/substr_replace). * * $str = Utf8Helper::substr_replace($str, $replacement, $offset); * * @param string $str input string * @param string $replacement replacement string * @param integer $offset offset * @return string * @author Harry Fuecks <hfuecks@gmail.com> */ public static function substr_replace($str, $replacement, $offset, $length = null) { self::requireFile(__FUNCTION__); return \_substr_replace($str, $replacement, $offset, $length); } /** * Makes a UTF-8 string lowercase. This is a UTF8-aware version * of [strtolower](http://php.net/strtolower). * * $str = Utf8Helper::strtolower($str); * * @param string $str mixed case string * @return string * @author Andreas Gohr <andi@splitbrain.org> * @uses Utf8Helper::$server_utf8 * @uses Yii::$app->charset */ public static function strtolower($str) { if (Utf8Helper::$server_utf8) { return mb_strtolower($str, Yii::$app->charset); } self::requireFile(__FUNCTION__); return \_strtolower($str); } /** * Makes a UTF-8 string uppercase. This is a UTF8-aware version * of [strtoupper](http://php.net/strtoupper). * * @param string $str mixed case string * @return string * @author Andreas Gohr <andi@splitbrain.org> * @uses Utf8Helper::$server_utf8 * @uses Yii::$app->charset */ public static function strtoupper($str) { if (Utf8Helper::$server_utf8) { return mb_strtoupper($str, Yii::$app->charset); } self::requireFile(__FUNCTION__); return \_strtoupper($str); } /** * Makes a UTF-8 string's first character uppercase. This is a UTF8-aware * version of [ucfirst](http://php.net/ucfirst). * * $str = Utf8Helper::ucfirst($str); * * @param string $str mixed case string * @return string * @author Harry Fuecks <hfuecks@gmail.com> */ public static function ucfirst($str) { self::requireFile(__FUNCTION__); return _ucfirst($str); } /** * Makes a UTF-8 string's first character lowercase. This is a UTF8-aware * version of [lcfirst](http://php.net/lcfirst). * * $str = Utf8Helper::lcfirst($str); * * @param string $str mixed case string * @return string */ public static function lcfirst($str) { self::requireFile(__FUNCTION__); return _lcfirst($str); } /** * Makes the first character of every word in a UTF-8 string uppercase. * This is a UTF8-aware version of [ucwords](http://php.net/ucwords). * * $str = Utf8Helper::ucwords($str); * * @param string $str mixed case string * @return string * @author Harry Fuecks <hfuecks@gmail.com> */ public static function ucwords($str) { self::requireFile(__FUNCTION__); return \_ucwords($str); } /** * Case-insensitive UTF-8 string comparison. This is a UTF8-aware version * of [strcasecmp](http://php.net/strcasecmp). * * $compare = Utf8Helper::strcasecmp($str1, $str2); * * @param string $str1 string to compare * @param string $str2 string to compare * @return integer less than 0 if str1 is less than str2 * @return integer greater than 0 if str1 is greater than str2 * @return integer 0 if they are equal * @author Harry Fuecks <hfuecks@gmail.com> */ public static function strcasecmp($str1, $str2) { self::requireFile(__FUNCTION__); return \_strcasecmp($str1, $str2); } /** * Returns a string or an array with all occurrences of search in subject * (ignoring case) and replaced with the given replace value. This is a * UTF8-aware version of [str_ireplace](http://php.net/str_ireplace). * * [!!] This function is very slow compared to the native version. Avoid * using it when possible. * * @param string|array $search text to replace * @param string|array $replace replacement text * @param string|array $str subject text * @param integer $count number of matched and replaced needles will be returned via this parameter which is passed by reference * @return string if the input was a string * @return array if the input was an array * @author Harry Fuecks <hfuecks@gmail.com */ public static function str_ireplace($search, $replace, $str, &$count = null) { self::requireFile(__FUNCTION__); return \_str_ireplace($search, $replace, $str, $count); } /** * Case-insensitive UTF-8 version of strstr. Returns all of input string * from the first occurrence of needle to the end. This is a UTF8-aware * version of [stristr](http://php.net/stristr). * * $found = Utf8Helper::stristr($str, $search); * * @param string $str input string * @param string $search needle * @return string matched substring if found * @return FALSE if the substring was not found * @author Harry Fuecks <hfuecks@gmail.com> */ public static function stristr($str, $search) { self::requireFile(__FUNCTION__); return \_stristr($str, $search); } /** * Finds the length of the initial segment matching mask. This is a * UTF8-aware version of [strspn](http://php.net/strspn). * * $found = Utf8Helper::strspn($str, $mask); * * @param string $str input string * @param string $mask mask for search * @param integer $offset start position of the string to examine * @param integer $length length of the string to examine * @return integer length of the initial segment that contains characters in the mask * @author Harry Fuecks <hfuecks@gmail.com> */ public static function strspn($str, $mask, $offset = null, $length = null) { self::requireFile(__FUNCTION__); return \_strspn($str, $mask, $offset, $length); } /** * Finds the length of the initial segment not matching mask. This is a * UTF8-aware version of [strcspn](http://php.net/strcspn). * * $found = Utf8Helper::strcspn($str, $mask); * * @param string $str input string * @param string $mask mask for search * @param integer $offset start position of the string to examine * @param integer $length length of the string to examine * @return integer length of the initial segment that contains characters not in the mask * @author Harry Fuecks <hfuecks@gmail.com> */ public static function strcspn($str, $mask, $offset = null, $length = null) { self::requireFile(__FUNCTION__); return \_strcspn($str, $mask, $offset, $length); } /** * Pads a UTF-8 string to a certain length with another string. This is a * UTF8-aware version of [str_pad](http://php.net/str_pad). * * $str = Utf8Helper::str_pad($str, $length); * * @param string $str input string * @param integer $final_str_length desired string length after padding * @param string $pad_str string to use as padding * @param string $pad_type padding type: STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH * @return string * @author Harry Fuecks <hfuecks@gmail.com> */ public static function str_pad($str, $final_str_length, $pad_str = ' ', $pad_type = STR_PAD_RIGHT) { self::requireFile(__FUNCTION__); return \_str_pad($str, $final_str_length, $pad_str, $pad_type); } /** * Converts a UTF-8 string to an array. This is a UTF8-aware version of * [str_split](http://php.net/str_split). * * $array = Utf8Helper::str_split($str); * * @param string $str input string * @param integer $split_length maximum length of each chunk * @return array * @author Harry Fuecks <hfuecks@gmail.com> */ public static function str_split($str, $split_length = 1) { self::requireFile(__FUNCTION__); return \_str_split($str, $split_length); } /** * Reverses a UTF-8 string. This is a UTF8-aware version of [strrev](http://php.net/strrev). * * $str = Utf8Helper::strrev($str); * * @param string $str string to be reversed * @return string * @author Harry Fuecks <hfuecks@gmail.com> */ public static function strrev($str) { self::requireFile(__FUNCTION__); return \_strrev($str); } /** * Strips whitespace (or other UTF-8 characters) from the beginning and * end of a string. This is a UTF8-aware version of [trim](http://php.net/trim). * * $str = Utf8Helper::trim($str); * * @param string $str input string * @param string $charlist string of characters to remove * @return string * @author Andreas Gohr <andi@splitbrain.org> */ public static function trim($str, $charlist = null) { self::requireFile(__FUNCTION__); return \_trim($str, $charlist); } /** * Strips whitespace (or other UTF-8 characters) from the beginning of * a string. This is a UTF8-aware version of [ltrim](http://php.net/ltrim). * * $str = Utf8Helper::ltrim($str); * * @param string $str input string * @param string $charlist string of characters to remove * @return string * @author Andreas Gohr <andi@splitbrain.org> */ public static function ltrim($str, $charlist = null) { self::requireFile(__FUNCTION__); return \_ltrim($str, $charlist); } /** * Strips whitespace (or other UTF-8 characters) from the end of a string. * This is a UTF8-aware version of [rtrim](http://php.net/rtrim). * * $str = Utf8Helper::rtrim($str); * * @param string $str input string * @param string $charlist string of characters to remove * @return string * @author Andreas Gohr <andi@splitbrain.org> */ public static function rtrim($str, $charlist = null) { self::requireFile(__FUNCTION__); return \_rtrim($str, $charlist); } /** * Returns the unicode ordinal for a character. This is a UTF8-aware * version of [ord](http://php.net/ord). * * $digit = Utf8Helper::ord($character); * * @param string $chr UTF-8 encoded character * @return integer * @author Harry Fuecks <hfuecks@gmail.com> */ public static function ord($chr) { self::requireFile(__FUNCTION__); return \_ord($chr); } /** * Takes an UTF-8 string and returns an array of ints representing the Unicode characters. * Astral planes are supported i.e. the ints in the output can be > 0xFFFF. * Occurrences of the BOM are ignored. Surrogates are not allowed. * * $array = Utf8Helper::to_unicode($str); * * The Original Code is Mozilla Communicator client code. * The Initial Developer of the Original Code is Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 1998 the Initial Developer. * Ported to PHP by Henri Sivonen <hsivonen@iki.fi>, see <http://hsivonen.iki.fi/php-utf8/> * Slight modifications to fit with phputf8 library by Harry Fuecks <hfuecks@gmail.com> * * @param string $str UTF-8 encoded string * @return array unicode code points * @return FALSE if the string is invalid */ public static function to_unicode($str) { self::requireFile(__FUNCTION__); return \_to_unicode($str); } /** * Takes an array of ints representing the Unicode characters and returns a UTF-8 string. * Astral planes are supported i.e. the ints in the input can be > 0xFFFF. * Occurrences of the BOM are ignored. Surrogates are not allowed. * * $str = Utf8Helper::to_unicode($array); * * The Original Code is Mozilla Communicator client code. * The Initial Developer of the Original Code is Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 1998 the Initial Developer. * Ported to PHP by Henri Sivonen <hsivonen@iki.fi>, see http://hsivonen.iki.fi/php-utf8/ * Slight modifications to fit with phputf8 library by Harry Fuecks <hfuecks@gmail.com>. * * @param array $str unicode code points representing a string * @return string utf8 string of characters * @return boolean FALSE if a code point cannot be found */ public static function from_unicode($arr) { self::requireFile(__FUNCTION__); return \_from_unicode($arr); } /** * функция проверять - строка в формате UTF-8 или нет * @param $string * @return bool */ public static function isUtf8($string) { // From http://w3.org/International/questions/qa-forms-utf-8.html return preg_match('%^(?: [\x09\x0A\x0D\x20-\x7E] # ASCII | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 )*$%xs', $string) !== false; } // /** * Удалить BOM из начала строки * @param $str * @return bool|string */ public static function removeBOM($str) { if (substr($str, 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) { $str = substr($str, 3); } return $str; } } if (Utf8Helper::$server_utf8 === null) { // Determine if this server supports UTF-8 natively Utf8Helper::$server_utf8 = extension_loaded('mbstring'); }