-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_string.php
More file actions
55 lines (42 loc) · 1.04 KB
/
random_string.php
File metadata and controls
55 lines (42 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
function random_string_test() {
$time_start = time();
for ($i = 0; $i < 1000000; $i++) {
random_string_1(10);
}
$time_end = time();
echo 'Time cost: ' . ($time_end - $time_start) . '.';
}
/**
* Generate a random string used chars provided by source.
*
* @param string $src
* Source characters.
* @param int $len
* Length of the return random string.
* @return string
*/
function random_string($src, $len) {
$ret = '';
$rand_max = strlen($src);
for ($i = 0; $i < $len; $i++) {
$ret .= $src[mt_rand(0, $rand_max)];
}
return $ret;
}
function random_string_1($len = 10) {
$source = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$result = '';
for ($i = 0; $i < $len; $i++) {
$result .= $source[mt_rand(0, strlen($source) - 1)];
}
return $result;
}
function random_string_2($len = 10) {
$source = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'));
$result = '';
for ($i = 0; $i < $len; $i++) {
$result .= $source[array_rand($source)];
}
return $result;
}