Skip to content
Derek Jones edited this page Jul 5, 2012 · 7 revisions

Category:Contributions::Plugins

The Random Plugin for CodeIgniter is a simple plugin that allow to generate random string of a specific lenght.

It's composed by two functions, the first help you to generate random strings, and the second to generate random int.

Plugin home : Random Plugin

How to use :

<?php
 
$this->load->plugin('random');

echo generate(32);
echo generateInt(8);
 
?>

Plugin's code :

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
 
/**
 * Ramdom string plugin for CodeIgniter applications
 * @author : Samuel Sanchez, March 2009, http://www.kromack.com/ 
 * @license : free
 * @see http://www.kromack.com/codeigniter/plugin-random-pour-codeigniterplugin-random-pour-codeigniter/ 
 */
 
 
    /**
    * Return a ramdom string of $lenght size.
    *
    * @param int $lenght
    * @return string
    */
    function generate($lenght)
    {
        $ensemble = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4');
 
        if($lenght > sizeof($ensemble)) {
 
            while($lenght > sizeof($ensemble)) {
 
                foreach($ensemble as $row) {
 
                    $ensemble[] = $row;
                }
            }
        }
 
        $ramdom = array_rand($ensemble, $lenght);
 
        $string = '';
 
        foreach($ramdom as $i)
        {
             $string = $string . $ensemble[$i];
        }
 
        return $string;
    }       
 
       /**
    * Return a ramdom int of $lenght size.
    *
    * @param int $lenght
    * @return string
    */
    function generateInt($lenght)
    {
        $ensemble = array('1', '2', '3', '4', '5', '6', '7', '8', '9');
 
        if($lenght > sizeof($ensemble)) {
 
            while($lenght > sizeof($ensemble)) {
 
                foreach($ensemble as $row) {
 
                    $ensemble[] = $row;
                }
            }
        }        
        $ramdom = array_rand($ensemble, $lenght);
 
        $string = '';
 
        foreach($ramdom as $i)
        {
             $string = $string . $ensemble[$i];
        }
 
        return $string;
    } 
 
?>
Clone this wiki locally