PageRenderTime 271ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/system/helpers/string_helper.php

https://gitlab.com/zanderwong/admin
PHP | 284 lines | 117 code | 32 blank | 135 comment | 17 complexity | b10db1f5e73392db43868cf0307d175e 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 - 2014, 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. function trim_slashes($str) {
  43. return trim($str, '/');
  44. }
  45. }
  46. // ------------------------------------------------------------------------
  47. /**
  48. * Strip Slashes
  49. *
  50. * Removes slashes contained in a string or in an array
  51. *
  52. * @access public
  53. * @param mixed string or array
  54. * @return mixed string or array
  55. */
  56. if (!function_exists('strip_slashes')) {
  57. function strip_slashes($str) {
  58. if (is_array($str)) {
  59. foreach ($str as $key => $val) {
  60. $str[$key] = strip_slashes($val);
  61. }
  62. } else {
  63. $str = stripslashes($str);
  64. }
  65. return $str;
  66. }
  67. }
  68. // ------------------------------------------------------------------------
  69. /**
  70. * Strip Quotes
  71. *
  72. * Removes single and double quotes from a string
  73. *
  74. * @access public
  75. * @param string
  76. * @return string
  77. */
  78. if (!function_exists('strip_quotes')) {
  79. function strip_quotes($str) {
  80. return str_replace(array('"', "'"), '', $str);
  81. }
  82. }
  83. // ------------------------------------------------------------------------
  84. /**
  85. * Quotes to Entities
  86. *
  87. * Converts single and double quotes to entities
  88. *
  89. * @access public
  90. * @param string
  91. * @return string
  92. */
  93. if (!function_exists('quotes_to_entities')) {
  94. function quotes_to_entities($str) {
  95. return str_replace(array("\'", "\"", "'", '"'), array("&#39;", "&quot;", "&#39;", "&quot;"), $str);
  96. }
  97. }
  98. // ------------------------------------------------------------------------
  99. /**
  100. * Reduce Double Slashes
  101. *
  102. * Converts double slashes in a string to a single slash,
  103. * except those found in http://
  104. *
  105. * http://www.some-site.com//index.php
  106. *
  107. * becomes:
  108. *
  109. * http://www.some-site.com/index.php
  110. *
  111. * @access public
  112. * @param string
  113. * @return string
  114. */
  115. if (!function_exists('reduce_double_slashes')) {
  116. function reduce_double_slashes($str) {
  117. return preg_replace("#(^|[^:])//+#", "\\1/", $str);
  118. }
  119. }
  120. // ------------------------------------------------------------------------
  121. /**
  122. * Reduce Multiples
  123. *
  124. * Reduces multiple instances of a particular character. Example:
  125. *
  126. * Fred, Bill,, Joe, Jimmy
  127. *
  128. * becomes:
  129. *
  130. * Fred, Bill, Joe, Jimmy
  131. *
  132. * @access public
  133. * @param string
  134. * @param string the character you wish to reduce
  135. * @param bool TRUE/FALSE - whether to trim the character from the beginning/end
  136. * @return string
  137. */
  138. if (!function_exists('reduce_multiples')) {
  139. function reduce_multiples($str, $character = ',', $trim = FALSE) {
  140. $str = preg_replace('#' . preg_quote($character, '#') . '{2,}#', $character, $str);
  141. if ($trim === TRUE) {
  142. $str = trim($str, $character);
  143. }
  144. return $str;
  145. }
  146. }
  147. // ------------------------------------------------------------------------
  148. /**
  149. * Create a Random String
  150. *
  151. * Useful for generating passwords or hashes.
  152. *
  153. * @access public
  154. * @param string type of random string. basic, alpha, alunum, numeric, nozero, unique, md5, encrypt and sha1
  155. * @param integer number of characters
  156. * @return string
  157. */
  158. if (!function_exists('random_string')) {
  159. function random_string($type = 'alnum', $len = 8) {
  160. switch ($type) {
  161. case 'basic' :
  162. return mt_rand();
  163. break;
  164. case 'alnum' :
  165. case 'numeric' :
  166. case 'nozero' :
  167. case 'alpha' :
  168. switch ($type) {
  169. case 'alpha' :
  170. $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  171. break;
  172. case 'alnum' :
  173. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  174. break;
  175. case 'numeric' :
  176. $pool = '0123456789';
  177. break;
  178. case 'nozero' :
  179. $pool = '123456789';
  180. break;
  181. }
  182. $str = '';
  183. for ($i = 0; $i < $len; $i++) {
  184. $str .= substr($pool, mt_rand(0, strlen($pool) - 1), 1);
  185. }
  186. return $str;
  187. break;
  188. case 'unique' :
  189. case 'md5' :
  190. return md5(uniqid(mt_rand()));
  191. break;
  192. case 'encrypt' :
  193. case 'sha1' :
  194. $CI =& get_instance();
  195. $CI->load->helper('security');
  196. return do_hash(uniqid(mt_rand(), TRUE), 'sha1');
  197. break;
  198. }
  199. }
  200. }
  201. // ------------------------------------------------------------------------
  202. /**
  203. * Add's _1 to a string or increment the ending number to allow _2, _3, etc
  204. *
  205. * @param string $str required
  206. * @param string $separator What should the duplicate number be appended with
  207. * @param string $first Which number should be used for the first dupe increment
  208. * @return string
  209. */
  210. function increment_string($str, $separator = '_', $first = 1) {
  211. preg_match('/(.+)' . $separator . '([0-9]+)$/', $str, $match);
  212. return isset($match[2]) ? $match[1] . $separator . ($match[2] + 1) : $str . $separator . $first;
  213. }
  214. // ------------------------------------------------------------------------
  215. /**
  216. * Alternator
  217. *
  218. * Allows strings to be alternated. See docs...
  219. *
  220. * @access public
  221. * @param string (as many parameters as needed)
  222. * @return string
  223. */
  224. if (!function_exists('alternator')) {
  225. function alternator() {
  226. static $i;
  227. if (func_num_args() == 0) {
  228. $i = 0;
  229. return '';
  230. }
  231. $args = func_get_args();
  232. return $args[($i++ % count($args))];
  233. }
  234. }
  235. // ------------------------------------------------------------------------
  236. /**
  237. * Repeater function
  238. *
  239. * @access public
  240. * @param string
  241. * @param integer number of repeats
  242. * @return string
  243. */
  244. if (!function_exists('repeater')) {
  245. function repeater($data, $num = 1) {
  246. return (($num > 0) ? str_repeat($data, $num) : '');
  247. }
  248. }
  249. /* End of file string_helper.php */
  250. /* Location: ./system/helpers/string_helper.php */