string_helper.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2008, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * CodeIgniter String Helpers
00020  *
00021  * @package             CodeIgniter
00022  * @subpackage  Helpers
00023  * @category    Helpers
00024  * @author              ExpressionEngine Dev Team
00025  * @link                http://codeigniter.com/user_guide/helpers/string_helper.html
00026  */
00027 
00028 // ------------------------------------------------------------------------
00029 
00030 /**
00031  * Trim Slashes
00032  *
00033  * Removes any leading/traling slashes from a string:
00034  *
00035  * /this/that/theother/
00036  *
00037  * becomes:
00038  *
00039  * this/that/theother
00040  *
00041  * @access      public
00042  * @param       string
00043  * @return      string
00044  */     
00045 if ( ! function_exists('trim_slashes'))
00046 {
00047         function trim_slashes($str)
00048         {
00049                 return trim($str, '/');
00050         } 
00051 }
00052         
00053 // ------------------------------------------------------------------------
00054 
00055 /**
00056  * Strip Slashes
00057  *
00058  * Removes slashes contained in a string or in an array
00059  *
00060  * @access      public
00061  * @param       mixed   string or array
00062  * @return      mixed   string or array
00063  */     
00064 if ( ! function_exists('strip_slashes'))
00065 {
00066         function strip_slashes($str)
00067         {
00068                 if (is_array($str))
00069                 {       
00070                         foreach ($str as $key => $val)
00071                         {
00072                                 $str[$key] = strip_slashes($val);
00073                         }
00074                 }
00075                 else
00076                 {
00077                         $str = stripslashes($str);
00078                 }
00079         
00080                 return $str;
00081         }
00082 }
00083 
00084 // ------------------------------------------------------------------------
00085 
00086 /**
00087  * Strip Quotes
00088  *
00089  * Removes single and double quotes from a string
00090  *
00091  * @access      public
00092  * @param       string
00093  * @return      string
00094  */     
00095 if ( ! function_exists('strip_quotes'))
00096 {
00097         function strip_quotes($str)
00098         {
00099                 return str_replace(array('"', "'"), '', $str);
00100         }
00101 }
00102 
00103 // ------------------------------------------------------------------------
00104 
00105 /**
00106  * Quotes to Entities
00107  *
00108  * Converts single and double quotes to entities
00109  *
00110  * @access      public
00111  * @param       string
00112  * @return      string
00113  */     
00114 if ( ! function_exists('quotes_to_entities'))
00115 {
00116         function quotes_to_entities($str)
00117         {       
00118                 return str_replace(array("\'","\"","'",'"'), array("&#39;","&quot;","&#39;","&quot;"), $str);
00119         }
00120 }
00121 
00122 // ------------------------------------------------------------------------
00123 /**
00124  * Reduce Double Slashes
00125  *
00126  * Converts double slashes in a string to a single slash,
00127  * except those found in http://
00128  *
00129  * http://www.some-site.com//index.php
00130  *
00131  * becomes:
00132  *
00133  * http://www.some-site.com/index.php
00134  *
00135  * @access      public
00136  * @param       string
00137  * @return      string
00138  */     
00139 if ( ! function_exists('reduce_double_slashes'))
00140 {
00141         function reduce_double_slashes($str)
00142         {
00143                 return preg_replace("#([^:])//+#", "\\1/", $str);
00144         }
00145 }
00146         
00147 // ------------------------------------------------------------------------
00148 
00149 /**
00150  * Reduce Multiples
00151  *
00152  * Reduces multiple instances of a particular character.  Example:
00153  *
00154  * Fred, Bill,, Joe, Jimmy
00155  *
00156  * becomes:
00157  *
00158  * Fred, Bill, Joe, Jimmy
00159  *
00160  * @access      public
00161  * @param       string
00162  * @param       string  the character you wish to reduce
00163  * @param       bool    TRUE/FALSE - whether to trim the character from the beginning/end
00164  * @return      string
00165  */     
00166 if ( ! function_exists('reduce_multiples'))
00167 {
00168         function reduce_multiples($str, $character = ',', $trim = FALSE)
00169         {
00170                 $str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str);
00171 
00172                 if ($trim === TRUE)
00173                 {
00174                         $str = trim($str, $character);
00175                 }
00176 
00177                 return $str;
00178         }
00179 }
00180         
00181 // ------------------------------------------------------------------------
00182 
00183 /**
00184  * Create a Random String
00185  *
00186  * Useful for generating passwords or hashes.
00187  *
00188  * @access      public
00189  * @param       string  type of random string.  Options: alunum, numeric, nozero, unique
00190  * @param       integer number of characters
00191  * @return      string
00192  */
00193 if ( ! function_exists('random_string'))
00194 {       
00195         function random_string($type = 'alnum', $len = 8)
00196         {                                       
00197                 switch($type)
00198                 {
00199                         case 'alnum'    :
00200                         case 'numeric'  :
00201                         case 'nozero'   :
00202                 
00203                                         switch ($type)
00204                                         {
00205                                                 case 'alnum'    :       $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
00206                                                         break;
00207                                                 case 'numeric'  :       $pool = '0123456789';
00208                                                         break;
00209                                                 case 'nozero'   :       $pool = '123456789';
00210                                                         break;
00211                                         }
00212 
00213                                         $str = '';
00214                                         for ($i=0; $i < $len; $i++)
00215                                         {
00216                                                 $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
00217                                         }
00218                                         return $str;
00219                           break;
00220                         case 'unique' : return md5(uniqid(mt_rand()));
00221                           break;
00222                 }
00223         }
00224 }
00225 
00226 // ------------------------------------------------------------------------
00227 
00228 /**
00229  * Alternator
00230  *
00231  * Allows strings to be alternated.  See docs...
00232  *
00233  * @access      public
00234  * @param       string (as many parameters as needed)
00235  * @return      string
00236  */     
00237 if ( ! function_exists('alternator'))
00238 {
00239         function alternator()
00240         {
00241                 static $i;      
00242 
00243                 if (func_num_args() == 0)
00244                 {
00245                         $i = 0;
00246                         return '';
00247                 }
00248                 $args = func_get_args();
00249                 return $args[($i++ % count($args))];
00250         }
00251 }
00252 
00253 // ------------------------------------------------------------------------
00254 
00255 /**
00256  * Repeater function
00257  *
00258  * @access      public
00259  * @param       string
00260  * @param       integer number of repeats
00261  * @return      string
00262  */     
00263 if ( ! function_exists('repeater'))
00264 {
00265         function repeater($data, $num = 1)
00266         {
00267                 return (($num > 0) ? str_repeat($data, $num) : '');
00268         } 
00269 }
00270 
00271 
00272 /* End of file string_helper.php */
00273 /* Location: ./system/helpers/string_helper.php */