123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695 |
- <?php
- class Kohana_Text {
-
- public static $units = [
- 1000000000 => 'billion',
- 1000000 => 'million',
- 1000 => 'thousand',
- 100 => 'hundred',
- 90 => 'ninety',
- 80 => 'eighty',
- 70 => 'seventy',
- 60 => 'sixty',
- 50 => 'fifty',
- 40 => 'fourty',
- 30 => 'thirty',
- 20 => 'twenty',
- 19 => 'nineteen',
- 18 => 'eighteen',
- 17 => 'seventeen',
- 16 => 'sixteen',
- 15 => 'fifteen',
- 14 => 'fourteen',
- 13 => 'thirteen',
- 12 => 'twelve',
- 11 => 'eleven',
- 10 => 'ten',
- 9 => 'nine',
- 8 => 'eight',
- 7 => 'seven',
- 6 => 'six',
- 5 => 'five',
- 4 => 'four',
- 3 => 'three',
- 2 => 'two',
- 1 => 'one',
- ];
-
- public static function limit_words($str, $limit = 100, $end_char = NULL)
- {
- $limit = (int) $limit;
- $end_char = ($end_char === NULL) ? '…' : $end_char;
- if (trim($str) === '')
- return $str;
- if ($limit <= 0)
- return $end_char;
- preg_match('/^\s*+(?:\S++\s*+){1,'.$limit.'}/u', $str, $matches);
-
-
- return rtrim($matches[0]).((strlen($matches[0]) === strlen($str)) ? '' : $end_char);
- }
-
- public static function limit_chars($str, $limit = 100, $end_char = NULL, $preserve_words = FALSE)
- {
- $end_char = ($end_char === NULL) ? '…' : $end_char;
- $limit = (int) $limit;
- if (trim($str) === '' OR UTF8::strlen($str) <= $limit)
- return $str;
- if ($limit <= 0)
- return $end_char;
- if ($preserve_words === FALSE)
- return rtrim(UTF8::substr($str, 0, $limit)).$end_char;
-
-
- if ( ! preg_match('/^.{0,'.$limit.'}\s/us', $str, $matches))
- return $end_char;
- return rtrim($matches[0]).((strlen($matches[0]) === strlen($str)) ? '' : $end_char);
- }
-
- public static function alternate()
- {
- static $i;
- if (func_num_args() === 0)
- {
- $i = 0;
- return '';
- }
- $args = func_get_args();
- return $args[($i++ % count($args))];
- }
-
- public static function random($type = NULL, $length = 8)
- {
- if ($type === NULL)
- {
-
- $type = 'alnum';
- }
- $utf8 = FALSE;
- switch ($type)
- {
- case 'alnum':
- $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
- break;
- case 'alpha':
- $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
- break;
- case 'hexdec':
- $pool = '0123456789abcdef';
- break;
- case 'numeric':
- $pool = '0123456789';
- break;
- case 'nozero':
- $pool = '123456789';
- break;
- case 'distinct':
- $pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
- break;
- default:
- $pool = (string) $type;
- $utf8 = ! UTF8::is_ascii($pool);
- break;
- }
-
- $pool = ($utf8 === TRUE) ? UTF8::str_split($pool, 1) : str_split($pool, 1);
-
- $max = count($pool) - 1;
- $str = '';
- for ($i = 0; $i < $length; $i++)
- {
-
- $str .= $pool[mt_rand(0, $max)];
- }
-
- if ($type === 'alnum' AND $length > 1)
- {
- if (ctype_alpha($str))
- {
-
- $str[mt_rand(0, $length - 1)] = chr(mt_rand(48, 57));
- }
- elseif (ctype_digit($str))
- {
-
- $str[mt_rand(0, $length - 1)] = chr(mt_rand(65, 90));
- }
- }
- return $str;
- }
-
- public static function ucfirst($string, $delimiter = '-')
- {
-
- return implode($delimiter, array_map('UTF8::ucfirst', explode($delimiter, $string)));
- }
-
- public static function reduce_slashes($str)
- {
- return preg_replace('#(?<!:)//+#', '/', $str);
- }
-
- public static function censor($str, $badwords, $replacement = '#', $replace_partial_words = TRUE)
- {
- foreach ( (array) $badwords as $key => $badword)
- {
- $badwords[$key] = str_replace('\*', '\S*?', preg_quote( (string) $badword));
- }
- $regex = '('.implode('|', $badwords).')';
- if ($replace_partial_words === FALSE)
- {
-
- $regex = '(?<=\b|\s|^)'.$regex.'(?=\b|\s|$)';
- }
- $regex = '!'.$regex.'!ui';
-
- if (UTF8::strlen($replacement) == 1)
- {
- return preg_replace_callback($regex, function($matches) use ($replacement) {
- return str_repeat($replacement, UTF8::strlen($matches[1]));
- }, $str);
- }
-
- return preg_replace($regex, $replacement, $str);
- }
-
- public static function similar(array $words)
- {
-
- $word = current($words);
- for ($i = 0, $max = strlen($word); $i < $max; ++$i)
- {
- foreach ($words as $w)
- {
-
- if ( ! isset($w[$i]) OR $w[$i] !== $word[$i])
- break 2;
- }
- }
-
- return substr($word, 0, $i);
- }
-
- public static function auto_link($text)
- {
-
- return Text::auto_link_urls(Text::auto_link_emails($text));
- }
-
- public static function auto_link_urls($text)
- {
-
- $text = preg_replace_callback('~\b(?<!href="|">)(?:ht|f)tps?://[^<\s]+(?:/|\b)~i', 'Text::_auto_link_urls_callback1', $text);
-
- return preg_replace_callback('~\b(?<!://|">)www(?:\.[a-z0-9][-a-z0-9]*+)+\.[a-z]{2,6}[^<\s]*\b~i', 'Text::_auto_link_urls_callback2', $text);
- }
- protected static function _auto_link_urls_callback1($matches)
- {
- return HTML::anchor($matches[0]);
- }
- protected static function _auto_link_urls_callback2($matches)
- {
- return HTML::anchor('http://'.$matches[0], $matches[0]);
- }
-
- public static function auto_link_emails($text)
- {
-
-
-
- return preg_replace_callback('~\b(?<!href="mailto:|58;)(?!\.)[-+_a-z0-9.]++(?<!\.)@(?![-.])[-a-z0-9.]+(?<!\.)\.[a-z]{2,6}\b(?!</a>)~i', 'Text::_auto_link_emails_callback', $text);
- }
- protected static function _auto_link_emails_callback($matches)
- {
- return HTML::mailto($matches[0]);
- }
-
- public static function auto_p($str, $br = TRUE)
- {
-
- if (($str = trim($str)) === '')
- return '';
-
- $str = str_replace(["\r\n", "\r"], "\n", $str);
-
- $str = preg_replace('~^[ \t]+~m', '', $str);
- $str = preg_replace('~[ \t]+$~m', '', $str);
-
- if ($html_found = (strpos($str, '<') !== FALSE))
- {
-
- $no_p = '(?:p|div|h[1-6r]|ul|ol|li|blockquote|d[dlt]|pre|t[dhr]|t(?:able|body|foot|head)|c(?:aption|olgroup)|form|s(?:elect|tyle)|a(?:ddress|rea)|ma(?:p|th))';
-
- $str = preg_replace('~^<'.$no_p.'[^>]*+>~im', "\n$0", $str);
- $str = preg_replace('~</'.$no_p.'\s*+>$~im', "$0\n", $str);
- }
-
- $str = '<p>'.trim($str).'</p>';
- $str = preg_replace('~\n{2,}~', "</p>\n\n<p>", $str);
-
- if ($html_found !== FALSE)
- {
-
- $str = preg_replace('~<p>(?=</?'.$no_p.'[^>]*+>)~i', '', $str);
- $str = preg_replace('~(</?'.$no_p.'[^>]*+>)</p>~i', '$1', $str);
- }
-
- if ($br === TRUE)
- {
- $str = preg_replace('~(?<!\n)\n(?!\n)~', "<br />\n", $str);
- }
- return $str;
- }
-
- public static function bytes($bytes, $force_unit = NULL, $format = NULL, $si = TRUE)
- {
-
- $format = ($format === NULL) ? '%01.2f %s' : (string) $format;
-
- if ($si == FALSE OR strpos($force_unit, 'i') !== FALSE)
- {
- $units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
- $mod = 1024;
- }
-
- else
- {
- $units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB'];
- $mod = 1000;
- }
-
- if (($power = array_search( (string) $force_unit, $units)) === FALSE)
- {
- $power = ($bytes > 0) ? floor(log($bytes, $mod)) : 0;
- }
- return sprintf($format, $bytes / pow($mod, $power), $units[$power]);
- }
-
- public static function number($number)
- {
-
- $number = (int) $number;
-
- $text = [];
-
- $last_unit = NULL;
-
- $last_item = '';
- foreach (Text::$units as $unit => $name)
- {
- if ($number / $unit >= 1)
- {
-
- $number -= $unit * ($value = (int) floor($number / $unit));
-
- $item = '';
- if ($unit < 100)
- {
- if ($last_unit < 100 AND $last_unit >= 20)
- {
- $last_item .= '-'.$name;
- }
- else
- {
- $item = $name;
- }
- }
- else
- {
- $item = Text::number($value).' '.$name;
- }
-
-
- if (empty($item))
- {
- array_pop($text);
- $item = $last_item;
- }
- $last_item = $text[] = $item;
- $last_unit = $unit;
- }
- }
- if (count($text) > 1)
- {
- $and = array_pop($text);
- }
- $text = implode(', ', $text);
- if (isset($and))
- {
- $text .= ' and '.$and;
- }
- return $text;
- }
-
- public static function widont($str)
- {
-
- $widont_regex = "%
- ((?:</?(?:a|em|span|strong|i|b)[^>]*>)|[^<>\s]) # must be proceeded by an approved inline opening or closing tag or a nontag/nonspace
- \s+ # the space to replace
- ([^<>\s]+ # must be flollowed by non-tag non-space characters
- \s* # optional white space!
- (</(a|em|span|strong|i|b)>\s*)* # optional closing inline tags with optional white space after each
- ((</(p|h[1-6]|li|dt|dd)>)|$)) # end with a closing p, h1-6, li or the end of the string
- %x";
- return preg_replace($widont_regex, '$1 $2', $str);
- }
-
- public static function user_agent($agent, $value)
- {
- if (is_array($value))
- {
- $data = [];
- foreach ($value as $part)
- {
-
- $data[$part] = Text::user_agent($agent, $part);
- }
- return $data;
- }
- if ($value === 'browser' OR $value == 'version')
- {
-
- $info = [];
-
- $browsers = Kohana::$config->load('user_agents')->browser;
- foreach ($browsers as $search => $name)
- {
- if (stripos($agent, $search) !== FALSE)
- {
-
- $info['browser'] = $name;
- if (preg_match('#'.preg_quote($search).'[^0-9.]*+([0-9.][0-9.a-z]*)#i', $agent, $matches))
- {
-
- $info['version'] = $matches[1];
- }
- else
- {
-
- $info['version'] = FALSE;
- }
- return $info[$value];
- }
- }
- }
- else
- {
-
- $group = Kohana::$config->load('user_agents')->$value;
- foreach ($group as $search => $name)
- {
- if (stripos($agent, $search) !== FALSE)
- {
-
- return $name;
- }
- }
- }
-
- return FALSE;
- }
- }
|