
這篇文章主要介紹“php如何生成短域名函數(shù)”,在日常操作中,相信很多人在php如何生成短域名函數(shù)問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”php如何生成短域名函數(shù)”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

php生成短域名函數(shù)
public function createRandCode($string) {
$code = '';
$hex_code = '1qaz2wsx3edc4rfv5t-gb6yhn7ujm8ik9ol0p_';
$now = microtime(true) * 10000;
$strlen = strlen($hex_code);
$hash_code = hash('sha256', $string);
// 這里會(huì)為編碼定義一個(gè)隨機(jī)的長(zhǎng)度,長(zhǎng)度取決于step
$step = rand(8, 16);
$count = ceil(strlen($hash_code) / $step);
for($i = 0; $i < $count; $i++) {
$start = $i * $step;
$hex_num = substr($hash_code, $start, $step);
$num = 0x3fffffff & (1 * '0x' . $hex_num);
$n = $num % $strlen;
$code .= $hex_code[$n];
}
return $code;
}到此,關(guān)于“php如何生成短域名函數(shù)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!