PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/modules/fuel/helpers/MY_string_helper.php

http://github.com/daylightstudio/FUEL-CMS
PHP | 292 lines | 134 code | 33 blank | 125 comment | 20 complexity | d9ecc701dbc17447193db59a69c09411 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * FUEL CMS
  4. * http://www.getfuelcms.com
  5. *
  6. * An open source Content Management System based on the
  7. * Codeigniter framework (http://codeigniter.com)
  8. *
  9. * @package FUEL CMS
  10. * @author David McReynolds @ Daylight Studio
  11. * @copyright Copyright (c) 2018, Daylight Studio LLC.
  12. * @license http://docs.getfuelcms.com/general/license
  13. * @link http://www.getfuelcms.com
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * FUEL String Helper
  19. *
  20. * This helper extends CI's string helper
  21. *
  22. *
  23. * @package FUEL CMS
  24. * @subpackage Helpers
  25. * @category Helpers
  26. * @author David McReynolds @ Daylight Studio
  27. * @link http://docs.getfuelcms.com/helpers/my_string_helper
  28. */
  29. // --------------------------------------------------------------------
  30. if (!function_exists('eval_string'))
  31. {
  32. /**
  33. * Evaluates a strings PHP code. Used especially for outputting FUEL page data
  34. *
  35. * @param string string to evaluate
  36. * @param mixed variables to pass to the string
  37. * @return string
  38. */
  39. function eval_string($str, $vars = array())
  40. {
  41. $CI =& get_instance();
  42. extract($CI->load->get_vars()); // extract cached variables
  43. extract($vars);
  44. // fix XML
  45. $str = str_replace('<?xml', '<@xml', $str);
  46. ob_start();
  47. if ((bool) @ini_get('short_open_tag') === FALSE AND $CI->config->item('rewrite_short_tags') == TRUE)
  48. {
  49. $str = eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', $str)).'<?php ');
  50. }
  51. else
  52. {
  53. $str = eval('?>'.$str.'<?php ');
  54. }
  55. $str = ob_get_clean();
  56. // change XML back
  57. $str = str_replace('<@xml', '<?xml', $str);
  58. return $str;
  59. }
  60. }
  61. // --------------------------------------------------------------------
  62. if (!function_exists('pluralize'))
  63. {
  64. /**
  65. * Add an s to the end of s string based on the number
  66. *
  67. * @param int number to compare against to determine if it needs to be plural
  68. * @param string string to evaluate
  69. * @param string plural value to add
  70. * @return string
  71. */
  72. function pluralize($num, $str = '', $plural = 's')
  73. {
  74. if (is_array($num))
  75. {
  76. $num = count($num);
  77. }
  78. if ($num != 1)
  79. {
  80. $str .= $plural;
  81. }
  82. return $str;
  83. }
  84. }
  85. // --------------------------------------------------------------------
  86. if (!function_exists('strip_whitespace'))
  87. {
  88. /**
  89. * Strips extra whitespace from a string
  90. *
  91. * @param string
  92. * @return string
  93. */
  94. function strip_whitespace($str)
  95. {
  96. return trim(preg_replace('/\s\s+|\n/m', '', $str));
  97. }
  98. }
  99. // --------------------------------------------------------------------
  100. if (!function_exists('trim_multiline'))
  101. {
  102. /**
  103. * Trims extra whitespace from the end and beginning of a string on multiple lines
  104. *
  105. * @param string
  106. * @return string
  107. */
  108. function trim_multiline($str)
  109. {
  110. return trim(implode("\n", array_map('trim', explode("\n", $str))));
  111. }
  112. }
  113. // --------------------------------------------------------------------
  114. if (!function_exists('smart_ucwords'))
  115. {
  116. /**
  117. * Converts words to title case and allows for exceptions
  118. *
  119. * @param string string to evaluate
  120. * @param mixed variables to pass to the string
  121. * @return string
  122. */
  123. function smart_ucwords($str, $exceptions = array('of', 'the'))
  124. {
  125. $out = "";
  126. $i = 0;
  127. foreach (explode(" ", $str) as $word)
  128. {
  129. $out .= (!in_array($word, $exceptions) OR $i == 0) ? strtoupper($word{0}) . substr($word, 1) . " " : $word . " ";
  130. $i++;
  131. }
  132. return rtrim($out);
  133. }
  134. }
  135. // --------------------------------------------------------------------
  136. if (!function_exists('zap_gremlins'))
  137. {
  138. /**
  139. * Removes "Gremlin" characters
  140. *
  141. * (hidden control characters that the remove_invisible_characters function misses)
  142. *
  143. * @param string string to evaluate
  144. * @param string the value used to replace a gremlin
  145. * @return string
  146. */
  147. function zap_gremlins($str, $replace = '')
  148. {
  149. // there is a hidden bullet looking thingy that photoshop likes to include in it's text'
  150. // the remove_invisible_characters doesn't seem to remove this
  151. $str = preg_replace('/[^\x0A\x0D\x20-\x7E]/', $replace, $str);
  152. return $str;
  153. }
  154. }
  155. // --------------------------------------------------------------------
  156. if (!function_exists('strip_javascript'))
  157. {
  158. /**
  159. * Removes javascript from a string
  160. *
  161. * @param string string to remove javascript
  162. * @return string
  163. */
  164. function strip_javascript($str)
  165. {
  166. $str = preg_replace('#<script[^>]*>.*?</script>#is', '', $str);
  167. return $str;
  168. }
  169. }
  170. // --------------------------------------------------------------------
  171. if (!function_exists('safe_htmlentities'))
  172. {
  173. /**
  174. * Safely converts a string's entities without encoding HTML tags and quotes
  175. *
  176. * @param string string to evaluate
  177. * @param boolean determines whether to encode the ampersand or not
  178. * @return string
  179. */
  180. function safe_htmlentities($str, $protect_amp = TRUE)
  181. {
  182. // convert all hex single quotes to numeric ...
  183. // this was due to an issue we saw with htmlentities still encoding it's ampersand again'...
  184. // but was inconsistent across different environments and versions... not sure the issue
  185. // may need to look into other hex characters
  186. $str = str_replace('&#x27;', '&#39;', $str);
  187. // setup temp markers for existing encoded tag brackets
  188. $find = array('&lt;','&gt;');
  189. $replace = array('__TEMP_LT__','__TEMP_GT__');
  190. $str = str_replace($find,$replace, $str);
  191. // encode just &
  192. if ($protect_amp)
  193. {
  194. $str = preg_replace('/&(?![a-z#]+;)/i', '__TEMP_AMP__', $str);
  195. }
  196. // safely translate now
  197. if (version_compare(PHP_VERSION, '5.2.3', '>='))
  198. {
  199. //$str = htmlspecialchars($str, ENT_NOQUOTES, 'UTF-8', FALSE);
  200. $str = htmlentities($str, ENT_NOQUOTES, config_item('charset'), FALSE);
  201. }
  202. else
  203. {
  204. $str = preg_replace('/&(?!(?:#\d++|[a-z]++);)/ui', '&amp;', $str);
  205. $str = str_replace(array('<', '>'), array('&lt;', '&gt;'), $str);
  206. }
  207. // translate everything back
  208. $str = str_replace($find, array('<','>'), $str);
  209. $str = str_replace($replace, $find, $str);
  210. if ($protect_amp)
  211. {
  212. $str = str_replace('__TEMP_AMP__', '&', $str);
  213. }
  214. return $str;
  215. }
  216. }
  217. // --------------------------------------------------------------------
  218. /**
  219. * Convert PHP syntax to templating syntax
  220. *
  221. * @param string string to evaluate
  222. * @param string the templating engine to use
  223. * @return string
  224. */
  225. function php_to_template_syntax($str, $engine = NULL)
  226. {
  227. $CI =& get_instance();
  228. if (empty($engine))
  229. {
  230. $engine = $CI->fuel->config('parser_engine');
  231. }
  232. return $CI->fuel->parser->set_engine($engine)->php_to_syntax($str);
  233. }
  234. // --------------------------------------------------------------------
  235. /**
  236. * Convert string to templating syntax
  237. *
  238. * @param string string to evaluate
  239. * @param array variables to parse with string
  240. * @param string the templating engine to use
  241. * @param array an array of configuration variables like compile_dir, delimiters, allowed_functions, refs and data
  242. * @return string
  243. */
  244. function parse_template_syntax($str, $vars = array(), $engine = NULL, $config = array())
  245. {
  246. $CI =& get_instance();
  247. // for backwards compatibility
  248. if ($engine === TRUE)
  249. {
  250. $engine = 'ci';
  251. }
  252. elseif (empty($engine))
  253. {
  254. $engine = $CI->fuel->config('parser_engine');
  255. }
  256. return $CI->fuel->parser->set_engine($engine, $config)->parse_string($str, $vars, TRUE);
  257. }
  258. /* End of file MY_string_helper.php */
  259. /* Location: ./modules/fuel/helpers/MY_string_helper.php */