|
- <?php
-
-
- namespace yii\helpers;
-
- use Yii;
-
-
- class BaseStringHelper
- {
-
-
- public static function byteLength($string)
- {
- return mb_strlen($string, '8bit');
- }
-
-
-
- public static function byteSubstr($string, $start, $length = null)
- {
- return mb_substr($string, $start, $length === null ? mb_strlen($string, '8bit') : $length, '8bit');
- }
-
-
-
- public static function basename($path, $suffix = '')
- {
- if (($len = mb_strlen($suffix)) > 0 && mb_substr($path, -$len) === $suffix) {
- $path = mb_substr($path, 0, -$len);
- }
- $path = rtrim(str_replace('\\', '/', $path), '/\\');
- if (($pos = mb_strrpos($path, '/')) !== false) {
- return mb_substr($path, $pos + 1);
- }
-
- return $path;
- }
-
-
-
- public static function dirname($path)
- {
- $pos = mb_strrpos(str_replace('\\', '/', $path), '/');
- if ($pos !== false) {
- return mb_substr($path, 0, $pos);
- } else {
- return '';
- }
- }
-
-
-
- public static function truncate($string, $length, $suffix = '...', $encoding = null, $asHtml = false)
- {
- if ($asHtml) {
- return static::truncateHtml($string, $length, $suffix, $encoding ?: Yii::$app->charset);
- }
-
- if (mb_strlen($string, $encoding ?: Yii::$app->charset) > $length) {
- return trim(mb_substr($string, 0, $length, $encoding ?: Yii::$app->charset)) . $suffix;
- } else {
- return $string;
- }
- }
-
-
-
- public static function truncateWords($string, $count, $suffix = '...', $asHtml = false)
- {
- if ($asHtml) {
- return static::truncateHtml($string, $count, $suffix);
- }
-
- $words = preg_split('/(\s+)/u', trim($string), null, PREG_SPLIT_DELIM_CAPTURE);
- if (count($words) / 2 > $count) {
- return implode('', array_slice($words, 0, ($count * 2) - 1)) . $suffix;
- } else {
- return $string;
- }
- }
-
-
-
- protected static function truncateHtml($string, $count, $suffix, $encoding = false)
- {
- $config = \HTMLPurifier_Config::create(null);
- $config->set('Cache.SerializerPath', \Yii::$app->getRuntimePath());
- $lexer = \HTMLPurifier_Lexer::create($config);
- $tokens = $lexer->tokenizeHTML($string, $config, null);
- $openTokens = 0;
- $totalCount = 0;
- $truncated = [];
- foreach ($tokens as $token) {
- if ($token instanceof \HTMLPurifier_Token_Start) {
- $openTokens++;
- $truncated[] = $token;
- } elseif ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) {
- if (false === $encoding) {
- $token->data = self::truncateWords($token->data, $count - $totalCount, '');
- $currentCount = self::countWords($token->data);
- } else {
- $token->data = self::truncate($token->data, $count - $totalCount, '', $encoding) . ' ';
- $currentCount = mb_strlen($token->data, $encoding);
- }
- $totalCount += $currentCount;
- if (1 === $currentCount) {
- $token->data = ' ' . $token->data;
- }
- $truncated[] = $token;
- } elseif ($token instanceof \HTMLPurifier_Token_End) {
- $openTokens--;
- $truncated[] = $token;
- } elseif ($token instanceof \HTMLPurifier_Token_Empty) {
- $truncated[] = $token;
- }
- if (0 === $openTokens && $totalCount >= $count) {
- break;
- }
- }
- $context = new \HTMLPurifier_Context();
- $generator = new \HTMLPurifier_Generator($config, $context);
- return $generator->generateFromTokens($truncated) . ($totalCount >= $count ? $suffix : '');
- }
-
-
-
- public static function startsWith($string, $with, $caseSensitive = true)
- {
- if (!$bytes = static::byteLength($with)) {
- return true;
- }
- if ($caseSensitive) {
- return strncmp($string, $with, $bytes) === 0;
- } else {
- return mb_strtolower(mb_substr($string, 0, $bytes, '8bit'), Yii::$app->charset) === mb_strtolower($with, Yii::$app->charset);
- }
- }
-
-
-
- public static function endsWith($string, $with, $caseSensitive = true)
- {
- if (!$bytes = static::byteLength($with)) {
- return true;
- }
- if ($caseSensitive) {
-
- if (static::byteLength($string) < $bytes) {
- return false;
- }
- return substr_compare($string, $with, -$bytes, $bytes) === 0;
- } else {
- return mb_strtolower(mb_substr($string, -$bytes, mb_strlen($string, '8bit'), '8bit'), Yii::$app->charset) === mb_strtolower($with, Yii::$app->charset);
- }
- }
-
-
-
- public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = false)
- {
- $result = explode($delimiter, $string);
- if ($trim) {
- if ($trim === true) {
- $trim = 'trim';
- } elseif (!is_callable($trim)) {
- $trim = function ($v) use ($trim) {
- return trim($v, $trim);
- };
- }
- $result = array_map($trim, $result);
- }
- if ($skipEmpty) {
-
- $result = array_values(array_filter($result, function ($value) {
- return $value !== '';
- }));
- }
- return $result;
- }
-
-
-
- public static function countWords($string)
- {
- return count(preg_split('/\s+/u', $string, null, PREG_SPLIT_NO_EMPTY));
- }
- }
|