123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- <?php namespace Laravel;
- class Str {
-
- public static $pluralizer;
-
- public static function encoding()
- {
- return Config::get('application.encoding');
- }
-
- public static function length($value)
- {
- return (MB_STRING) ? mb_strlen($value, static::encoding()) : strlen($value);
- }
-
- public static function lower($value)
- {
- return (MB_STRING) ? mb_strtolower($value, static::encoding()) : strtolower($value);
- }
-
- public static function upper($value)
- {
- return (MB_STRING) ? mb_strtoupper($value, static::encoding()) : strtoupper($value);
- }
-
- public static function title($value)
- {
- if (MB_STRING)
- {
- return mb_convert_case($value, MB_CASE_TITLE, static::encoding());
- }
- return ucwords(strtolower($value));
- }
-
- public static function limit($value, $limit = 100, $end = '...')
- {
- if (static::length($value) <= $limit) return $value;
- if (MB_STRING)
- {
- return mb_substr($value, 0, $limit, static::encoding()).$end;
- }
- return substr($value, 0, $limit).$end;
- }
-
- public static function words($value, $words = 100, $end = '...')
- {
- if (trim($value) == '') return '';
- preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $value, $matches);
- if (static::length($value) == static::length($matches[0]))
- {
- $end = '';
- }
- return rtrim($matches[0]).$end;
- }
-
- public static function singular($value)
- {
- return static::pluralizer()->singular($value);
- }
-
- public static function plural($value, $count = 2)
- {
- return static::pluralizer()->plural($value, $count);
- }
-
- protected static function pluralizer()
- {
- $config = Config::get('strings');
- return static::$pluralizer ?: static::$pluralizer = new Pluralizer($config);
- }
-
- public static function slug($title, $separator = '-')
- {
- $title = static::ascii($title);
-
- $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', static::lower($title));
-
- $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
- return trim($title, $separator);
- }
-
- public static function ascii($value)
- {
- $foreign = Config::get('strings.ascii');
- $value = preg_replace(array_keys($foreign), array_values($foreign), $value);
- return preg_replace('/[^\x09\x0A\x0D\x20-\x7E]/', '', $value);
- }
-
- public static function classify($value)
- {
- $search = array('_', '-', '.');
- return str_replace(' ', '_', static::title(str_replace($search, ' ', $value)));
- }
-
- public static function segments($value)
- {
- return array_diff(explode('/', trim($value, '/')), array(''));
- }
-
- public static function random($length, $type = 'alnum')
- {
- return substr(str_shuffle(str_repeat(static::pool($type), 5)), 0, $length);
- }
-
- public static function is($pattern, $value)
- {
-
-
-
- if ($pattern !== '/')
- {
- $pattern = str_replace('*', '(.*)', $pattern).'\z';
- }
- else
- {
- $pattern = '^/$';
- }
- return preg_match('#'.$pattern.'#', $value);
- }
-
- protected static function pool($type)
- {
- switch ($type)
- {
- case 'alpha':
- return 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
- case 'alnum':
- return '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
- default:
- throw new \Exception("Invalid random string type [$type].");
- }
- }
- }
|