Last active
January 2, 2016 12:09
-
-
Save gchacaltana/8300983 to your computer and use it in GitHub Desktop.
Get random string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//declaramos la funcion : randomString | |
function randomString($length, $type = '') { | |
// Seleccionamos el tipo de caracteres que deseas que devuelva el string | |
switch ($type) { | |
case 'num': | |
// Solo cuando deseas que devuelva numeros. | |
$salt = '1234567890'; | |
break; | |
case 'lower': | |
// Solo cuando deseas que devuelva letras en minusculas. | |
$salt = 'abcdefghijklmnopqrstuvwxyz'; | |
break; | |
case 'upper': | |
// Solo cuando deseas que devuelva letras en mayusculas. | |
$salt = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
break; | |
default: | |
// Para cuando deseas que la cadena este compuesta por letras y numeros | |
$salt = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; | |
break; | |
} | |
$rand = ''; | |
$i = 0; | |
while ($i < $length) { | |
//Loop hasta que el string aleatorio contenga la longitud ingresada. | |
$num = rand() % strlen($salt); | |
$tmp = substr($salt, $num, 1); | |
$rand = $rand . $tmp; | |
$i++; | |
} | |
//Retorno del string aleatorio. | |
return $rand; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment