Skip to content

Instantly share code, notes, and snippets.

@shouhei
Created March 10, 2014 07:15
Show Gist options
  • Save shouhei/9460724 to your computer and use it in GitHub Desktop.
Save shouhei/9460724 to your computer and use it in GitHub Desktop.
make random strings to use php
<?php
class RandStr{
private $small = 'abcdefghjiklmnopqrstuvwxyz';
private $large = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
private $num = '0123456789';
private $all_ary = array();
function __construct(){
array_push($this->all_ary,$this->small,$this->large,$this->num);
}
public function add_str($param = array()){
foreach($param as $key => $value){
$this->$key = $value;
$this->all_ary[] = $value;
}
}
public function mk_rd_str($length,$param = array('small','large','num')) {
$carset = '';
foreach($param as $val){
$charset .= $this->$val;
}
$charset_length = strlen($charset);
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= $charset[mt_rand(0, $charset_length - 1)];
}
return $string;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment