Home > Backend Development > PHP Tutorial > PHP generate short domain name function_PHP tutorial

PHP generate short domain name function_PHP tutorial

WBOY
Release: 2016-07-13 10:00:29
Original
1066 people have browsed it

PHP function to generate short domain names

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

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);

 

// 这里会为编码定义一个随机的长度,长度取决于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;

}

1 2

3

4 5

67 8 9 10 11 12
13
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; }
The above is the entire content of the code shared with you in this article. I hope you will like it. http://www.bkjia.com/PHPjc/973140.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/973140.htmlTechArticlephp generate short domain name function Short URL has been popular for some time, when I was doing Sina Weibo application I have been in contact with it, but I haven’t figured it out. Recently I came into contact with this stuff again and carefully...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template