Random String Generator : CI Helper File



Now, I have this small helper file that I used in a project I was working on using codeigniter where I generated random strings which were used as activation code of users. The helper file is embeded as follows:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// ------------------------------------------------------------------------
/**
* Random String Generator : Helper File for Codeigniter
*
* @author Paras Nath Chaudhary
* @link https://gist.github.com/opnchaudhary/4995012
*
*/
// ------------------------------------------------------------------------
/*
Documentation:
=============
1.
$this->load->helper('rand_helper');
$randomString=generateRandomString();
echo $randomString;
2.
$this->load->helper('rand_helper');
$randomString=generateRandomString(14);
echo $randomString;
*/
if ( ! function_exists('generateRandomString'))
{
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
}
/* End of file rand_helper.php */
view raw rand_helper.php hosted with ❤ by GitHub
Using this is very easy just copy the code in a file rand_helper.php and put it in helpers directory and load the helper file using $this->load->helper('rand_helper') and call the function generateRandomString()or with length of string to be generated generateRandomString(15). THe default length of string is 10. Its documented in the helper file too. If you need to modify it, please go ahead. :)

Comments

Popular posts from this blog

Automate file upload in Selenium IDE

How To Install and Configure Nextcloud

Google translate adds Nepali Language Conversion