Losowy ciąg znaków

<?php
class RandomCodeGenerator {

    /**
     * rand code(integer or alphanumeric) by lenght
     * 
     * @param int $length
     * @return string|integer
     */
    public static function generate($length = 4, $alphanumeric = false) {

        if ($alphanumeric) {
            $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        } else {
            $characters = '0123456789';
        }

        $characterLength = strlen($characters);
        $randomcode = '';
        for ($i = 0; $i < $length; $i++) {
            $randomcode .= $characters[rand(0, $characterLength - 1)];
        }

        return $randomcode;
    }

}
Komentarze wyłączone