Short URLs have been popular for a while. I had contact with them when I was working on Sina Weibo application, but I didn’t figure it out. I have been exposed to it again recently. When I came to this stuff, I studied it carefully and found that shortening the URL is actually quite easy. Next, we will record the implementation method of using php to generate short URLs.
php generate short domain name function
?
3 4 513 14
15
16
17
18
19
20
21
22
|
public function createRandCode($string) { $code = ''; $hex_code = '1qaz2wsx3edc4rfv5t-gb6yhn7ujm8ik9ol0p_'; $now = microtime(true) * 10000; $strlen = strlen($hex_code); $hash_code = hash('sha256', $string); // This will define a random length for the encoding, the length depends on 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; } |