PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/rxwandc/system/classes/kohana/text.php

https://bitbucket.org/i1598/caiyun_stat
PHP | 606 lines | 291 code | 72 blank | 243 comment | 31 complexity | 35352cf60809d219454f69412821deac MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Text helper class. Provides simple methods for working with text.
  4. *
  5. * @package Kohana
  6. * @category Helpers
  7. * @author Kohana Team
  8. * @copyright (c) 2007-2012 Kohana Team
  9. * @license http://kohanaframework.org/license
  10. */
  11. class Kohana_Text {
  12. /**
  13. * @var array number units and text equivalents
  14. */
  15. public static $units = array(
  16. 1000000000 => 'billion',
  17. 1000000 => 'million',
  18. 1000 => 'thousand',
  19. 100 => 'hundred',
  20. 90 => 'ninety',
  21. 80 => 'eighty',
  22. 70 => 'seventy',
  23. 60 => 'sixty',
  24. 50 => 'fifty',
  25. 40 => 'fourty',
  26. 30 => 'thirty',
  27. 20 => 'twenty',
  28. 19 => 'nineteen',
  29. 18 => 'eighteen',
  30. 17 => 'seventeen',
  31. 16 => 'sixteen',
  32. 15 => 'fifteen',
  33. 14 => 'fourteen',
  34. 13 => 'thirteen',
  35. 12 => 'twelve',
  36. 11 => 'eleven',
  37. 10 => 'ten',
  38. 9 => 'nine',
  39. 8 => 'eight',
  40. 7 => 'seven',
  41. 6 => 'six',
  42. 5 => 'five',
  43. 4 => 'four',
  44. 3 => 'three',
  45. 2 => 'two',
  46. 1 => 'one',
  47. );
  48. /**
  49. * Limits a phrase to a given number of words.
  50. *
  51. * $text = Text::limit_words($text);
  52. *
  53. * @param string $str phrase to limit words of
  54. * @param integer $limit number of words to limit to
  55. * @param string $end_char end character or entity
  56. * @return string
  57. */
  58. public static function limit_words($str, $limit = 100, $end_char = NULL)
  59. {
  60. $limit = (int) $limit;
  61. $end_char = ($end_char === NULL) ? '…' : $end_char;
  62. if (trim($str) === '')
  63. return $str;
  64. if ($limit <= 0)
  65. return $end_char;
  66. preg_match('/^\s*+(?:\S++\s*+){1,'.$limit.'}/u', $str, $matches);
  67. // Only attach the end character if the matched string is shorter
  68. // than the starting string.
  69. return rtrim($matches[0]).((strlen($matches[0]) === strlen($str)) ? '' : $end_char);
  70. }
  71. /**
  72. * Limits a phrase to a given number of characters.
  73. *
  74. * $text = Text::limit_chars($text);
  75. *
  76. * @param string $str phrase to limit characters of
  77. * @param integer $limit number of characters to limit to
  78. * @param string $end_char end character or entity
  79. * @param boolean $preserve_words enable or disable the preservation of words while limiting
  80. * @return string
  81. * @uses UTF8::strlen
  82. */
  83. public static function limit_chars($str, $limit = 100, $end_char = NULL, $preserve_words = FALSE)
  84. {
  85. $end_char = ($end_char === NULL) ? '…' : $end_char;
  86. $limit = (int) $limit;
  87. if (trim($str) === '' OR UTF8::strlen($str) <= $limit)
  88. return $str;
  89. if ($limit <= 0)
  90. return $end_char;
  91. if ($preserve_words === FALSE)
  92. return rtrim(UTF8::substr($str, 0, $limit)).$end_char;
  93. // Don't preserve words. The limit is considered the top limit.
  94. // No strings with a length longer than $limit should be returned.
  95. if ( ! preg_match('/^.{0,'.$limit.'}\s/us', $str, $matches))
  96. return $end_char;
  97. return rtrim($matches[0]).((strlen($matches[0]) === strlen($str)) ? '' : $end_char);
  98. }
  99. /**
  100. * Alternates between two or more strings.
  101. *
  102. * echo Text::alternate('one', 'two'); // "one"
  103. * echo Text::alternate('one', 'two'); // "two"
  104. * echo Text::alternate('one', 'two'); // "one"
  105. *
  106. * Note that using multiple iterations of different strings may produce
  107. * unexpected results.
  108. *
  109. * @param string $str,... strings to alternate between
  110. * @return string
  111. */
  112. public static function alternate()
  113. {
  114. static $i;
  115. if (func_num_args() === 0)
  116. {
  117. $i = 0;
  118. return '';
  119. }
  120. $args = func_get_args();
  121. return $args[($i++ % count($args))];
  122. }
  123. /**
  124. * Generates a random string of a given type and length.
  125. *
  126. *
  127. * $str = Text::random(); // 8 character random string
  128. *
  129. * The following types are supported:
  130. *
  131. * alnum
  132. * : Upper and lower case a-z, 0-9 (default)
  133. *
  134. * alpha
  135. * : Upper and lower case a-z
  136. *
  137. * hexdec
  138. * : Hexadecimal characters a-f, 0-9
  139. *
  140. * distinct
  141. * : Uppercase characters and numbers that cannot be confused
  142. *
  143. * You can also create a custom type by providing the "pool" of characters
  144. * as the type.
  145. *
  146. * @param string $type a type of pool, or a string of characters to use as the pool
  147. * @param integer $length length of string to return
  148. * @return string
  149. * @uses UTF8::split
  150. */
  151. public static function random($type = NULL, $length = 8)
  152. {
  153. if ($type === NULL)
  154. {
  155. // Default is to generate an alphanumeric string
  156. $type = 'alnum';
  157. }
  158. $utf8 = FALSE;
  159. switch ($type)
  160. {
  161. case 'alnum':
  162. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  163. break;
  164. case 'alpha':
  165. $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  166. break;
  167. case 'hexdec':
  168. $pool = '0123456789abcdef';
  169. break;
  170. case 'numeric':
  171. $pool = '0123456789';
  172. break;
  173. case 'nozero':
  174. $pool = '123456789';
  175. break;
  176. case 'distinct':
  177. $pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
  178. break;
  179. default:
  180. $pool = (string) $type;
  181. $utf8 = ! UTF8::is_ascii($pool);
  182. break;
  183. }
  184. // Split the pool into an array of characters
  185. $pool = ($utf8 === TRUE) ? UTF8::str_split($pool, 1) : str_split($pool, 1);
  186. // Largest pool key
  187. $max = count($pool) - 1;
  188. $str = '';
  189. for ($i = 0; $i < $length; $i++)
  190. {
  191. // Select a random character from the pool and add it to the string
  192. $str .= $pool[mt_rand(0, $max)];
  193. }
  194. // Make sure alnum strings contain at least one letter and one digit
  195. if ($type === 'alnum' AND $length > 1)
  196. {
  197. if (ctype_alpha($str))
  198. {
  199. // Add a random digit
  200. $str[mt_rand(0, $length - 1)] = chr(mt_rand(48, 57));
  201. }
  202. elseif (ctype_digit($str))
  203. {
  204. // Add a random letter
  205. $str[mt_rand(0, $length - 1)] = chr(mt_rand(65, 90));
  206. }
  207. }
  208. return $str;
  209. }
  210. /**
  211. * Uppercase words that are not separated by spaces, using a custom
  212. * delimiter or the default.
  213. *
  214. * $str = Text::ucfirst('content-type'); // returns "Content-Type"
  215. *
  216. * @param string $string string to transform
  217. * @param string $delimiter delemiter to use
  218. * @return string
  219. */
  220. public static function ucfirst($string, $delimiter = '-')
  221. {
  222. // Put the keys back the Case-Convention expected
  223. return implode($delimiter, array_map('ucfirst', explode($delimiter, $string)));
  224. }
  225. /**
  226. * Reduces multiple slashes in a string to single slashes.
  227. *
  228. * $str = Text::reduce_slashes('foo//bar/baz'); // "foo/bar/baz"
  229. *
  230. * @param string $str string to reduce slashes of
  231. * @return string
  232. */
  233. public static function reduce_slashes($str)
  234. {
  235. return preg_replace('#(?<!:)//+#', '/', $str);
  236. }
  237. /**
  238. * Replaces the given words with a string.
  239. *
  240. * // Displays "What the #####, man!"
  241. * echo Text::censor('What the frick, man!', array(
  242. * 'frick' => '#####',
  243. * ));
  244. *
  245. * @param string $str phrase to replace words in
  246. * @param array $badwords words to replace
  247. * @param string $replacement replacement string
  248. * @param boolean $replace_partial_words replace words across word boundries (space, period, etc)
  249. * @return string
  250. * @uses UTF8::strlen
  251. */
  252. public static function censor($str, $badwords, $replacement = '#', $replace_partial_words = TRUE)
  253. {
  254. foreach ( (array) $badwords as $key => $badword)
  255. {
  256. $badwords[$key] = str_replace('\*', '\S*?', preg_quote( (string) $badword));
  257. }
  258. $regex = '('.implode('|', $badwords).')';
  259. if ($replace_partial_words === FALSE)
  260. {
  261. // Just using \b isn't sufficient when we need to replace a badword that already contains word boundaries itself
  262. $regex = '(?<=\b|\s|^)'.$regex.'(?=\b|\s|$)';
  263. }
  264. $regex = '!'.$regex.'!ui';
  265. if (UTF8::strlen($replacement) == 1)
  266. {
  267. $regex .= 'e';
  268. return preg_replace($regex, 'str_repeat($replacement, UTF8::strlen(\'$1\'))', $str);
  269. }
  270. return preg_replace($regex, $replacement, $str);
  271. }
  272. /**
  273. * Finds the text that is similar between a set of words.
  274. *
  275. * $match = Text::similar(array('fred', 'fran', 'free'); // "fr"
  276. *
  277. * @param array $words words to find similar text of
  278. * @return string
  279. */
  280. public static function similar(array $words)
  281. {
  282. // First word is the word to match against
  283. $word = current($words);
  284. for ($i = 0, $max = strlen($word); $i < $max; ++$i)
  285. {
  286. foreach ($words as $w)
  287. {
  288. // Once a difference is found, break out of the loops
  289. if ( ! isset($w[$i]) OR $w[$i] !== $word[$i])
  290. break 2;
  291. }
  292. }
  293. // Return the similar text
  294. return substr($word, 0, $i);
  295. }
  296. /**
  297. * Converts text email addresses and anchors into links. Existing links
  298. * will not be altered.
  299. *
  300. * echo Text::auto_link($text);
  301. *
  302. * [!!] This method is not foolproof since it uses regex to parse HTML.
  303. *
  304. * @param string $text text to auto link
  305. * @return string
  306. * @uses Text::auto_link_urls
  307. * @uses Text::auto_link_emails
  308. */
  309. public static function auto_link($text)
  310. {
  311. // Auto link emails first to prevent problems with "www.domain.com@example.com"
  312. return Text::auto_link_urls(Text::auto_link_emails($text));
  313. }
  314. /**
  315. * Converts text anchors into links. Existing links will not be altered.
  316. *
  317. * echo Text::auto_link_urls($text);
  318. *
  319. * [!!] This method is not foolproof since it uses regex to parse HTML.
  320. *
  321. * @param string $text text to auto link
  322. * @return string
  323. * @uses HTML::anchor
  324. */
  325. public static function auto_link_urls($text)
  326. {
  327. // Find and replace all http/https/ftp/ftps links that are not part of an existing html anchor
  328. $text = preg_replace_callback('~\b(?<!href="|">)(?:ht|f)tps?://[^<\s]+(?:/|\b)~i', 'Text::_auto_link_urls_callback1', $text);
  329. // Find and replace all naked www.links.com (without http://)
  330. 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);
  331. }
  332. protected static function _auto_link_urls_callback1($matches)
  333. {
  334. return HTML::anchor($matches[0]);
  335. }
  336. protected static function _auto_link_urls_callback2($matches)
  337. {
  338. return HTML::anchor('http://'.$matches[0], $matches[0]);
  339. }
  340. /**
  341. * Converts text email addresses into links. Existing links will not
  342. * be altered.
  343. *
  344. * echo Text::auto_link_emails($text);
  345. *
  346. * [!!] This method is not foolproof since it uses regex to parse HTML.
  347. *
  348. * @param string $text text to auto link
  349. * @return string
  350. * @uses HTML::mailto
  351. */
  352. public static function auto_link_emails($text)
  353. {
  354. // Find and replace all email addresses that are not part of an existing html mailto anchor
  355. // Note: The "58;" negative lookbehind prevents matching of existing encoded html mailto anchors
  356. // The html entity for a colon (:) is &#58; or &#058; or &#0058; etc.
  357. 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);
  358. }
  359. protected static function _auto_link_emails_callback($matches)
  360. {
  361. return HTML::mailto($matches[0]);
  362. }
  363. /**
  364. * Automatically applies "p" and "br" markup to text.
  365. * Basically [nl2br](http://php.net/nl2br) on steroids.
  366. *
  367. * echo Text::auto_p($text);
  368. *
  369. * [!!] This method is not foolproof since it uses regex to parse HTML.
  370. *
  371. * @param string $str subject
  372. * @param boolean $br convert single linebreaks to <br />
  373. * @return string
  374. */
  375. public static function auto_p($str, $br = TRUE)
  376. {
  377. // Trim whitespace
  378. if (($str = trim($str)) === '')
  379. return '';
  380. // Standardize newlines
  381. $str = str_replace(array("\r\n", "\r"), "\n", $str);
  382. // Trim whitespace on each line
  383. $str = preg_replace('~^[ \t]+~m', '', $str);
  384. $str = preg_replace('~[ \t]+$~m', '', $str);
  385. // The following regexes only need to be executed if the string contains html
  386. if ($html_found = (strpos($str, '<') !== FALSE))
  387. {
  388. // Elements that should not be surrounded by p tags
  389. $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))';
  390. // Put at least two linebreaks before and after $no_p elements
  391. $str = preg_replace('~^<'.$no_p.'[^>]*+>~im', "\n$0", $str);
  392. $str = preg_replace('~</'.$no_p.'\s*+>$~im', "$0\n", $str);
  393. }
  394. // Do the <p> magic!
  395. $str = '<p>'.trim($str).'</p>';
  396. $str = preg_replace('~\n{2,}~', "</p>\n\n<p>", $str);
  397. // The following regexes only need to be executed if the string contains html
  398. if ($html_found !== FALSE)
  399. {
  400. // Remove p tags around $no_p elements
  401. $str = preg_replace('~<p>(?=</?'.$no_p.'[^>]*+>)~i', '', $str);
  402. $str = preg_replace('~(</?'.$no_p.'[^>]*+>)</p>~i', '$1', $str);
  403. }
  404. // Convert single linebreaks to <br />
  405. if ($br === TRUE)
  406. {
  407. $str = preg_replace('~(?<!\n)\n(?!\n)~', "<br />\n", $str);
  408. }
  409. return $str;
  410. }
  411. /**
  412. * Returns human readable sizes. Based on original functions written by
  413. * [Aidan Lister](http://aidanlister.com/repos/v/function.size_readable.php)
  414. * and [Quentin Zervaas](http://www.phpriot.com/d/code/strings/filesize-format/).
  415. *
  416. * echo Text::bytes(filesize($file));
  417. *
  418. * @param integer $bytes size in bytes
  419. * @param string $force_unit a definitive unit
  420. * @param string $format the return string format
  421. * @param boolean $si whether to use SI prefixes or IEC
  422. * @return string
  423. */
  424. public static function bytes($bytes, $force_unit = NULL, $format = NULL, $si = TRUE)
  425. {
  426. // Format string
  427. $format = ($format === NULL) ? '%01.2f %s' : (string) $format;
  428. // IEC prefixes (binary)
  429. if ($si == FALSE OR strpos($force_unit, 'i') !== FALSE)
  430. {
  431. $units = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
  432. $mod = 1024;
  433. }
  434. // SI prefixes (decimal)
  435. else
  436. {
  437. $units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
  438. $mod = 1000;
  439. }
  440. // Determine unit to use
  441. if (($power = array_search( (string) $force_unit, $units)) === FALSE)
  442. {
  443. $power = ($bytes > 0) ? floor(log($bytes, $mod)) : 0;
  444. }
  445. return sprintf($format, $bytes / pow($mod, $power), $units[$power]);
  446. }
  447. /**
  448. * Format a number to human-readable text.
  449. *
  450. * // Display: one thousand and twenty-four
  451. * echo Text::number(1024);
  452. *
  453. * // Display: five million, six hundred and thirty-two
  454. * echo Text::number(5000632);
  455. *
  456. * @param integer $number number to format
  457. * @return string
  458. * @since 3.0.8
  459. */
  460. public static function number($number)
  461. {
  462. // The number must always be an integer
  463. $number = (int) $number;
  464. // Uncompiled text version
  465. $text = array();
  466. // Last matched unit within the loop
  467. $last_unit = NULL;
  468. // The last matched item within the loop
  469. $last_item = '';
  470. foreach (Text::$units as $unit => $name)
  471. {
  472. if ($number / $unit >= 1)
  473. {
  474. // $value = the number of times the number is divisble by unit
  475. $number -= $unit * ($value = (int) floor($number / $unit));
  476. // Temporary var for textifying the current unit
  477. $item = '';
  478. if ($unit < 100)
  479. {
  480. if ($last_unit < 100 AND $last_unit >= 20)
  481. {
  482. $last_item .= '-'.$name;
  483. }
  484. else
  485. {
  486. $item = $name;
  487. }
  488. }
  489. else
  490. {
  491. $item = Text::number($value).' '.$name;
  492. }
  493. // In the situation that we need to make a composite number (i.e. twenty-three)
  494. // then we need to modify the previous entry
  495. if (empty($item))
  496. {
  497. array_pop($text);
  498. $item = $last_item;
  499. }
  500. $last_item = $text[] = $item;
  501. $last_unit = $unit;
  502. }
  503. }
  504. if (count($text) > 1)
  505. {
  506. $and = array_pop($text);
  507. }
  508. $text = implode(', ', $text);
  509. if (isset($and))
  510. {
  511. $text .= ' and '.$and;
  512. }
  513. return $text;
  514. }
  515. /**
  516. * Prevents [widow words](http://www.shauninman.com/archive/2006/08/22/widont_wordpress_plugin)
  517. * by inserting a non-breaking space between the last two words.
  518. *
  519. * echo Text::widont($text);
  520. *
  521. * @param string $str text to remove widows from
  522. * @return string
  523. */
  524. public static function widont($str)
  525. {
  526. $str = rtrim($str);
  527. $space = strrpos($str, ' ');
  528. if ($space !== FALSE)
  529. {
  530. $str = substr($str, 0, $space).'&nbsp;'.substr($str, $space + 1);
  531. }
  532. return $str;
  533. }
  534. } // End text