string_helper.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
  33. * @license https://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * CodeIgniter String Helpers
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Helpers
  44. * @category Helpers
  45. * @author EllisLab Dev Team
  46. * @link https://codeigniter.com/user_guide/helpers/string_helper.html
  47. */
  48. // ------------------------------------------------------------------------
  49. if ( ! function_exists('trim_slashes'))
  50. {
  51. /**
  52. * Trim Slashes
  53. *
  54. * Removes any leading/trailing slashes from a string:
  55. *
  56. * /this/that/theother/
  57. *
  58. * becomes:
  59. *
  60. * this/that/theother
  61. *
  62. * @todo Remove in version 3.1+.
  63. * @deprecated 3.0.0 This is just an alias for PHP's native trim()
  64. *
  65. * @param string
  66. * @return string
  67. */
  68. function trim_slashes($str)
  69. {
  70. return trim($str, '/');
  71. }
  72. }
  73. // ------------------------------------------------------------------------
  74. if ( ! function_exists('strip_slashes'))
  75. {
  76. /**
  77. * Strip Slashes
  78. *
  79. * Removes slashes contained in a string or in an array
  80. *
  81. * @param mixed string or array
  82. * @return mixed string or array
  83. */
  84. function strip_slashes($str)
  85. {
  86. if ( ! is_array($str))
  87. {
  88. return stripslashes($str);
  89. }
  90. foreach ($str as $key => $val)
  91. {
  92. $str[$key] = strip_slashes($val);
  93. }
  94. return $str;
  95. }
  96. }
  97. // ------------------------------------------------------------------------
  98. if ( ! function_exists('strip_quotes'))
  99. {
  100. /**
  101. * Strip Quotes
  102. *
  103. * Removes single and double quotes from a string
  104. *
  105. * @param string
  106. * @return string
  107. */
  108. function strip_quotes($str)
  109. {
  110. return str_replace(array('"', "'"), '', $str);
  111. }
  112. }
  113. // ------------------------------------------------------------------------
  114. if ( ! function_exists('quotes_to_entities'))
  115. {
  116. /**
  117. * Quotes to Entities
  118. *
  119. * Converts single and double quotes to entities
  120. *
  121. * @param string
  122. * @return string
  123. */
  124. function quotes_to_entities($str)
  125. {
  126. return str_replace(array("\'","\"","'",'"'), array("&#39;","&quot;","&#39;","&quot;"), $str);
  127. }
  128. }
  129. // ------------------------------------------------------------------------
  130. if ( ! function_exists('reduce_double_slashes'))
  131. {
  132. /**
  133. * Reduce Double Slashes
  134. *
  135. * Converts double slashes in a string to a single slash,
  136. * except those found in http://
  137. *
  138. * http://www.some-site.com//index.php
  139. *
  140. * becomes:
  141. *
  142. * http://www.some-site.com/index.php
  143. *
  144. * @param string
  145. * @return string
  146. */
  147. function reduce_double_slashes($str)
  148. {
  149. return preg_replace('#(^|[^:])//+#', '\\1/', $str);
  150. }
  151. }
  152. // ------------------------------------------------------------------------
  153. if ( ! function_exists('reduce_multiples'))
  154. {
  155. /**
  156. * Reduce Multiples
  157. *
  158. * Reduces multiple instances of a particular character. Example:
  159. *
  160. * Fred, Bill,, Joe, Jimmy
  161. *
  162. * becomes:
  163. *
  164. * Fred, Bill, Joe, Jimmy
  165. *
  166. * @param string
  167. * @param string the character you wish to reduce
  168. * @param bool TRUE/FALSE - whether to trim the character from the beginning/end
  169. * @return string
  170. */
  171. function reduce_multiples($str, $character = ',', $trim = FALSE)
  172. {
  173. $str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str);
  174. return ($trim === TRUE) ? trim($str, $character) : $str;
  175. }
  176. }
  177. // ------------------------------------------------------------------------
  178. if ( ! function_exists('random_string'))
  179. {
  180. /**
  181. * Create a "Random" String
  182. *
  183. * @param string type of random string. basic, alpha, alnum, numeric, nozero, unique, md5, encrypt and sha1
  184. * @param int number of characters
  185. * @return string
  186. */
  187. function random_string($type = 'alnum', $len = 8)
  188. {
  189. switch ($type)
  190. {
  191. case 'basic':
  192. return mt_rand();
  193. case 'alnum':
  194. case 'numeric':
  195. case 'nozero':
  196. case 'alpha':
  197. switch ($type)
  198. {
  199. case 'alpha':
  200. $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  201. break;
  202. case 'alnum':
  203. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  204. break;
  205. case 'numeric':
  206. $pool = '0123456789';
  207. break;
  208. case 'nozero':
  209. $pool = '123456789';
  210. break;
  211. }
  212. return substr(str_shuffle(str_repeat($pool, ceil($len / strlen($pool)))), 0, $len);
  213. case 'unique': // todo: remove in 3.1+
  214. case 'md5':
  215. return md5(uniqid(mt_rand()));
  216. case 'encrypt': // todo: remove in 3.1+
  217. case 'sha1':
  218. return sha1(uniqid(mt_rand(), TRUE));
  219. }
  220. }
  221. }
  222. // ------------------------------------------------------------------------
  223. if ( ! function_exists('increment_string'))
  224. {
  225. /**
  226. * Add's _1 to a string or increment the ending number to allow _2, _3, etc
  227. *
  228. * @param string required
  229. * @param string What should the duplicate number be appended with
  230. * @param string Which number should be used for the first dupe increment
  231. * @return string
  232. */
  233. function increment_string($str, $separator = '_', $first = 1)
  234. {
  235. preg_match('/(.+)'.preg_quote($separator, '/').'([0-9]+)$/', $str, $match);
  236. return isset($match[2]) ? $match[1].$separator.($match[2] + 1) : $str.$separator.$first;
  237. }
  238. }
  239. // ------------------------------------------------------------------------
  240. if ( ! function_exists('alternator'))
  241. {
  242. /**
  243. * Alternator
  244. *
  245. * Allows strings to be alternated. See docs...
  246. *
  247. * @param string (as many parameters as needed)
  248. * @return string
  249. */
  250. function alternator()
  251. {
  252. static $i;
  253. if (func_num_args() === 0)
  254. {
  255. $i = 0;
  256. return '';
  257. }
  258. $args = func_get_args();
  259. return $args[($i++ % count($args))];
  260. }
  261. }
  262. // ------------------------------------------------------------------------
  263. if ( ! function_exists('repeater'))
  264. {
  265. /**
  266. * Repeater function
  267. *
  268. * @todo Remove in version 3.1+.
  269. * @deprecated 3.0.0 This is just an alias for PHP's native str_repeat()
  270. *
  271. * @param string $data String to repeat
  272. * @param int $num Number of repeats
  273. * @return string
  274. */
  275. function repeater($data, $num = 1)
  276. {
  277. return ($num > 0) ? str_repeat($data, $num) : '';
  278. }
  279. }