Source for file string_functions.php

Documentation is available at string_functions.php

  1. <?php
  2. /**
  3.  * CodeIgniter
  4.  *
  5.  * An open source application development framework for PHP 4.3.2 or newer
  6.  *
  7.  * @package        CodeIgniter
  8.  * @subpackage     Helpers
  9.  * @author        ExpressionEngine Dev Team
  10.  * @copyright    Copyright (c) 2006, EllisLab, Inc.
  11.  * @license        http://codeigniter.com/user_guide/license.html
  12.  * @link        http://codeigniter.com
  13.  * @since        Version 1.0
  14.  * @filesource
  15.  */
  16.  
  17. // ------------------------------------------------------------------------
  18.  
  19. /**
  20.  * CodeIgniter String Helpers
  21.  *
  22.  * @author        ExpressionEngine Dev Team
  23.  * @link        http://codeigniter.com/user_guide/helpers/string_helper.html
  24.  */
  25.  
  26. // ------------------------------------------------------------------------
  27.  
  28. if (function_exists('trim_slashes')) {
  29.     /**
  30.      * Trim Slashes
  31.      *
  32.      * Removes any leading/traling slashes from a string:
  33.      *
  34.      * /this/that/theother/
  35.      *
  36.      * becomes:
  37.      *
  38.      * this/that/theother
  39.      *
  40.      * @access    public
  41.      * @param    string 
  42.      * @return    string 
  43.      */    
  44.     function trim_slashes($str)
  45.     {
  46.         return trim($str'/');
  47.     
  48. }
  49.     
  50. // ------------------------------------------------------------------------
  51.  
  52. if (function_exists('strip_slashes')) {
  53.     /**
  54.      * Strip Slashes
  55.      *
  56.      * Removes slashes contained in a string or in an array
  57.      *
  58.      * @access    public
  59.      * @param    mixed    string or array
  60.      * @return    mixed    string or array
  61.      */    
  62.     function strip_slashes($str)
  63.     {
  64.         if (is_array($str))
  65.         {    
  66.             foreach ($str as $key => $val)
  67.             {
  68.                 $str[$keystrip_slashes($val);
  69.             }
  70.         }
  71.         else
  72.         {
  73.             $str stripslashes($str);
  74.         }
  75.     
  76.         return $str;
  77.     }
  78. }
  79.  
  80. // ------------------------------------------------------------------------
  81.  
  82. if (function_exists('escape')) {
  83.     /**
  84.      * Escape Input
  85.      *
  86.      * Prepara una/s cadena/s para ser enviada/s al HTML.
  87.      *
  88.      * @access    public
  89.      * @param    mixed    string or array $str
  90.      * @param    string $charset 
  91.      * @return    mixed    string or array
  92.      */    
  93.     function escape($str$charset='UTF-8')
  94.     {
  95.         if (is_array($str))
  96.         {    
  97.             foreach ($str as $key => $val)
  98.             {
  99.                 if (is_array($val)) {
  100.                     $str[$keyescape($val$charset);
  101.                 }
  102.                 else {
  103.                     $str[$keystripslashes($val);
  104.                     $str[$keyhtmlentities($str[$key]ENT_QUOTES$charset);
  105.                 }
  106.             }
  107.         }
  108.         else
  109.         {
  110.             $str stripslashes($str);
  111.             $str htmlentities($strENT_QUOTES$charset);
  112.             
  113.         }
  114.     
  115.         return $str;
  116.     }
  117. }
  118.  
  119. // ------------------------------------------------------------------------
  120.  
  121. if (function_exists('escape_input')) {
  122.     /**
  123.      * Escape Input
  124.      *
  125.      * Prepara una/s cadena/s para ser enviada/s a un input.
  126.      *
  127.      * @access    public
  128.      * @param    mixed    string or array $str
  129.      * @param    string $charset 
  130.      * @return    mixed    string or array
  131.      */    
  132.     function escape_input($str$charset='UTF-8')
  133.     {
  134.         if (is_array($str))
  135.         {    
  136.             foreach ($str as $key => $val)
  137.             {
  138.                 $str[$keystripslashes($val);
  139.                 $str[$keyhtmlentities($str[$key]ENT_QUOTES$charset);
  140.                 
  141.             }
  142.         }
  143.         else
  144.         {
  145.             $str stripslashes($str);
  146.             $str htmlentities($strENT_QUOTES$charset);
  147.             
  148.         }
  149.     
  150.         return $str;
  151.     }
  152. }
  153.  
  154. // ------------------------------------------------------------------------
  155.  
  156. if (function_exists('escape_jsvar')) {
  157.     /**
  158.      * Escape Input
  159.      *
  160.      * Prepara una/s cadena/s para ser enviada/s a una variable javascript.
  161.      *
  162.      * @access    public
  163.      * @param    mixed    string or array $str
  164.      * @param    string $charset 
  165.      * @return    mixed    string or array
  166.      */    
  167.     function escape_jsvar($str$charset='UTF-8')
  168.     {
  169.         if (is_array($str))
  170.         {    
  171.             foreach ($str as $key => $val)
  172.             {
  173.                 $str[$keystripslashes($val);
  174.                 $str[$keyaddcslashes($val"\0..\37\n\r");
  175.                 
  176.             }
  177.         }
  178.         else
  179.         {
  180.             $str stripslashes($str);
  181.             $str addcslashes($str"\0..\37\n\r");
  182.             
  183.         }
  184.     
  185.         return $str;
  186.     }
  187. }
  188.  
  189. // ------------------------------------------------------------------------
  190.  
  191. if (function_exists('strip_quotes')) {
  192.     /**
  193.      * Strip Quotes
  194.      *
  195.      * Removes single and double quotes from a string
  196.      *
  197.      * @access    public
  198.      * @param    string 
  199.      * @return    string 
  200.      */    
  201.     function strip_quotes($str)
  202.     {
  203.         return str_replace(array('"'"'")''$str);
  204.     }
  205. }
  206.  
  207. // ------------------------------------------------------------------------
  208.  
  209. if (function_exists('quotes_to_entities')) {
  210.     /**
  211.      * Quotes to Entities
  212.      *
  213.      * Converts single and double quotes to entities
  214.      *
  215.      * @access    public
  216.      * @param    string 
  217.      * @return    string 
  218.      */    
  219.     function quotes_to_entities($str)
  220.     {    
  221.         return str_replace(array("\'","\"","'",'"')array("&#39;","&quot;","&#39;","&quot;")$str);
  222.     }
  223. }
  224.  
  225. // ------------------------------------------------------------------------
  226.  
  227. if (function_exists('reduce_double_slashes')) {
  228.     /**
  229.      * Reduce Double Slashes
  230.      *
  231.      * Converts double slashes in a string to a single slash,
  232.      * except those found in http://
  233.      *
  234.      * http://www.some-site.com//index.php
  235.      *
  236.      * becomes:
  237.      *
  238.      * http://www.some-site.com/index.php
  239.      *
  240.      * @access    public
  241.      * @param    string 
  242.      * @return    string 
  243.      */    
  244.     function reduce_double_slashes($str)
  245.     {
  246.         return preg_replace("#([^:])//+#""\\1/"$str);
  247.     }
  248. }
  249.     
  250. // ------------------------------------------------------------------------
  251.  
  252. if (function_exists('reduce_multiples')) {
  253.     /**
  254.      * Reduce Multiples
  255.      *
  256.      * Reduces multiple instances of a particular character.  Example:
  257.      *
  258.      * Fred, Bill,, Joe, Jimmy
  259.      *
  260.      * becomes:
  261.      *
  262.      * Fred, Bill, Joe, Jimmy
  263.      *
  264.      * @access    public
  265.      * @param    string 
  266.      * @param    string    the character you wish to reduce
  267.      * @param    bool    TRUE/FALSE - whether to trim the character from the beginning/end
  268.      * @return    string 
  269.      */    
  270.     function reduce_multiples($str$character ','$trim FALSE)
  271.     {
  272.         $str preg_replace('#'.preg_quote($character'#').'{2,}#'$character$str);
  273.  
  274.         if ($trim === TRUE)
  275.         {
  276.             $str trim($str$character);
  277.         }
  278.     
  279.         return $str;
  280.     }
  281. }
  282.     
  283. // ------------------------------------------------------------------------
  284.  
  285. if (function_exists('random_string')) {
  286.     /**
  287.      * Create a Random String
  288.      *
  289.      * Useful for generating passwords or hashes.
  290.      *
  291.      * @access    public
  292.      * @param    string     type of random string.  Options: alunum, numeric, nozero, unique
  293.      * @param    integer    number of characters
  294.      * @return    string 
  295.      */
  296.     function random_string($type 'alnum'$len 8)
  297.     {                    
  298.         switch($type)
  299.         {
  300.             case 'alnum'    :
  301.             case 'numeric'    :
  302.             case 'nozero'    :
  303.         
  304.                     switch ($type)
  305.                     {
  306.                         case 'alnum'    :    $pool '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  307.                             break;
  308.                         case 'numeric'    :    $pool '0123456789';
  309.                             break;
  310.                         case 'nozero'    :    $pool '123456789';
  311.                             break;
  312.                     }
  313.  
  314.                     $str '';
  315.                     for ($i=0$i $len$i++)
  316.                     {
  317.                         $str .= substr($poolmt_rand(0strlen($pool-1)1);
  318.                     }
  319.                     return $str;
  320.               break;
  321.             case 'unique' return md5(uniqid(mt_rand()));
  322.               break;
  323.         }
  324.     }
  325. }
  326.  
  327. // ------------------------------------------------------------------------
  328.  
  329. if (function_exists('alternator'))
  330. {
  331.     /**
  332.      * Alternator
  333.      *
  334.      * Allows strings to be alternated.  See docs...
  335.      *
  336.      * @access    public
  337.      * @param    string (as many parameters as needed)
  338.      * @return    string 
  339.      */    
  340.     function alternator()
  341.     {
  342.         static $i;    
  343.  
  344.         if (func_num_args(== 0)
  345.         {
  346.             $i 0;
  347.             return '';
  348.         }
  349.         $args func_get_args();
  350.         return $args[($i++ % count($args))];
  351.     }
  352. }
  353.  
  354. // ------------------------------------------------------------------------
  355.  
  356. if (function_exists('repeater')) {
  357.     /**
  358.      * Repeater function
  359.      *
  360.      * @access    public
  361.      * @param    string 
  362.      * @param    integer    number of repeats
  363.      * @return    string 
  364.      */    
  365.     function repeater($data$num 1)
  366.     {
  367.         return (($num 0str_repeat($data$num'');
  368.     
  369. }
  370.  
  371.  
  372. if (function_exists('strip_returns')) {
  373.     /**
  374.      * Strip Returns
  375.      * Quita los retornos de carro y saltos de linea de la cadena $str.
  376.      *
  377.      * @access    public
  378.      * @param    string 
  379.      * @return    string 
  380.      */    
  381.     function strip_returns($str)
  382.     {
  383.         return str_replace(array("\r""\n""\r\n")''$str);
  384.     }
  385. }
  386.  
  387. if (function_exists('string_ai')) {
  388.     /**
  389.      * String AI
  390.      * Devuelve una cadena formateada para usar en PREG de modo que sea insensitiva
  391.      * a acentos.
  392.      * 
  393.      * Funciona en UTF8
  394.      *
  395.      * @access    public
  396.      * @param    string 
  397.      * @return    string 
  398.      */    
  399.     function string_ai($str{
  400.         $return $str;
  401.         
  402.         $return preg_replace('/a|à|á|â|ã|ä|å|ǻ|ā|ă|ą/iu''[aàáâãäåǻāăą]'$return);
  403.         $return preg_replace('/e|è|é|ê|ë/iu''[eèéêë]'$return);
  404.         $return preg_replace('/i|ì|í|î|ï/iu''[iìíîï]'$return);
  405.         $return preg_replace('/o|ò|ó|ô|ö/iu''[oòóôö]'$return);
  406.         $return preg_replace('/u|ù|ú|û|ü/iu''[uùúûü]'$return);
  407.         
  408.         return $return;
  409.     }
  410. }
  411.  
  412. if (function_exists('ibm850_ansi')) {
  413.     /**
  414.      * Convierte una cadena en IBM850 a ANSI
  415.      *
  416.      * @access    public
  417.      * @param    string 
  418.      * @return    string 
  419.      */    
  420.     function ibm850_ansi($cStr{
  421.         $aConv array(128=>199,129=>252,130=>233,131=>226,132=>228,133=>224,134=>229,135=>231,136=>234,137=>235,138=>232,139=>239,140=>238,141=>236,142=>196,143=>197,144=>201,145=>230,146=>198,147=>244,148=>246,149=>242,150=>251,151=>249,152=>255,153=>214,154=>220,155=>248,156=>163,157=>216,158=>215,159=>402,160=>225,161=>237,162=>243,163=>250,164=>241,165=>209,166=>170,167=>186,168=>191,169=>174,170=>172,171=>189,172=>188,173=>161,174=>171,175=>187,176=>9617,177=>9618,178=>9619,179=>9474,180=>9508,181=>193,182=>194,183=>192,184=>169,185=>9571,186=>9553,187=>9559,188=>9565,189=>162,190=>165,191=>9488,192=>9492,193=>9524,194=>9516,195=>9500,196=>9472,197=>9532,198=>227,199=>195,200=>9562,201=>9556,202=>9577,203=>9574,204=>9568,205=>9552,206=>9580,207=>164,208=>240,209=>208,210=>202,211=>203,212=>200,213=>305,214=>205,215=>206,216=>207,217=>9496,218=>9484,219=>9608,220=>9604,221=>166,222=>204,223=>9600,224=>211,225=>223,226=>212,227=>210,228=>245,229=>213,230=>181,231=>254,232=>222,233=>218,234=>219,235=>217,236=>253,237=>221,238=>175,239=>180,240=>173,241=>177,242=>8215,243=>190,244=>182,245=>167,246=>247,247=>184,248=>176,249=>168,250=>183,251=>185,252=>179,253=>178,254=>9632,255=>160);
  422.         
  423.         $nCant strlen($cStr);
  424.         for ($i=0$i<$nCant++$i{
  425.             if (isset($aConv[ord($cStr{$i})])) {
  426.                 $cStr{$ichr($aConv[ord($cStr{$i})]);
  427.             }
  428.         }
  429.         
  430.         return $cStr;
  431.     }
  432. }
  433. ?>

Documentation generated on Tue, 22 Nov 2011 13:29:00 -0200 by phpDocumentor 1.4.3