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

/system/helpers/text_helper.php

https://github.com/joelmwas/CodeIgniter
PHP | 542 lines | 327 code | 60 blank | 155 comment | 39 complexity | 65bd67305d9e425f2326c9304e773d2a MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.2.4 or newer
  6. *
  7. * NOTICE OF LICENSE
  8. *
  9. * Licensed under the Open Software License version 3.0
  10. *
  11. * This source file is subject to the Open Software License (OSL 3.0) that is
  12. * bundled with this package in the files license.txt / license.rst. It is
  13. * also available through the world wide web at this URL:
  14. * http://opensource.org/licenses/OSL-3.0
  15. * If you did not receive a copy of the license and are unable to obtain it
  16. * through the world wide web, please send an email to
  17. * licensing@ellislab.com so we can send you a copy immediately.
  18. *
  19. * @package CodeIgniter
  20. * @author EllisLab Dev Team
  21. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
  22. * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
  23. * @link http://codeigniter.com
  24. * @since Version 1.0
  25. * @filesource
  26. */
  27. defined('BASEPATH') OR exit('No direct script access allowed');
  28. /**
  29. * CodeIgniter Text Helpers
  30. *
  31. * @package CodeIgniter
  32. * @subpackage Helpers
  33. * @category Helpers
  34. * @author EllisLab Dev Team
  35. * @link http://codeigniter.com/user_guide/helpers/text_helper.html
  36. */
  37. // ------------------------------------------------------------------------
  38. if ( ! function_exists('word_limiter'))
  39. {
  40. /**
  41. * Word Limiter
  42. *
  43. * Limits a string to X number of words.
  44. *
  45. * @param string
  46. * @param int
  47. * @param string the end character. Usually an ellipsis
  48. * @return string
  49. */
  50. function word_limiter($str, $limit = 100, $end_char = '&#8230;')
  51. {
  52. if (trim($str) === '')
  53. {
  54. return $str;
  55. }
  56. preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str, $matches);
  57. if (strlen($str) === strlen($matches[0]))
  58. {
  59. $end_char = '';
  60. }
  61. return rtrim($matches[0]).$end_char;
  62. }
  63. }
  64. // ------------------------------------------------------------------------
  65. if ( ! function_exists('character_limiter'))
  66. {
  67. /**
  68. * Character Limiter
  69. *
  70. * Limits the string based on the character count. Preserves complete words
  71. * so the character count may not be exactly as specified.
  72. *
  73. * @param string
  74. * @param int
  75. * @param string the end character. Usually an ellipsis
  76. * @return string
  77. */
  78. function character_limiter($str, $n = 500, $end_char = '&#8230;')
  79. {
  80. if (mb_strlen($str) < $n)
  81. {
  82. return $str;
  83. }
  84. // a bit complicated, but faster than preg_replace with \s+
  85. $str = preg_replace('/ {2,}/', ' ', str_replace(array("\r", "\n", "\t", "\x0B", "\x0C"), ' ', $str));
  86. if (mb_strlen($str) <= $n)
  87. {
  88. return $str;
  89. }
  90. $out = '';
  91. foreach (explode(' ', trim($str)) as $val)
  92. {
  93. $out .= $val.' ';
  94. if (mb_strlen($out) >= $n)
  95. {
  96. $out = trim($out);
  97. return (mb_strlen($out) === mb_strlen($str)) ? $out : $out.$end_char;
  98. }
  99. }
  100. }
  101. }
  102. // ------------------------------------------------------------------------
  103. if ( ! function_exists('ascii_to_entities'))
  104. {
  105. /**
  106. * High ASCII to Entities
  107. *
  108. * Converts high ASCII text and MS Word special characters to character entities
  109. *
  110. * @param string $str
  111. * @return string
  112. */
  113. function ascii_to_entities($str)
  114. {
  115. $out = '';
  116. for ($i = 0, $s = strlen($str) - 1, $count = 1, $temp = array(); $i <= $s; $i++)
  117. {
  118. $ordinal = ord($str[$i]);
  119. if ($ordinal < 128)
  120. {
  121. /*
  122. If the $temp array has a value but we have moved on, then it seems only
  123. fair that we output that entity and restart $temp before continuing. -Paul
  124. */
  125. if (count($temp) === 1)
  126. {
  127. $out .= '&#'.array_shift($temp).';';
  128. $count = 1;
  129. }
  130. $out .= $str[$i];
  131. }
  132. else
  133. {
  134. if (count($temp) === 0)
  135. {
  136. $count = ($ordinal < 224) ? 2 : 3;
  137. }
  138. $temp[] = $ordinal;
  139. if (count($temp) === $count)
  140. {
  141. $number = ($count === 3)
  142. ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
  143. : (($temp[0] % 32) * 64) + ($temp[1] % 64);
  144. $out .= '&#'.$number.';';
  145. $count = 1;
  146. $temp = array();
  147. }
  148. // If this is the last iteration, just output whatever we have
  149. elseif ($i === $s)
  150. {
  151. $out .= '&#'.implode(';', $temp).';';
  152. }
  153. }
  154. }
  155. return $out;
  156. }
  157. }
  158. // ------------------------------------------------------------------------
  159. if ( ! function_exists('entities_to_ascii'))
  160. {
  161. /**
  162. * Entities to ASCII
  163. *
  164. * Converts character entities back to ASCII
  165. *
  166. * @param string
  167. * @param bool
  168. * @return string
  169. */
  170. function entities_to_ascii($str, $all = TRUE)
  171. {
  172. if (preg_match_all('/\&#(\d+)\;/', $str, $matches))
  173. {
  174. for ($i = 0, $s = count($matches[0]); $i < $s; $i++)
  175. {
  176. $digits = $matches[1][$i];
  177. $out = '';
  178. if ($digits < 128)
  179. {
  180. $out .= chr($digits);
  181. }
  182. elseif ($digits < 2048)
  183. {
  184. $out .= chr(192 + (($digits - ($digits % 64)) / 64)).chr(128 + ($digits % 64));
  185. }
  186. else
  187. {
  188. $out .= chr(224 + (($digits - ($digits % 4096)) / 4096))
  189. .chr(128 + ((($digits % 4096) - ($digits % 64)) / 64))
  190. .chr(128 + ($digits % 64));
  191. }
  192. $str = str_replace($matches[0][$i], $out, $str);
  193. }
  194. }
  195. if ($all)
  196. {
  197. return str_replace(
  198. array('&amp;', '&lt;', '&gt;', '&quot;', '&apos;', '&#45;'),
  199. array('&', '<', '>', '"', "'", '-'),
  200. $str
  201. );
  202. }
  203. return $str;
  204. }
  205. }
  206. // ------------------------------------------------------------------------
  207. if ( ! function_exists('word_censor'))
  208. {
  209. /**
  210. * Word Censoring Function
  211. *
  212. * Supply a string and an array of disallowed words and any
  213. * matched words will be converted to #### or to the replacement
  214. * word you've submitted.
  215. *
  216. * @param string the text string
  217. * @param string the array of censoered words
  218. * @param string the optional replacement value
  219. * @return string
  220. */
  221. function word_censor($str, $censored, $replacement = '')
  222. {
  223. if ( ! is_array($censored))
  224. {
  225. return $str;
  226. }
  227. $str = ' '.$str.' ';
  228. // \w, \b and a few others do not match on a unicode character
  229. // set for performance reasons. As a result words like 端ber
  230. // will not match on a word boundary. Instead, we'll assume that
  231. // a bad word will be bookeneded by any of these characters.
  232. $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';
  233. foreach ($censored as $badword)
  234. {
  235. if ($replacement !== '')
  236. {
  237. $str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/i", "\\1{$replacement}\\3", $str);
  238. }
  239. else
  240. {
  241. $str = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($badword, '/')).")({$delim})/ie", "'\\1'.str_repeat('#', strlen('\\2')).'\\3'", $str);
  242. }
  243. }
  244. return trim($str);
  245. }
  246. }
  247. // ------------------------------------------------------------------------
  248. if ( ! function_exists('highlight_code'))
  249. {
  250. /**
  251. * Code Highlighter
  252. *
  253. * Colorizes code strings
  254. *
  255. * @param string the text string
  256. * @return string
  257. */
  258. function highlight_code($str)
  259. {
  260. /* The highlight string function encodes and highlights
  261. * brackets so we need them to start raw.
  262. *
  263. * Also replace any existing PHP tags to temporary markers
  264. * so they don't accidentally break the string out of PHP,
  265. * and thus, thwart the highlighting.
  266. */
  267. $str = str_replace(
  268. array('&lt;', '&gt;', '<?', '?>', '<%', '%>', '\\', '</script>'),
  269. array('<', '>', 'phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
  270. $str
  271. );
  272. // The highlight_string function requires that the text be surrounded
  273. // by PHP tags, which we will remove later
  274. $str = highlight_string('<?php '.$str.' ?>', TRUE);
  275. // Remove our artificially added PHP, and the syntax highlighting that came with it
  276. $str = preg_replace(
  277. array(
  278. '/<span style="color: #([A-Z0-9]+)">&lt;\?php(&nbsp;| )/i',
  279. '/(<span style="color: #[A-Z0-9]+">.*?)\?&gt;<\/span>\n<\/span>\n<\/code>/is',
  280. '/<span style="color: #[A-Z0-9]+"\><\/span>/i'
  281. ),
  282. array(
  283. '<span style="color: #$1">',
  284. "$1</span>\n</span>\n</code>",
  285. ''
  286. ),
  287. $str
  288. );
  289. // Replace our markers back to PHP tags.
  290. return str_replace(
  291. array('phptagopen', 'phptagclose', 'asptagopen', 'asptagclose', 'backslashtmp', 'scriptclose'),
  292. array('&lt;?', '?&gt;', '&lt;%', '%&gt;', '\\', '&lt;/script&gt;'),
  293. $str
  294. );
  295. }
  296. }
  297. // ------------------------------------------------------------------------
  298. if ( ! function_exists('highlight_phrase'))
  299. {
  300. /**
  301. * Phrase Highlighter
  302. *
  303. * Highlights a phrase within a text string
  304. *
  305. * @param string $str the text string
  306. * @param string $phrase the phrase you'd like to highlight
  307. * @param string $tag_open the openging tag to precede the phrase with
  308. * @param string $tag_close the closing tag to end the phrase with
  309. * @return string
  310. */
  311. function highlight_phrase($str, $phrase, $tag_open = '<mark>', $tag_close = '</mark>')
  312. {
  313. return ($str !== '' && $phrase !== '')
  314. ? preg_replace('/('.preg_quote($phrase, '/').')/i'.(UTF8_ENABLED ? 'u' : ''), $tag_open.'\\1'.$tag_close, $str)
  315. : $str;
  316. }
  317. }
  318. // ------------------------------------------------------------------------
  319. if ( ! function_exists('convert_accented_characters'))
  320. {
  321. /**
  322. * Convert Accented Foreign Characters to ASCII
  323. *
  324. * @param string $str Input string
  325. * @return string
  326. */
  327. function convert_accented_characters($str)
  328. {
  329. static $array_from, $array_to;
  330. if ( ! is_array($array_from))
  331. {
  332. if (file_exists(APPPATH.'config/foreign_chars.php'))
  333. {
  334. include(APPPATH.'config/foreign_chars.php');
  335. }
  336. if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php'))
  337. {
  338. include(APPPATH.'config/'.ENVIRONMENT.'/foreign_chars.php');
  339. }
  340. if (empty($foreign_characters) OR ! is_array($foreign_characters))
  341. {
  342. $array_from = array();
  343. $array_to = array();
  344. return $str;
  345. }
  346. $array_from = array_keys($foreign_characters);
  347. $array_to = array_values($foreign_characters);
  348. }
  349. return preg_replace($array_from, $array_to, $str);
  350. }
  351. }
  352. // ------------------------------------------------------------------------
  353. if ( ! function_exists('word_wrap'))
  354. {
  355. /**
  356. * Word Wrap
  357. *
  358. * Wraps text at the specified character. Maintains the integrity of words.
  359. * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
  360. * will URLs.
  361. *
  362. * @param string $str the text string
  363. * @param int $charlim = 76 the number of characters to wrap at
  364. * @return string
  365. */
  366. function word_wrap($str, $charlim = 76)
  367. {
  368. // Set the character limit
  369. is_numeric($charlim) OR $charlim = 76;
  370. // Reduce multiple spaces
  371. $str = preg_replace('| +|', ' ', $str);
  372. // Standardize newlines
  373. if (strpos($str, "\r") !== FALSE)
  374. {
  375. $str = str_replace(array("\r\n", "\r"), "\n", $str);
  376. }
  377. // If the current word is surrounded by {unwrap} tags we'll
  378. // strip the entire chunk and replace it with a marker.
  379. $unwrap = array();
  380. if (preg_match_all('|(\{unwrap\}.+?\{/unwrap\})|s', $str, $matches))
  381. {
  382. for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
  383. {
  384. $unwrap[] = $matches[1][$i];
  385. $str = str_replace($matches[1][$i], '{{unwrapped'.$i.'}}', $str);
  386. }
  387. }
  388. // Use PHP's native function to do the initial wordwrap.
  389. // We set the cut flag to FALSE so that any individual words that are
  390. // too long get left alone. In the next step we'll deal with them.
  391. $str = wordwrap($str, $charlim, "\n", FALSE);
  392. // Split the string into individual lines of text and cycle through them
  393. $output = '';
  394. foreach (explode("\n", $str) as $line)
  395. {
  396. // Is the line within the allowed character count?
  397. // If so we'll join it to the output and continue
  398. if (mb_strlen($line) <= $charlim)
  399. {
  400. $output .= $line."\n";
  401. continue;
  402. }
  403. $temp = '';
  404. while (mb_strlen($line) > $charlim)
  405. {
  406. // If the over-length word is a URL we won't wrap it
  407. if (preg_match('!\[url.+\]|://|wwww.!', $line))
  408. {
  409. break;
  410. }
  411. // Trim the word down
  412. $temp .= mb_substr($line, 0, $charlim - 1);
  413. $line = mb_substr($line, $charlim - 1);
  414. }
  415. // If $temp contains data it means we had to split up an over-length
  416. // word into smaller chunks so we'll add it back to our current line
  417. if ($temp !== '')
  418. {
  419. $output .= $temp."\n".$line."\n";
  420. }
  421. else
  422. {
  423. $output .= $line."\n";
  424. }
  425. }
  426. // Put our markers back
  427. if (count($unwrap) > 0)
  428. {
  429. foreach ($unwrap as $key => $val)
  430. {
  431. $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
  432. }
  433. }
  434. // Remove the unwrap tags and return
  435. return str_replace(array('{unwrap}', '{/unwrap}'), '', $output);
  436. }
  437. }
  438. // ------------------------------------------------------------------------
  439. if ( ! function_exists('ellipsize'))
  440. {
  441. /**
  442. * Ellipsize String
  443. *
  444. * This function will strip tags from a string, split it at its max_length and ellipsize
  445. *
  446. * @param string string to ellipsize
  447. * @param int max length of string
  448. * @param mixed int (1|0) or float, .5, .2, etc for position to split
  449. * @param string ellipsis ; Default '...'
  450. * @return string ellipsized string
  451. */
  452. function ellipsize($str, $max_length, $position = 1, $ellipsis = '&hellip;')
  453. {
  454. // Strip tags
  455. $str = trim(strip_tags($str));
  456. // Is the string long enough to ellipsize?
  457. if (mb_strlen($str) <= $max_length)
  458. {
  459. return $str;
  460. }
  461. $beg = mb_substr($str, 0, floor($max_length * $position));
  462. $position = ($position > 1) ? 1 : $position;
  463. if ($position === 1)
  464. {
  465. $end = mb_substr($str, 0, -($max_length - mb_strlen($beg)));
  466. }
  467. else
  468. {
  469. $end = mb_substr($str, -($max_length - mb_strlen($beg)));
  470. }
  471. return $beg.$ellipsis.$end;
  472. }
  473. }
  474. /* End of file text_helper.php */
  475. /* Location: ./system/helpers/text_helper.php */