PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/system/helpers/text_helper.php

https://github.com/xupisco/Xare-a-Link
PHP | 421 lines | 324 code | 28 blank | 69 comment | 18 complexity | 13f6a4a5ee91708aff33303ecc1b40c5 MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author Rick Ellis
  9. * @copyright Copyright (c) 2006, EllisLab, Inc.
  10. * @license http://www.codeignitor.com/user_guide/license.html
  11. * @link http://www.codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * CodeIgniter Text Helpers
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Helpers
  21. * @category Helpers
  22. * @author Rick Ellis
  23. * @link http://www.codeigniter.com/user_guide/helpers/text_helper.html
  24. */
  25. // ------------------------------------------------------------------------
  26. /**
  27. * Word Limiter
  28. *
  29. * Limits a string to X number of words.
  30. *
  31. * @access public
  32. * @param string
  33. * @param integer
  34. * @param string the end character. Usually an ellipsis
  35. * @return string
  36. */
  37. function word_limiter($str, $n = 100, $end_char = '&#8230;')
  38. {
  39. if (strlen($str) < $n)
  40. {
  41. return $str;
  42. }
  43. $words = explode(' ', preg_replace("/\s+/", ' ', preg_replace("/(\r\n|\r|\n)/", " ", $str)));
  44. if (count($words) <= $n)
  45. {
  46. return $str;
  47. }
  48. $str = '';
  49. for ($i = 0; $i < $n; $i++)
  50. {
  51. $str .= $words[$i].' ';
  52. }
  53. return trim($str).$end_char;
  54. }
  55. // ------------------------------------------------------------------------
  56. /**
  57. * Character Limiter
  58. *
  59. * Limits the string based on the character count. Preserves complete words
  60. * so the character count may not be exactly as specified.
  61. *
  62. * @access public
  63. * @param string
  64. * @param integer
  65. * @param string the end character. Usually an ellipsis
  66. * @return string
  67. */
  68. function character_limiter($str, $n = 500, $end_char = '&#8230;')
  69. {
  70. if (strlen($str) < $n)
  71. {
  72. return $str;
  73. }
  74. $str = preg_replace("/\s+/", ' ', preg_replace("/(\r\n|\r|\n)/", " ", $str));
  75. if (strlen($str) <= $n)
  76. {
  77. return $str;
  78. }
  79. $out = "";
  80. foreach (explode(' ', trim($str)) as $val)
  81. {
  82. $out .= $val.' ';
  83. if (strlen($out) >= $n)
  84. {
  85. return trim($out).$end_char;
  86. }
  87. }
  88. }
  89. // ------------------------------------------------------------------------
  90. /**
  91. * High ASCII to Entities
  92. *
  93. * Converts High ascii text and MS Word special characters to character entities
  94. *
  95. * @access public
  96. * @param string
  97. * @return string
  98. */
  99. function ascii_to_entities($str)
  100. {
  101. $count = 1;
  102. $out = '';
  103. $temp = array();
  104. for ($i = 0, $s = strlen($str); $i < $s; $i++)
  105. {
  106. $ordinal = ord($str[$i]);
  107. if ($ordinal < 128)
  108. {
  109. $out .= $str[$i];
  110. }
  111. else
  112. {
  113. if (count($temp) == 0)
  114. {
  115. $count = ($ordinal < 224) ? 2 : 3;
  116. }
  117. $temp[] = $ordinal;
  118. if (count($temp) == $count)
  119. {
  120. $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
  121. $out .= '&#'.$number.';';
  122. $count = 1;
  123. $temp = array();
  124. }
  125. }
  126. }
  127. return $out;
  128. }
  129. // ------------------------------------------------------------------------
  130. /**
  131. * Entities to ASCII
  132. *
  133. * Converts character entities back to ASCII
  134. *
  135. * @access public
  136. * @param string
  137. * @param bool
  138. * @return string
  139. */
  140. function entities_to_ascii($str, $all = TRUE)
  141. {
  142. if (preg_match_all('/\&#(\d+)\;/', $str, $matches))
  143. {
  144. for ($i = 0, $s = count($matches['0']); $i < $s; $i++)
  145. {
  146. $digits = $matches['1'][$i];
  147. $out = '';
  148. if ($digits < 128)
  149. {
  150. $out .= chr($digits);
  151. }
  152. elseif ($digits < 2048)
  153. {
  154. $out .= chr(192 + (($digits - ($digits % 64)) / 64));
  155. $out .= chr(128 + ($digits % 64));
  156. }
  157. else
  158. {
  159. $out .= chr(224 + (($digits - ($digits % 4096)) / 4096));
  160. $out .= chr(128 + ((($digits % 4096) - ($digits % 64)) / 64));
  161. $out .= chr(128 + ($digits % 64));
  162. }
  163. $str = str_replace($matches['0'][$i], $out, $str);
  164. }
  165. }
  166. if ($all)
  167. {
  168. $str = str_replace(array("&amp;", "&lt;", "&gt;", "&quot;", "&apos;", "&#45;"),
  169. array("&","<",">","\"", "'", "-"),
  170. $str);
  171. }
  172. return $str;
  173. }
  174. // ------------------------------------------------------------------------
  175. /**
  176. * Word Censoring Function
  177. *
  178. * Supply a string and an array of disallowed words and any
  179. * matched words will be converted to #### or to the replacement
  180. * word you've submitted.
  181. *
  182. * @access public
  183. * @param string the text string
  184. * @param string the array of censoered words
  185. * @param string the optional replacement value
  186. * @return string
  187. */
  188. function word_censor($str, $censored, $replacement = '')
  189. {
  190. if ( ! is_array($censored))
  191. {
  192. return $str;
  193. }
  194. $str = ' '.$str.' ';
  195. foreach ($censored as $badword)
  196. {
  197. if ($replacement != '')
  198. {
  199. $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/i", $replacement, $str);
  200. }
  201. else
  202. {
  203. $str = preg_replace("/\b(".str_replace('\*', '\w*?', preg_quote($badword)).")\b/ie", "str_repeat('#', strlen('\\1'))", $str);
  204. }
  205. }
  206. return trim($str);
  207. }
  208. // ------------------------------------------------------------------------
  209. /**
  210. * Code Highlighter
  211. *
  212. * Colorizes code strings
  213. *
  214. * @access public
  215. * @param string the text string
  216. * @return string
  217. */
  218. function highlight_code($str)
  219. {
  220. // The highlight string function encodes and highlights
  221. // brackets so we need them to start raw
  222. $str = str_replace(array('&lt;', '&gt;'), array('<', '>'), $str);
  223. // Replace any existing PHP tags to temporary markers so they don't accidentally
  224. // break the string out of PHP, and thus, thwart the highlighting.
  225. $str = str_replace(array('&lt;?php', '?&gt;', '\\'), array('phptagopen', 'phptagclose', 'backslashtmp'), $str);
  226. // The highlight_string function requires that the text be surrounded
  227. // by PHP tags. Since we don't know if A) the submitted text has PHP tags,
  228. // or B) whether the PHP tags enclose the entire string, we will add our
  229. // own PHP tags around the string along with some markers to make replacement easier later
  230. $str = '<?php //tempstart'."\n".$str.'//tempend ?>';
  231. // All the magic happens here, baby!
  232. $str = highlight_string($str, TRUE);
  233. // Prior to PHP 5, the highlight function used icky font tags
  234. // so we'll replace them with span tags.
  235. if (abs(phpversion()) < 5)
  236. {
  237. $str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str);
  238. $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str);
  239. }
  240. // Remove our artificially added PHP
  241. $str = preg_replace("#\<code\>.+?//tempstart\<br />\</span\>#is", "<code>\n", $str);
  242. $str = preg_replace("#\<code\>.+?//tempstart\<br />#is", "<code>\n", $str);
  243. $str = preg_replace("#//tempend.+#is", "</span>\n</code>", $str);
  244. // Replace our markers back to PHP tags.
  245. $str = str_replace(array('phptagopen', 'phptagclose', 'backslashtmp'), array('&lt;?php', '?&gt;', '\\'), $str); //<?
  246. return $str;
  247. }
  248. // ------------------------------------------------------------------------
  249. /**
  250. * Phrase Highlighter
  251. *
  252. * Highlights a phrase within a text string
  253. *
  254. * @access public
  255. * @param string the text string
  256. * @param string the phrase you'd like to highlight
  257. * @param string the openging tag to precede the phrase with
  258. * @param string the closing tag to end the phrase with
  259. * @return string
  260. */
  261. function highlight_phrase($str, $phrase, $tag_open = '<strong>', $tag_close = '</strong>')
  262. {
  263. if ($str == '')
  264. {
  265. return '';
  266. }
  267. if ($phrase != '')
  268. {
  269. return preg_replace('/('.preg_quote($phrase).')/i', $tag_open."\\1".$tag_close, $str);
  270. }
  271. return $str;
  272. }
  273. // ------------------------------------------------------------------------
  274. /**
  275. * Word Wrap
  276. *
  277. * Wraps text at the specified character. Maintains the integrity of words.
  278. * Anything placed between {unwrap}{/unwrap} will not be word wrapped, nor
  279. * will URLs.
  280. *
  281. * @access public
  282. * @param string the text string
  283. * @param integer the number of characters to wrap at
  284. * @return string
  285. */
  286. function word_wrap($str, $charlim = '76')
  287. {
  288. // Se the character limit
  289. if ( ! is_numeric($charlim))
  290. $charlim = 76;
  291. // Reduce multiple spaces
  292. $str = preg_replace("| +|", " ", $str);
  293. // Standardize newlines
  294. $str = preg_replace("/\r\n|\r/", "\n", $str);
  295. // If the current word is surrounded by {unwrap} tags we'll
  296. // strip the entire chunk and replace it with a marker.
  297. $unwrap = array();
  298. if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
  299. {
  300. for ($i = 0; $i < count($matches['0']); $i++)
  301. {
  302. $unwrap[] = $matches['1'][$i];
  303. $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
  304. }
  305. }
  306. // Use PHP's native function to do the initial wordwrap.
  307. // We set the cut flag to FALSE so that any individual words that are
  308. // too long get left alone. In the next step we'll deal with them.
  309. $str = wordwrap($str, $charlim, "\n", FALSE);
  310. // Split the string into individual lines of text and cycle through them
  311. $output = "";
  312. foreach (explode("\n", $str) as $line)
  313. {
  314. // Is the line within the allowed character count?
  315. // If so we'll join it to the output and continue
  316. if (strlen($line) <= $charlim)
  317. {
  318. $output .= $line."\n";
  319. continue;
  320. }
  321. $temp = '';
  322. while((strlen($line)) > $charlim)
  323. {
  324. // If the over-length word is a URL we won't wrap it
  325. if (preg_match("!\[url.+\]|://|wwww.!", $line))
  326. {
  327. break;
  328. }
  329. // Trim the word down
  330. $temp .= substr($line, 0, $charlim-1);
  331. $line = substr($line, $charlim-1);
  332. }
  333. // If $temp contains data it means we had to split up an over-length
  334. // word into smaller chunks so we'll add it back to our current line
  335. if ($temp != '')
  336. {
  337. $output .= $temp.$this->newline.$line;
  338. }
  339. else
  340. {
  341. $output .= $line;
  342. }
  343. $output .= "\n";
  344. }
  345. // Put our markers back
  346. if (count($unwrap) > 0)
  347. {
  348. foreach ($unwrap as $key => $val)
  349. {
  350. $output = str_replace("{{unwrapped".$key."}}", $val, $output);
  351. }
  352. }
  353. // Remove the unwrap tags
  354. $output = str_replace(array('{unwrap}', '{/unwrap}'), '', $output);
  355. return $output;
  356. }
  357. ?>