BaseStringHelper.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\helpers;
  8. use Yii;
  9. /**
  10. * BaseStringHelper provides concrete implementation for [[StringHelper]].
  11. *
  12. * Do not use BaseStringHelper. Use [[StringHelper]] instead.
  13. *
  14. * @author Qiang Xue <qiang.xue@gmail.com>
  15. * @author Alex Makarov <sam@rmcreative.ru>
  16. * @since 2.0
  17. */
  18. class BaseStringHelper
  19. {
  20. /**
  21. * Returns the number of bytes in the given string.
  22. * This method ensures the string is treated as a byte array by using `mb_strlen()`.
  23. * @param string $string the string being measured for length
  24. * @return int the number of bytes in the given string.
  25. */
  26. public static function byteLength($string)
  27. {
  28. return mb_strlen($string, '8bit');
  29. }
  30. /**
  31. * Returns the portion of string specified by the start and length parameters.
  32. * This method ensures the string is treated as a byte array by using `mb_substr()`.
  33. * @param string $string the input string. Must be one character or longer.
  34. * @param int $start the starting position
  35. * @param int $length the desired portion length. If not specified or `null`, there will be
  36. * no limit on length i.e. the output will be until the end of the string.
  37. * @return string the extracted part of string, or FALSE on failure or an empty string.
  38. * @see http://www.php.net/manual/en/function.substr.php
  39. */
  40. public static function byteSubstr($string, $start, $length = null)
  41. {
  42. return mb_substr($string, $start, $length === null ? mb_strlen($string, '8bit') : $length, '8bit');
  43. }
  44. /**
  45. * Returns the trailing name component of a path.
  46. * This method is similar to the php function `basename()` except that it will
  47. * treat both \ and / as directory separators, independent of the operating system.
  48. * This method was mainly created to work on php namespaces. When working with real
  49. * file paths, php's `basename()` should work fine for you.
  50. * Note: this method is not aware of the actual filesystem, or path components such as "..".
  51. *
  52. * @param string $path A path string.
  53. * @param string $suffix If the name component ends in suffix this will also be cut off.
  54. * @return string the trailing name component of the given path.
  55. * @see http://www.php.net/manual/en/function.basename.php
  56. */
  57. public static function basename($path, $suffix = '')
  58. {
  59. if (($len = mb_strlen($suffix)) > 0 && mb_substr($path, -$len) === $suffix) {
  60. $path = mb_substr($path, 0, -$len);
  61. }
  62. $path = rtrim(str_replace('\\', '/', $path), '/\\');
  63. if (($pos = mb_strrpos($path, '/')) !== false) {
  64. return mb_substr($path, $pos + 1);
  65. }
  66. return $path;
  67. }
  68. /**
  69. * Returns parent directory's path.
  70. * This method is similar to `dirname()` except that it will treat
  71. * both \ and / as directory separators, independent of the operating system.
  72. *
  73. * @param string $path A path string.
  74. * @return string the parent directory's path.
  75. * @see http://www.php.net/manual/en/function.basename.php
  76. */
  77. public static function dirname($path)
  78. {
  79. $pos = mb_strrpos(str_replace('\\', '/', $path), '/');
  80. if ($pos !== false) {
  81. return mb_substr($path, 0, $pos);
  82. } else {
  83. return '';
  84. }
  85. }
  86. /**
  87. * Truncates a string to the number of characters specified.
  88. *
  89. * @param string $string The string to truncate.
  90. * @param int $length How many characters from original string to include into truncated string.
  91. * @param string $suffix String to append to the end of truncated string.
  92. * @param string $encoding The charset to use, defaults to charset currently used by application.
  93. * @param bool $asHtml Whether to treat the string being truncated as HTML and preserve proper HTML tags.
  94. * This parameter is available since version 2.0.1.
  95. * @return string the truncated string.
  96. */
  97. public static function truncate($string, $length, $suffix = '...', $encoding = null, $asHtml = false)
  98. {
  99. if ($asHtml) {
  100. return static::truncateHtml($string, $length, $suffix, $encoding ?: Yii::$app->charset);
  101. }
  102. if (mb_strlen($string, $encoding ?: Yii::$app->charset) > $length) {
  103. return rtrim(mb_substr($string, 0, $length, $encoding ?: Yii::$app->charset)) . $suffix;
  104. } else {
  105. return $string;
  106. }
  107. }
  108. /**
  109. * Truncates a string to the number of words specified.
  110. *
  111. * @param string $string The string to truncate.
  112. * @param int $count How many words from original string to include into truncated string.
  113. * @param string $suffix String to append to the end of truncated string.
  114. * @param bool $asHtml Whether to treat the string being truncated as HTML and preserve proper HTML tags.
  115. * This parameter is available since version 2.0.1.
  116. * @return string the truncated string.
  117. */
  118. public static function truncateWords($string, $count, $suffix = '...', $asHtml = false)
  119. {
  120. if ($asHtml) {
  121. return static::truncateHtml($string, $count, $suffix);
  122. }
  123. $words = preg_split('/(\s+)/u', trim($string), null, PREG_SPLIT_DELIM_CAPTURE);
  124. if (count($words) / 2 > $count) {
  125. return implode('', array_slice($words, 0, ($count * 2) - 1)) . $suffix;
  126. } else {
  127. return $string;
  128. }
  129. }
  130. /**
  131. * Truncate a string while preserving the HTML.
  132. *
  133. * @param string $string The string to truncate
  134. * @param int $count
  135. * @param string $suffix String to append to the end of the truncated string.
  136. * @param string|bool $encoding
  137. * @return string
  138. * @since 2.0.1
  139. */
  140. protected static function truncateHtml($string, $count, $suffix, $encoding = false)
  141. {
  142. $config = \HTMLPurifier_Config::create(null);
  143. $config->set('Cache.SerializerPath', \Yii::$app->getRuntimePath());
  144. $lexer = \HTMLPurifier_Lexer::create($config);
  145. $tokens = $lexer->tokenizeHTML($string, $config, new \HTMLPurifier_Context());
  146. $openTokens = [];
  147. $totalCount = 0;
  148. $truncated = [];
  149. foreach ($tokens as $token) {
  150. if ($token instanceof \HTMLPurifier_Token_Start) { //Tag begins
  151. if ($totalCount < $count) {
  152. $openTokens[$token->name] = isset($openTokens[$token->name]) ? $openTokens[$token->name] + 1 : 1;
  153. $truncated[] = $token;
  154. }
  155. } elseif ($token instanceof \HTMLPurifier_Token_Text && $totalCount <= $count) { //Text
  156. if (false === $encoding) {
  157. preg_match('/^(\s*)/um', $token->data, $prefixSpace) ?: $prefixSpace = ['',''];
  158. $token->data = $prefixSpace[1] . self::truncateWords(ltrim($token->data), $count - $totalCount, '');
  159. $currentCount = self::countWords($token->data);
  160. } else {
  161. $token->data = self::truncate($token->data, $count - $totalCount, '', $encoding);
  162. $currentCount = mb_strlen($token->data, $encoding);
  163. }
  164. $totalCount += $currentCount;
  165. $truncated[] = $token;
  166. } elseif ($token instanceof \HTMLPurifier_Token_End) { //Tag ends
  167. if (!empty($openTokens[$token->name])) {
  168. $openTokens[$token->name]--;
  169. $truncated[] = $token;
  170. }
  171. } elseif ($token instanceof \HTMLPurifier_Token_Empty) { //Self contained tags, i.e. <img/> etc.
  172. $truncated[] = $token;
  173. }
  174. if (0 === $openTokens && $totalCount >= $count) {
  175. break;
  176. }
  177. }
  178. $context = new \HTMLPurifier_Context();
  179. $generator = new \HTMLPurifier_Generator($config, $context);
  180. return $generator->generateFromTokens($truncated) . ($totalCount >= $count ? $suffix : '');
  181. }
  182. /**
  183. * Check if given string starts with specified substring.
  184. * Binary and multibyte safe.
  185. *
  186. * @param string $string Input string
  187. * @param string $with Part to search inside the $string
  188. * @param bool $caseSensitive Case sensitive search. Default is true. When case sensitive is enabled, $with must exactly match the starting of the string in order to get a true value.
  189. * @return bool Returns true if first input starts with second input, false otherwise
  190. */
  191. public static function startsWith($string, $with, $caseSensitive = true)
  192. {
  193. if (!$bytes = static::byteLength($with)) {
  194. return true;
  195. }
  196. if ($caseSensitive) {
  197. return strncmp($string, $with, $bytes) === 0;
  198. } else {
  199. return mb_strtolower(mb_substr($string, 0, $bytes, '8bit'), Yii::$app->charset) === mb_strtolower($with, Yii::$app->charset);
  200. }
  201. }
  202. /**
  203. * Check if given string ends with specified substring.
  204. * Binary and multibyte safe.
  205. *
  206. * @param string $string Input string to check
  207. * @param string $with Part to search inside of the $string.
  208. * @param bool $caseSensitive Case sensitive search. Default is true. When case sensitive is enabled, $with must exactly match the ending of the string in order to get a true value.
  209. * @return bool Returns true if first input ends with second input, false otherwise
  210. */
  211. public static function endsWith($string, $with, $caseSensitive = true)
  212. {
  213. if (!$bytes = static::byteLength($with)) {
  214. return true;
  215. }
  216. if ($caseSensitive) {
  217. // Warning check, see http://php.net/manual/en/function.substr-compare.php#refsect1-function.substr-compare-returnvalues
  218. if (static::byteLength($string) < $bytes) {
  219. return false;
  220. }
  221. return substr_compare($string, $with, -$bytes, $bytes) === 0;
  222. } else {
  223. return mb_strtolower(mb_substr($string, -$bytes, mb_strlen($string, '8bit'), '8bit'), Yii::$app->charset) === mb_strtolower($with, Yii::$app->charset);
  224. }
  225. }
  226. /**
  227. * Explodes string into array, optionally trims values and skips empty ones
  228. *
  229. * @param string $string String to be exploded.
  230. * @param string $delimiter Delimiter. Default is ','.
  231. * @param mixed $trim Whether to trim each element. Can be:
  232. * - boolean - to trim normally;
  233. * - string - custom characters to trim. Will be passed as a second argument to `trim()` function.
  234. * - callable - will be called for each value instead of trim. Takes the only argument - value.
  235. * @param bool $skipEmpty Whether to skip empty strings between delimiters. Default is false.
  236. * @return array
  237. * @since 2.0.4
  238. */
  239. public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = false)
  240. {
  241. $result = explode($delimiter, $string);
  242. if ($trim) {
  243. if ($trim === true) {
  244. $trim = 'trim';
  245. } elseif (!is_callable($trim)) {
  246. $trim = function ($v) use ($trim) {
  247. return trim($v, $trim);
  248. };
  249. }
  250. $result = array_map($trim, $result);
  251. }
  252. if ($skipEmpty) {
  253. // Wrapped with array_values to make array keys sequential after empty values removing
  254. $result = array_values(array_filter($result, function ($value) {
  255. return $value !== '';
  256. }));
  257. }
  258. return $result;
  259. }
  260. /**
  261. * Counts words in a string
  262. * @since 2.0.8
  263. *
  264. * @param string $string
  265. * @return int
  266. */
  267. public static function countWords($string)
  268. {
  269. return count(preg_split('/\s+/u', $string, null, PREG_SPLIT_NO_EMPTY));
  270. }
  271. /**
  272. * Returns string represenation of number value with replaced commas to dots, if decimal point
  273. * of current locale is comma
  274. * @param int|float|string $value
  275. * @return string
  276. * @since 2.0.11
  277. */
  278. public static function normalizeNumber($value)
  279. {
  280. $value = "$value";
  281. $localeInfo = localeconv();
  282. $decimalSeparator = isset($localeInfo['decimal_point']) ? $localeInfo['decimal_point'] : null;
  283. if ($decimalSeparator !== null && $decimalSeparator !== '.') {
  284. $value = str_replace($decimalSeparator, '.', $value);
  285. }
  286. return $value;
  287. }
  288. }