Skip to content

Instantly share code, notes, and snippets.

@florianajir
Last active December 19, 2017 15:53
Show Gist options
  • Select an option

  • Save florianajir/ca1f631682c9dfe91ff5 to your computer and use it in GitHub Desktop.

Select an option

Save florianajir/ca1f631682c9dfe91ff5 to your computer and use it in GitHub Desktop.
Generate random identifier
<?php
const SKU_LENGTH = 24;
const PREFIX_SEPARATOR = '-';
public function createSku($prefix)
{
$prefix .= self::PREFIX_SEPARATOR;
$randLength = self::SKU_LENGTH - count($prefix);
$sku = $prefix . $this->generateRandom($missingLength);
return $sku;
}
private function generateRandom($length)
{
if (!function_exists( 'openssl_random_pseudo_bytes' )) {
throw new Exception('missing OpenSSL extension');
}
$bytes = openssl_random_pseudo_bytes($length);
$hex = bin2hex($bytes);
return $hex;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment