3v4l.org

run code in 300+ PHP versions simultaneously
<?php // namespace AppBundle\Helper; /** * @author Bouchez Guillaume <guillaume.bouchez@infolegale.fr> */ class ShortIdGenerator { /* * Version du générateur */ const GENERATOR_VERSION = '1'; /** * Identifiant de version, prefix */ const GENERATOR_VERSION_PREFIX = 'a'; /** * @return string */ public static function generate() { $microtime = microtime(true); $numericId = intval(implode('', explode('.', $microtime))); $ret = self::numToAlpha($numericId, self::GENERATOR_VERSION_PREFIX); var_dump($numericId, $ret); return $ret; } /** * @see http://kvz.io/blog/2009/06/10/create-short-ids-with-php-like-youtube-or-tinyurl/ * @param mixed $in int, bigint input to translate * @param mixed $pad_up Number or boolean pads the result up to a specified length * @param string $pass_key Supplying a password makes it harder to calculate the original ID */ protected static function numToAlpha( $in, $prefix = '', $pad_up = false, $pass_key = null ) { $out = ''; $index = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $base = strlen($index); if ($pass_key !== null) { for ($n = 0; $n < strlen($index); $n++) { $i[] = substr($index, $n, 1); } $pass_hash = hash('sha256', $pass_key); $pass_hash = (strlen($pass_hash) < strlen($index) ? hash('sha512', $pass_key) : $pass_hash); for ($n = 0; $n < strlen($index); $n++) { $p[] = substr($pass_hash, $n, 1); } array_multisort($p, SORT_DESC, $i); $index = implode($i); } if (is_numeric($pad_up)) { $pad_up--; if ($pad_up > 0) { $in += pow($base, $pad_up); } } for ($t = ($in != 0 ? floor(log($in, $base)) : 0); $t >= 0; $t--) { $bcp = bcpow($base, $t); $a = floor($in / $bcp) % $base; $out = $out . substr($index, $a, 1); $in = $in - ($a * $bcp); } return $prefix.$out; } } ShortIdGenerator::generate();

preferences:
34.17 ms | 402 KiB | 5 Q