JBZoo\Utils\Str::random PHP Method

random() public static method

Generate ridable random string
public static random ( integer $length = 10, boolean $isReadable = true ) : string
$length integer
$isReadable boolean
return string
    public static function random($length = 10, $isReadable = true)
    {
        $result = '';
        if ($isReadable) {
            $vocal = array('a', 'e', 'i', 'o', 'u', '0');
            $conso = array('b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9');
            $max = $length / 2;
            for ($pos = 1; $pos <= $max; $pos++) {
                $result .= $conso[mt_rand(0, count($conso) - 1)];
                $result .= $vocal[mt_rand(0, count($vocal) - 1)];
            }
        } else {
            $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            for ($pos = 0; $pos < $length; $pos++) {
                $result .= $chars[mt_rand() % strlen($chars)];
            }
        }
        return $result;
    }

Usage Example

Example #1
0
 /**
  * @param $file
  * @return array
  */
 public function uploadScreenShot3($file)
 {
     $name = Str::random() . '.' . strtolower($file->ext);
     $image = new Image($file->image);
     $image->saveAs(PATH_PUBLIC . '/images/' . $name, 100);
     return array('UploadScreenShot3Result' => $image->getUrl());
 }
All Usage Examples Of JBZoo\Utils\Str::random