PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/system/helpers/string_helper.php

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