PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/system/helpers/text_helper.php

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