PageRenderTime 49ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/string_helper.php

http://github.com/imagecms/ImageCMS
PHP | 308 lines | 140 code | 32 blank | 136 comment | 15 complexity | c70b86a5cc91e1e913404e36c561e09e MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.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.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
  10. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  11. * @license http://codeigniter.com/user_guide/license.html
  12. * @link http://codeigniter.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * CodeIgniter String Helpers
  19. *
  20. * @package CodeIgniter
  21. * @subpackage Helpers
  22. * @category Helpers
  23. * @author EllisLab Dev Team
  24. * @link http://codeigniter.com/user_guide/helpers/string_helper.html
  25. */
  26. // ------------------------------------------------------------------------
  27. /**
  28. * Trim Slashes
  29. *
  30. * Removes any leading/trailing slashes from a string:
  31. *
  32. * /this/that/theother/
  33. *
  34. * becomes:
  35. *
  36. * this/that/theother
  37. *
  38. * @access public
  39. * @param string
  40. * @return string
  41. */
  42. if ( ! function_exists('trim_slashes'))
  43. {
  44. function trim_slashes($str)
  45. {
  46. return trim($str, '/');
  47. }
  48. }
  49. // ------------------------------------------------------------------------
  50. /**
  51. * Strip Slashes
  52. *
  53. * Removes slashes contained in a string or in an array
  54. *
  55. * @access public
  56. * @param mixed string or array
  57. * @return mixed string or array
  58. */
  59. if ( ! function_exists('strip_slashes'))
  60. {
  61. function strip_slashes($str)
  62. {
  63. if (is_array($str))
  64. {
  65. foreach ($str as $key => $val)
  66. {
  67. $str[$key] = strip_slashes($val);
  68. }
  69. }
  70. else
  71. {
  72. $str = stripslashes($str);
  73. }
  74. return $str;
  75. }
  76. }
  77. // ------------------------------------------------------------------------
  78. /**
  79. * Strip Quotes
  80. *
  81. * Removes single and double quotes from a string
  82. *
  83. * @access public
  84. * @param string
  85. * @return string
  86. */
  87. if ( ! function_exists('strip_quotes'))
  88. {
  89. function strip_quotes($str)
  90. {
  91. return str_replace(array('"', "'"), '', $str);
  92. }
  93. }
  94. // ------------------------------------------------------------------------
  95. /**
  96. * Quotes to Entities
  97. *
  98. * Converts single and double quotes to entities
  99. *
  100. * @access public
  101. * @param string
  102. * @return string
  103. */
  104. if ( ! function_exists('quotes_to_entities'))
  105. {
  106. function quotes_to_entities($str)
  107. {
  108. return str_replace(array("\'","\"","'",'"'), array("&#39;","&quot;","&#39;","&quot;"), $str);
  109. }
  110. }
  111. // ------------------------------------------------------------------------
  112. /**
  113. * Reduce Double Slashes
  114. *
  115. * Converts double slashes in a string to a single slash,
  116. * except those found in http://
  117. *
  118. * http://www.some-site.com//index.php
  119. *
  120. * becomes:
  121. *
  122. * http://www.some-site.com/index.php
  123. *
  124. * @access public
  125. * @param string
  126. * @return string
  127. */
  128. if ( ! function_exists('reduce_double_slashes'))
  129. {
  130. function reduce_double_slashes($str)
  131. {
  132. return preg_replace("#(^|[^:])//+#", "\\1/", $str);
  133. }
  134. }
  135. // ------------------------------------------------------------------------
  136. /**
  137. * Reduce Multiples
  138. *
  139. * Reduces multiple instances of a particular character. Example:
  140. *
  141. * Fred, Bill,, Joe, Jimmy
  142. *
  143. * becomes:
  144. *
  145. * Fred, Bill, Joe, Jimmy
  146. *
  147. * @access public
  148. * @param string
  149. * @param string the character you wish to reduce
  150. * @param bool TRUE/FALSE - whether to trim the character from the beginning/end
  151. * @return string
  152. */
  153. if ( ! function_exists('reduce_multiples'))
  154. {
  155. function reduce_multiples($str, $character = ',', $trim = FALSE)
  156. {
  157. $str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str);
  158. if ($trim === TRUE)
  159. {
  160. $str = trim($str, $character);
  161. }
  162. return $str;
  163. }
  164. }
  165. // ------------------------------------------------------------------------
  166. /**
  167. * Create a Random String
  168. *
  169. * Useful for generating passwords or hashes.
  170. *
  171. * @access public
  172. * @param string type of random string. basic, alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1
  173. * @param integer number of characters
  174. * @return string
  175. */
  176. if ( ! function_exists('random_string'))
  177. {
  178. function random_string($type = 'alnum', $len = 8)
  179. {
  180. switch($type)
  181. {
  182. case 'basic' : return mt_rand();
  183. break;
  184. case 'alnum' :
  185. case 'numeric' :
  186. case 'nozero' :
  187. case 'alpha' :
  188. switch ($type)
  189. {
  190. case 'alpha' : $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  191. break;
  192. case 'alnum' : $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  193. break;
  194. case 'numeric' : $pool = '0123456789';
  195. break;
  196. case 'nozero' : $pool = '123456789';
  197. break;
  198. }
  199. $str = '';
  200. for ($i=0; $i < $len; $i++)
  201. {
  202. $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
  203. }
  204. return $str;
  205. break;
  206. case 'unique' :
  207. case 'md5' :
  208. return md5(uniqid(mt_rand()));
  209. break;
  210. case 'encrypt' :
  211. case 'sha1' :
  212. $CI =& get_instance();
  213. $CI->load->helper('security');
  214. return do_hash(uniqid(mt_rand(), TRUE), 'sha1');
  215. break;
  216. }
  217. }
  218. }
  219. // ------------------------------------------------------------------------
  220. /**
  221. * Add's _1 to a string or increment the ending number to allow _2, _3, etc
  222. *
  223. * @param string $str required
  224. * @param string $separator What should the duplicate number be appended with
  225. * @param string $first Which number should be used for the first dupe increment
  226. * @return string
  227. */
  228. function increment_string($str, $separator = '_', $first = 1)
  229. {
  230. preg_match('/(.+)'.$separator.'([0-9]+)$/', $str, $match);
  231. return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first;
  232. }
  233. // ------------------------------------------------------------------------
  234. /**
  235. * Alternator
  236. *
  237. * Allows strings to be alternated. See docs...
  238. *
  239. * @access public
  240. * @param string (as many parameters as needed)
  241. * @return string
  242. */
  243. if ( ! function_exists('alternator'))
  244. {
  245. function alternator()
  246. {
  247. static $i;
  248. if (func_num_args() == 0)
  249. {
  250. $i = 0;
  251. return '';
  252. }
  253. $args = func_get_args();
  254. return $args[($i++ % count($args))];
  255. }
  256. }
  257. // ------------------------------------------------------------------------
  258. /**
  259. * Repeater function
  260. *
  261. * @access public
  262. * @param string
  263. * @param integer number of repeats
  264. * @return string
  265. */
  266. if ( ! function_exists('repeater'))
  267. {
  268. function repeater($data, $num = 1)
  269. {
  270. return (($num > 0) ? str_repeat($data, $num) : '');
  271. }
  272. }
  273. /* End of file string_helper.php */
  274. /* Location: ./system/helpers/string_helper.php */