PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/RP_1073/system/helpers/string_helper.php

https://github.com/holsinger/openfloor
PHP | 154 lines | 52 code | 16 blank | 86 comment | 5 complexity | 3fd3cbfe66da44e8bde91643e2eb6ba2 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 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author Rick Ellis
  9. * @copyright Copyright (c) 2006, EllisLab, Inc.
  10. * @license http://www.codeignitor.com/user_guide/license.html
  11. * @link http://www.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 Rick Ellis
  23. * @link http://www.codeigniter.com/user_guide/helpers/string_helper.html
  24. */
  25. // ------------------------------------------------------------------------
  26. /**
  27. * Trim Slashes
  28. *
  29. * Removes any leading/traling 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. function trim_slashes($str)
  42. {
  43. return trim($str, '/');
  44. }
  45. // ------------------------------------------------------------------------
  46. /**
  47. * Reduce Double Slashes
  48. *
  49. * Converts double slashes in a string to a single slash,
  50. * except those found in http://
  51. *
  52. * http://www.some-site.com//index.php
  53. *
  54. * becomes:
  55. *
  56. * http://www.some-site.com/index.php
  57. *
  58. * @access public
  59. * @param string
  60. * @return string
  61. */
  62. function reduce_double_slashes($str)
  63. {
  64. return preg_replace("#([^:])//+#", "\\1/", $str);
  65. }
  66. // ------------------------------------------------------------------------
  67. /**
  68. * Create a Random String
  69. *
  70. * Useful for generating passwords or hashes.
  71. *
  72. * @access public
  73. * @param string type of random string. Options: alunum, numeric, nozero, unique
  74. * @param integer number of characters
  75. * @return string
  76. */
  77. function random_string($type = 'alnum', $len = 8)
  78. {
  79. switch($type)
  80. {
  81. case 'alnum' :
  82. case 'numeric' :
  83. case 'nozero' :
  84. switch ($type)
  85. {
  86. case 'alnum' : $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  87. break;
  88. case 'numeric' : $pool = '0123456789';
  89. break;
  90. case 'nozero' : $pool = '123456789';
  91. break;
  92. }
  93. $str = '';
  94. for ($i=0; $i < $len; $i++)
  95. {
  96. $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
  97. }
  98. return $str;
  99. break;
  100. case 'unique' : return md5(uniqid(mt_rand()));
  101. break;
  102. }
  103. }
  104. // ------------------------------------------------------------------------
  105. /**
  106. * Alternator
  107. *
  108. * Allows strings to be alternated. See docs...
  109. *
  110. * @access public
  111. * @param string (as many parameters as needed)
  112. * @return string
  113. */
  114. function alternator()
  115. {
  116. static $i;
  117. if (func_num_args() == 0)
  118. {
  119. $i = 0;
  120. return '';
  121. }
  122. $args = func_get_args();
  123. return $args[($i++ % count($args))];
  124. }
  125. // ------------------------------------------------------------------------
  126. /**
  127. * Repeater function
  128. *
  129. * @access public
  130. * @param string
  131. * @param integer number of repeats
  132. * @return string
  133. */
  134. function repeater($data, $num = 1)
  135. {
  136. return (($num > 0) ? str_repeat($data, $num) : '');
  137. }
  138. ?>