function Cadena_aleatoria($Longitud) { #$Caracteres = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'; $Caracteres = 'abcdefghijklmnopqrstuvwxyz1234567890'; // Length of character list $Longitud_caracteres = (strlen($Caracteres) - 1); // Start our string $Cadena = $Caracteres{rand(0, $Longitud_caracteres)}; // Generate random string for ($i = 1; $i < $Longitud; $i = strlen($Cadena)) { // Grab a random character from our list $Caracter = $Caracteres{rand(0, $Longitud_caracteres)}; // Make sure the same two characters don't appear next to each other if ($Caracter != $Cadena{$i - 1}) $Cadena .= $Caracter; } // Return the string return $Cadena; } $Cadena=Cadena_aleatoria(8); echo "Cadena: $Cadena | MD5: ".md5($Cadena);