Function.class.php
5.12 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
class Util_Utils_Function {
/**
* 获知php版本
*
* @param String $version
* @return bool
*/
public static function is_php($version = '5.0.0')
{
static $_is_php;
$version = (string) $version;
if (!isset($_is_php[$version]))
{
$_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE;
}
return $_is_php[$version];
}
/**
* 压缩转换成十六进制
*
* @param String $string
* @return String
*/
public static function compress($string)
{
return bin2hex(gzdeflate($string, 9));
}
/**
* 转成二进制并解压
*
* @param String $string
* @return String
*/
public static function uncompress($string)
{
return gzinflate(pack('H' . strlen($string), $string));
}
/**
* 对URL进行转码(主要防范+)
*
* @param String $input
* @return String
*/
public static function base64_url_encode($input)
{
return strtr(base64_encode($input), '+/=', '-_.');
}
/**
* 对URL进行还原
*
* @param String $input
* @return String
*/
public static function base64_url_decode($input)
{
return base64_decode(strtr($input, '-_.', '+/='));
}
/**
* 对Str进行转码(主要防范+)
*
* @param String $input
* @return String
*/
public static function base64_str_encode($input)
{
return strtr($input, '+/=', '-_.');
}
/**
* 对Str进行还原
*
* @param String $input
* @return String
*/
public static function base64_str_decode($input)
{
return strtr($input, '-_.', '+/=');
}
/**
* 判断IP
*
* 使用字符比较
*
* @param String $str
* @return bool
*/
public static function is_ip($str)
{
if (!strcmp(long2ip(sprintf("%u", ip2long($str))), $str))
return true;
else
return false;
}
public static function insert($array, $val, $pos = null)
{
if (null == $pos)
{
$array[] = $val;
return $array;
}
else
{
$array2 = array_splice($array, $pos);
$array[] = $val;
$array = array_merge($array, $array2);
return $array;
}
}
/**
* 安全的数组合并,保持字符串和数字索引,字符串请使用array_merger以提高效率
*
* @param array $array
* @return array
*/
public static function safeMerge($array)
{
$num = func_num_args();
for ($i = 1; $i < $num; $i++)
{
$ary = func_get_arg($i);
foreach ($ary as $k => $v)
{
$array[$k] = $v;
}
}
return $array;
}
/*
* 组合数组
*/
public static function safeCombine(array $keys, array $values)
{
$keys = array_values($keys);
$values = array_values($values);
$return = array();
$counter = count($keys);
for ($i = 0; $i < $counter; $i++)
{
$return[$keys[$i]] = isset($values[$i]) ? $values[$i] : false;
}
return $return;
}
public static function sortLastModified(array $data)
{
$lastModified = 0;
foreach ($data as $url)
{
$_lastModified = Q_Utils_Function::headLastModified($url);
if ($_lastModified > $lastModified)
{
$lastModified = $_lastModified;
}
}
return $lastModified;
}
/**
* 取GB2312字符串首字母,原理是GBK汉字是按拼音顺序编码的.
* @param String $input
* @return String
**/
public static function getLetter($input)
{
$dict = array(
'a' => 0xB0C4,
'b' => 0xB2C0,
'c' => 0xB4ED,
'd' => 0xB6E9,
'e' => 0xB7A1,
'f' => 0xB8C0,
'g' => 0xB9FD,
'h' => 0xBBF6,
'j' => 0xBFA5,
'k' => 0xC0AB,
'l' => 0xC2E7,
'm' => 0xC4C2,
'n' => 0xC5B5,
'o' => 0xC5BD,
'p' => 0xC6D9,
'q' => 0xC8BA,
'r' => 0xC8F5,
's' => 0xCBF9,
't' => 0xCDD9,
'w' => 0xCEF3,
'x' => 0xD188,
'y' => 0xD4D0,
'z' => 0xD7F9
);
$str_1 = substr($input, 0, 1);
if ($str_1 >= chr(0x81) && $str_1 <= chr(0xfe))
{
$num = hexdec(bin2hex(substr($input, 0, 2)));
foreach ($dict as $k => $v)
{
if ($v >= $num)
{
break;
}
}
return $k;
}
else
{
return $str_1;
}
}
public static function urlDecode( $input )
{
return preg_replace("#\%u([0-9a-f]{4})#ie", "iconv('UCS-2', 'UTF-8//IGNORE', pack('H4', '\\1'))", $input);
}
}