Derek Allard | 72b58ca | 2007-04-07 20:58:23 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
| 2 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame^] | 3 | * CodeIgniter
|
Derek Allard | 72b58ca | 2007-04-07 20:58:23 +0000 | [diff] [blame] | 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
| 8 | * @author Rick Ellis
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame^] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Allard | 72b58ca | 2007-04-07 20:58:23 +0000 | [diff] [blame] | 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 |
|
| 18 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame^] | 19 | * CodeIgniter String Helpers
|
Derek Allard | 72b58ca | 2007-04-07 20:58:23 +0000 | [diff] [blame] | 20 | *
|
| 21 | * @package CodeIgniter
|
| 22 | * @subpackage Helpers
|
| 23 | * @category Helpers
|
| 24 | * @author Rick Ellis
|
| 25 | * @link http://www.codeigniter.com/user_guide/helpers/string_helper.html
|
| 26 | */
|
| 27 |
|
| 28 | // ------------------------------------------------------------------------
|
| 29 |
|
| 30 | /**
|
| 31 | * Trim Slashes
|
| 32 | *
|
| 33 | * Removes any leading/traling slashes from a string:
|
| 34 | *
|
| 35 | * /this/that/theother/
|
| 36 | *
|
| 37 | * becomes:
|
| 38 | *
|
| 39 | * this/that/theother
|
| 40 | *
|
| 41 | * @access public
|
| 42 | * @param string
|
| 43 | * @return string
|
| 44 | */
|
| 45 | function trim_slashes($str)
|
| 46 | {
|
| 47 | return preg_replace("|^/*(.+?)/*$|", "\\1", $str);
|
| 48 | }
|
| 49 |
|
| 50 | // ------------------------------------------------------------------------
|
| 51 |
|
| 52 | /**
|
| 53 | * Reduce Double Slashes
|
| 54 | *
|
| 55 | * Converts double slashes in a string to a single slash,
|
| 56 | * except those found in http://
|
| 57 | *
|
| 58 | * http://www.some-site.com//index.php
|
| 59 | *
|
| 60 | * becomes:
|
| 61 | *
|
| 62 | * http://www.some-site.com/index.php
|
| 63 | *
|
| 64 | * @access public
|
| 65 | * @param string
|
| 66 | * @return string
|
| 67 | */
|
| 68 | function reduce_double_slashes($str)
|
| 69 | {
|
| 70 | return preg_replace("#([^:])//+#", "\\1/", $str);
|
| 71 | }
|
| 72 |
|
| 73 | // ------------------------------------------------------------------------
|
| 74 |
|
| 75 | /**
|
| 76 | * Create a Random String
|
| 77 | *
|
| 78 | * Useful for generating passwords or hashes.
|
| 79 | *
|
| 80 | * @access public
|
| 81 | * @param string type of random string. Options: alunum, numeric, nozero, unique
|
| 82 | * @param integer number of characters
|
| 83 | * @return string
|
| 84 | */
|
| 85 | function random_string($type = 'alnum', $len = 8)
|
| 86 | {
|
| 87 | switch($type)
|
| 88 | {
|
| 89 | case 'alnum' :
|
| 90 | case 'numeric' :
|
| 91 | case 'nozero' :
|
| 92 |
|
| 93 | switch ($type)
|
| 94 | {
|
| 95 | case 'alnum' : $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
| 96 | break;
|
| 97 | case 'numeric' : $pool = '0123456789';
|
| 98 | break;
|
| 99 | case 'nozero' : $pool = '123456789';
|
| 100 | break;
|
| 101 | }
|
| 102 |
|
| 103 | $str = '';
|
| 104 | for ($i=0; $i < $len; $i++)
|
| 105 | {
|
| 106 | $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
|
| 107 | }
|
| 108 | return $str;
|
| 109 | break;
|
| 110 | case 'unique' : return md5(uniqid(mt_rand()));
|
| 111 | break;
|
| 112 | }
|
| 113 | }
|
| 114 | // ------------------------------------------------------------------------
|
| 115 |
|
| 116 | /**
|
| 117 | * Alternator
|
| 118 | *
|
| 119 | * Allows strings to be alternated. See docs...
|
| 120 | *
|
| 121 | * @access public
|
| 122 | * @param string (as many parameters as needed)
|
| 123 | * @return string
|
| 124 | */
|
| 125 | function alternator()
|
| 126 | {
|
| 127 | static $i;
|
| 128 |
|
| 129 | if (func_num_args() == 0)
|
| 130 | {
|
| 131 | $i = 0;
|
| 132 | return '';
|
| 133 | }
|
| 134 | $args = func_get_args();
|
| 135 | return $args[($i++ % count($args))];
|
| 136 | }
|
| 137 |
|
| 138 | // ------------------------------------------------------------------------
|
| 139 |
|
| 140 | /**
|
| 141 | * Repeater function
|
| 142 | *
|
| 143 | * @access public
|
| 144 | * @param string
|
| 145 | * @param integer number of repeats
|
| 146 | * @return string
|
| 147 | */
|
| 148 | function repeater($data, $num = 1)
|
| 149 | {
|
| 150 | return (($num > 0) ? str_repeat($data, $num) : '');
|
| 151 | }
|
| 152 |
|
| 153 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 154 | ?> |