src/StringLib.php
/**
     * Generates a random string. This can be useful for password generation 
     * or to create a single-use token for the user to do something 
     * (e.g. click an email link to register).
     * 
     * @param int $numChars - how many characters long the string should be
     * @param int $charOptions - bitwise result of following vars
     *          PASSWORD_DISABLE_LOWER_CASE
     *          PASSWORD_DISABLE_UPPER_CASE
     *          PASSWORD_DISABLE_NUMBERS
     *          PASSWORD_DISABLE_SPECIAL_CHARS
     *
     * @return token - the generated string
     */
Generates a random string. This can be useful for password generation
or to create a single-use token for the user to do something
.

@param int $numChars - how many characters long the string should be
@param int $charOptions - bitwise result of following vars
PASSWORD_DISABLE_LOWER_CASE
PASSWORD_DISABLE_UPPER_CASE
PASSWORD_DISABLE_NUMBERS
PASSWORD_DISABLE_SPECIAL_CHARS

@return token - the generated string
Generates a random string . This can be useful for password generation or to create a single - use token for the user to do something . @param int $numChars - how many characters long the string should be @param int $charOptions - bitwise result of following vars PASSWORD_DISABLE_LOWER_CASE PASSWORD_DISABLE_UPPER_CASE PASSWORD_DISABLE_NUMBERS PASSWORD_DISABLE_SPECIAL_CHARS @return token - the generated string
public static function generateRandomString($numChars, $charOptions=0)
    {
        $userLowerCase   = !($charOptions & self::PASSWORD_DISABLE_LOWER_CASE);
        $useUppercase    = !($charOptions & self::PASSWORD_DISABLE_UPPER_CASE); 
        $useNumbers      = !($charOptions & self::PASSWORD_DISABLE_NUMBERS);
        $useSpecialChars = !($charOptions & self::PASSWORD_DISABLE_SPECIAL_CHARS);
        $lowerCase      = str_split('abcdefghijklmnopqrstuvwxyz', 1);
        $numbers        = str_split('0123456789', 1);
        $capitalLetters = str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZ', 1);
        $specialChars   = str_split('!@#$%^&*(){}[]+-/_', 1);
        $possibleChars = array();
        if ($userLowerCase)
        {
            $possibleChars = array_merge($possibleChars, $lowerCase);
            $requirements['lower_case'] = $lowerCase;
        }
        if ($useUppercase)
        {
            $possibleChars = array_merge($possibleChars, $capitalLetters);
            $requirements['capitals'] = $capitalLetters;
        }
        if ($useNumbers)
        {
            $possibleChars = array_merge($possibleChars, $numbers);
            $requirements['numbers'] = $numbers;
        }
        if ($useSpecialChars)
        {
            $possibleChars = array_merge($possibleChars, $specialChars);
            $requirements['special_characters'] = $specialChars;
        }
        $acceptableToken = false;
        while (!$acceptableToken)
        {
            $outstandingRequirements = $requirements; 
            $token = '';
            $acceptableToken = true;
            $maxPossibleCharIndex = count($possibleChars) - 1;
            for ($s=0; $s<$numChars; $s++)
            {
                $token .= $possibleChars[rand(0, $maxPossibleCharIndex)];
            }
            $stringArray = str_split($token);
            foreach ($stringArray as $character)
            {
                if (count($outstandingRequirements) > 0) 
                {
                    foreach ($outstandingRequirements as $name => $arrayOfChars)
                    {
                        if (array_search($character, $arrayOfChars) !== FALSE)
                        {
                            unset($outstandingRequirements[$name]);
                            break;
                        }
                    }           
                }
                else
                {
                    break;
                }
            }
            if (count($outstandingRequirements) != 0)
            {
                $acceptableToken = false;
            }
        }
        return $token;
    }
public static function generateRandomString ( $ numChars , $ charOptions = 0 ) { $ userLowerCase = ! ( $ charOptions & self :: PASSWORD_DISABLE_LOWER_CASE ) ; $ useUppercase = ! ( $ charOptions & self :: PASSWORD_DISABLE_UPPER_CASE ) ; $ useNumbers = ! ( $ charOptions & self :: PASSWORD_DISABLE_NUMBERS ) ; $ useSpecialChars = ! ( $ charOptions & self :: PASSWORD_DISABLE_SPECIAL_CHARS ) ; $ lowerCase = str_split ( 'abcdefghijklmnopqrstuvwxyz' , 1 ) ; $ numbers = str_split ( '0123456789' , 1 ) ; $ capitalLetters = str_split ( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' , 1 ) ; $ specialChars = str_split ( '!@#$%^&*(){}[]+-/_' , 1 ) ; $ possibleChars = array ( ) ; if ( $ userLowerCase ) { $ possibleChars = array_merge ( $ possibleChars , $ lowerCase ) ; $ requirements [ 'lower_case' ] = $ lowerCase ; } if ( $ useUppercase ) { $ possibleChars = array_merge ( $ possibleChars , $ capitalLetters ) ; $ requirements [ 'capitals' ] = $ capitalLetters ; } if ( $ useNumbers ) { $ possibleChars = array_merge ( $ possibleChars , $ numbers ) ; $ requirements [ 'numbers' ] = $ numbers ; } if ( $ useSpecialChars ) { $ possibleChars = array_merge ( $ possibleChars , $ specialChars ) ; $ requirements [ 'special_characters' ] = $ specialChars ; } $ acceptableToken = false ; while ( ! $ acceptableToken ) { $ outstandingRequirements = $ requirements ; $ token = '' ; $ acceptableToken = true ; $ maxPossibleCharIndex = count ( $ possibleChars ) - 1 ; for ( $ s = 0 ; $ s < $ numChars ; $ s ++ ) { $ token .= $ possibleChars [ rand ( 0 , $ maxPossibleCharIndex ) ] ; } $ stringArray = str_split ( $ token ) ; foreach ( $ stringArray as $ character ) { if ( count ( $ outstandingRequirements ) > 0 ) { foreach ( $ outstandingRequirements as $ name => $ arrayOfChars ) { if ( array_search ( $ character , $ arrayOfChars ) !== FALSE ) { unset ( $ outstandingRequirements [ $ name ] ) ; break ; } } } else { break ; } } if ( count ( $ outstandingRequirements ) != 0 ) { $ acceptableToken = false ; } } return $ token ; }
Generates a random string.
Generates a random string .
#copy the array # must recalc each time # Stop parsing the token as soon as all required chars found