PHP杂项 业 ,精于勤 荒于嬉.
- PHP杂项 ip白黑名单验证
-
发表日期:2022-08-06 16:12:05 | 来源: | 分类:PHP杂项
-
示例1
01
function
validate_ip(
$ip
,
$allow_ip_map
)
02
{
03
if
(in_array(
$ip
,
$allow_ip_map
)) {
04
return
true;
05
}
else
{
06
$flag
= false;
07
$ip_pos
=
explode
(
'.'
,
$ip
);
08
foreach
(
$allow_ip_map
as
$val
) {
09
if
(
strpos
(
$val
,
'-'
) !== false) {
//范围
10
$ip_range
=
explode
(
'-'
,
$val
);
11
if
(
ip2long
(
$ip_range
[0]) * -1 >=
ip2long
(
$ip
) * -1 &&
ip2long
(
$ip_range
[1]) * -1 <=
ip2long
(
$ip
) * -1) {
12
$flag
= true;
13
break
;
14
}
15
}
else
{
//单个
16
if
(
strpos
(
$val
,
'*'
) !== false) {
//发现有*号替代符
17
$arr
=
explode
(
'.'
,
$val
);
18
$flag
= true;
//用于记录循环检测中是否有匹配成功的
19
for
(
$i
= 0;
$i
< 4;
$i
++) {
20
if
(
$arr
[
$i
] !=
'*'
) {
//不等于* 就要进来检测,如果为*符号替代符就不检查
21
if
(
$arr
[
$i
] !=
$ip_pos
[
$i
]) {
22
$flag
= false;
23
break
;
//终止检查本个ip 继续检查下一个ip
24
}
25
}
26
}
27
if
(
$flag
) {
//如果是true则终止匹配
28
break
;
29
}
30
}
31
}
32
}
33
return
$flag
;
34
}
35
}
- PHP杂项 自实现getallheaders函数 获取header信息
-
发表日期:2022-08-06 16:10:41 | 来源: | 分类:PHP杂项
-
示例1
01
if
(!function_exists(
'getallheaders'
)) {
02
/**
03
* @return mixed
04
* @deprecated 推荐使用request()->header()
05
*/
06
function
getallheaders
()
07
{
08
foreach
(
$_SERVER
as
$name
=>
$value
) {
09
if
(
substr
(
$name
, 0, 5) ==
'HTTP_'
) {
10
$headers
[
str_replace
(
' '
,
'-'
, ucwords(
strtolower
(
str_replace
(
'_'
,
' '
,
substr
(
$name
, 5)))))] =
$value
;
11
}
12
}
13
return
$headers
;
14
}
15
}
- PHP杂项 随机颜色
-
发表日期:2022-08-06 16:09:53 | 来源: | 分类:PHP杂项
-
示例1
1
function
random_color()
2
{
3
$rand
=
array
(
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
'8'
,
'9'
,
'a'
,
'b'
,
'c'
,
'd'
,
'e'
,
'f'
);
4
$color
=
'#'
.
$rand
[rand(0, 15)] .
$rand
[rand(0, 15)] .
$rand
[rand(0, 15)] .
$rand
[rand(0, 15)] .
$rand
[rand(0, 15)] .
$rand
[rand(0, 15)];
5
return
$color
;
6
}
- PHP杂项 中文全角半角互转
-
发表日期:2022-08-06 16:06:03 | 来源: | 分类:PHP杂项
-
示例1
01
/**
02
* 半角全角互转
03
* @param string $str
04
* @param string $args2 0半角到全角 1全角到半角
05
* @return string
06
**/
07
function
SBC_DBC(
$str
,
$args2
=0)
08
{
09
$DBC
=
array
(
10
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
11
'5'
,
'6'
,
'7'
,
'8'
,
'9'
,
12
'A'
,
'B'
,
'C'
,
'D'
,
'E'
,
13
'F'
,
'G'
,
'H'
,
'I'
,
'J'
,
14
'K'
,
'L'
,
'M'
,
'N'
,
'O'
,
15
'P'
,
'Q'
,
'R'
,
'S'
,
'T'
,
16
'U'
,
'V'
,
'W'
,
'X'
,
'Y'
,
17
'Z'
,
'a'
,
'b'
,
'c'
,
'd'
,
18
'e'
,
'f'
,
'g'
,
'h'
,
'i'
,
19
'j'
,
'k'
,
'l'
,
'm'
,
'n'
,
20
'o'
,
'p'
,
'q'
,
'r'
,
's'
,
21
't'
,
'u'
,
'v'
,
'w'
,
'x'
,
22
'y'
,
'z'
,
'-'
,
' '
,
':'
,
23
'.'
,
','
,
'/'
,
'%'
,
'#'
,
24
'!'
,
'@'
,
'&'
,
'('
,
')'
,
25
'<'
,
'>'
,
'"'
,
'''
,
'?'
,
26
'['
,
']'
,
'{'
,
'}'
,
'\'
,
27
'|'
,
'+'
,
'='
,
'_'
,
'^'
,
28
'¥'
,
' ̄'
,
'`'
29
);
30
$SBC
=
array
(
//半角
31
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
32
'5'
,
'6'
,
'7'
,
'8'
,
'9'
,
33
'A'
,
'B'
,
'C'
,
'D'
,
'E'
,
34
'F'
,
'G'
,
'H'
,
'I'
,
'J'
,
35
'K'
,
'L'
,
'M'
,
'N'
,
'O'
,
36
'P'
,
'Q'
,
'R'
,
'S'
,
'T'
,
37
'U'
,
'V'
,
'W'
,
'X'
,
'Y'
,
38
'Z'
,
'a'
,
'b'
,
'c'
,
'd'
,
39
'e'
,
'f'
,
'g'
,
'h'
,
'i'
,
40
'j'
,
'k'
,
'l'
,
'm'
,
'n'
,
41
'o'
,
'p'
,
'q'
,
'r'
,
's'
,
42
't'
,
'u'
,
'v'
,
'w'
,
'x'
,
43
'y'
,
'z'
,
'-'
,
' '
,
':'
,
44
'.'
,
','
,
'/'
,
'%'
,
'#'
,
45
'!'
,
'@'
,
'&'
,
'('
,
')'
,
46
'<'
,
'>'
,
'"'
,
'\''
,
'?'
,
47
'['
,
']'
,
'{'
,
'}'
,
'\\'
,
48
'|'
,
'+'
,
'='
,
'_'
,
'^'
,
49
'$'
,
'~'
,
'`'
50
);
51
if
(
$args2
== 0)
52
return
str_replace
(
$SBC
,
$DBC
,
$str
);
//半角到全角
53
if
(
$args2
== 1)
54
return
str_replace
(
$DBC
,
$SBC
,
$str
);
//全角到半角
55
else
56
return
false;
57
}
- PHP杂项 字符和unicode互转
-
发表日期:2022-08-06 16:03:20 | 来源: | 分类:PHP杂项
-
示例1
01
/**
02
* 将unicode转换成字符
03
* @param int $unicode
04
* @return string UTF-8字符
05
**/
06
function
unicode2Char(
$unicode
)
07
{
08
if
(
$unicode
< 128)
return
chr
(
$unicode
);
09
if
(
$unicode
< 2048)
return
chr
((
$unicode
>> 6) + 192) .
10
chr
((
$unicode
& 63) + 128);
11
if
(
$unicode
< 65536)
return
chr
((
$unicode
>> 12) + 224) .
12
chr
(((
$unicode
>> 6) & 63) + 128) .
13
chr
((
$unicode
& 63) + 128);
14
if
(
$unicode
< 2097152)
return
chr
((
$unicode
>> 18) + 240) .
15
chr
(((
$unicode
>> 12) & 63) + 128) .
16
chr
(((
$unicode
>> 6) & 63) + 128) .
17
chr
((
$unicode
& 63) + 128);
18
return
false;
19
}
20
21
/**
22
* 将字符转换成unicode
23
* @param string $char 必须是UTF-8字符
24
* @return int
25
**/
26
function
char2Unicode(
$char
)
27
{
28
switch
(
strlen
(
$char
)) {
29
case
1 :
30
return
ord(
$char
);
31
case
2 :
32
return
(ord(
$char
{1}) & 63) |
33
((ord(
$char
{0}) & 31) << 6);
34
case
3 :
35
return
(ord(
$char
{2}) & 63) |
36
((ord(
$char
{1}) & 63) << 6) |
37
((ord(
$char
{0}) & 15) << 12);
38
case
4 :
39
return
(ord(
$char
{3}) & 63) |
40
((ord(
$char
{2}) & 63) << 6) |
41
((ord(
$char
{1}) & 63) << 12) |
42
((ord(
$char
{0}) & 7) << 18);
43
default
:
44
trigger_error(
'Character is not UTF-8!'
, E_USER_WARNING);
45
return
false;
46
}
47
}
- PHP杂项 日期时间友好展示为今天/昨天/前天/分钟/小时/天前
-
发表日期:2022-08-06 16:00:35 | 来源: | 分类:PHP杂项
-
示例1
01
function
datetime_friendly(
$datetime
)
02
{
03
$time
=
strtotime
(
$datetime
);
04
05
$second
= time() -
$time
;
06
if
(
$second
< 60) {
07
$str
=
'刚刚'
;
08
}
elseif
(
$second
< 60 * 60) {
09
$min
=
floor
(
$second
/ 60);
10
$str
=
$min
.
' 分钟前'
;
11
}
elseif
(
$second
< 60 * 60 * 24) {
12
$h
=
floor
(
$second
/ (60 * 60));
13
$str
=
$h
.
' 小时前'
;
14
}
elseif
(
$second
< 60 * 60 * 24 * 3) {
15
$d
=
floor
(
$second
/ (60 * 60 * 24));
16
17
$rtime
=
date
(
"H:i"
,
$time
);
18
19
if
(
$d
== 1)
20
$str
=
'昨天 '
.
$rtime
;
21
else
22
$str
=
'前天 '
.
$rtime
;
23
}
elseif
(
$time
>
mktime
(0, 0, 0, 1, 1,
date
(
'Y'
))) {
24
$str
=
date
(
"m月d日"
,
$time
);
25
}
else
{
26
$str
=
date
(
"Y年m月d日"
,
$time
);
27
}
28
29
return
$str
;
30
}
- PHP杂项 递归创建多级目录
-
发表日期:2022-08-06 15:58:05 | 来源: | 分类:PHP杂项
-
示例1
01
/**
02
* 检测目录是否存在/创建多级目录
03
* @param String $path 目录路径[D:/a/b] or [a/b/c] or [./a/b] or [/a/b/c] or [../../a/b]
04
* @return boolean 目录是否存在/多级目录是否创建成功
05
*/
06
function
quick_make_dirs(
$path
)
07
{
08
if
(
is_dir
(
$path
))
return
true;
09
10
$path
=
str_replace
(ROOT_PATH,
''
, preg_replace(
'/[\\\|\/]+/'
, DS,
$path
));
11
12
$dirs
=
explode
(DS,
$path
);
13
14
$dir
= ROOT_PATH;
15
16
for
(
$i
= 0;
$i
<
count
(
$dirs
);
$i
++) {
17
18
if
(
$dirs
[
$i
] ==
''
)
continue
;
19
20
$dir
.=
$dirs
[
$i
] . DS;
21
22
if
(!
is_dir
(
$dir
)) {
23
//mkdir($dir, 0777) or TRIGGER_ERROR("!-目录[$dir]不可写,请手动设置目录权限!");
24
if
(!
mkdir
(
$dir
, 0777)) {
25
break
;
26
}
27
28
}
29
}
30
31
return
is_dir
(
$dir
) ? true : false;
32
}
- PHP杂项 字符串截取追加...
-
发表日期:2022-08-06 15:51:11 | 来源: | 分类:PHP杂项
-
示例1
01
/**
02
* 字符串截取
03
* @param String $str 要截取的字符串
04
* @param int $start 开始位置
05
* @param int $length 截取长度
06
* @param String $ellipsis 省略符
07
* @return String 截取后的字符串
08
*/
09
function
quick_mb_substr(
$str
,
$start
,
$length
,
$ellipsis
=
'...'
)
10
{
11
$str_length
= mb_strlen(
$str
,
'utf8'
);
12
13
if
(
$str_length
<=
$start
+
$length
) {
14
return
mb_substr(
$str
,
$start
,
$str_length
-
$start
,
'utf8'
);
15
}
16
17
return
mb_substr(
$str
,
$start
,
$length
,
'utf8'
) .
$ellipsis
;
18
}
- PHP杂项 批量反/转义特殊字符,数据库安全过滤写入,支持字符串、多维数组、对象集合
-
发表日期:2022-08-06 15:46:30 | 来源: | 分类:PHP杂项
-
示例1
01
/**
02
* 批量转义特殊字符
03
* '&' (ampersand) becomes '&'
04
* '"' (double quote) becomes '"' when ENT_NOQUOTES is not set.
05
* "'" (single quote) becomes ''' (or ') only when ENT_QUOTES is set.
06
* '<' (less than) becomes '<'
07
* '>' (greater than) becomes '>'
08
*
09
* @param array|object|string $data
10
* @return array|object|string
11
* @example
12
* $new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
13
* echo $new; // <a href='test'>Test</a>
14
*
15
*/
16
function
encode_special_symbol(
$data
)
17
{
18
if
(
is_array
(
$data
)) {
19
foreach
(
$data
as
$key
=>
$value
)
$data
[
$key
] = encode_special_symbol(
$value
);
20
}
elseif
(
is_object
(
$data
)) {
21
foreach
(
$data
as
$key
=>
$value
)
$data
->
$key
= encode_special_symbol(
$value
);
22
}
else
{
23
$data
= htmlspecialchars(trim(
$data
));
24
}
25
return
$data
;
26
}
27
28
/**
29
* 批量反转义特殊字符
30
* @param $data
31
* @return array|string
32
*/
33
function
decode_special_symbol(
$data
)
34
{
35
if
(
is_array
(
$data
)) {
36
foreach
(
$data
as
$key
=>
$value
)
$data
[
$key
] = decode_special_symbol(
$value
);
37
}
elseif
(
is_object
(
$data
)) {
38
foreach
(
$data
as
$key
=>
$value
)
$data
->
$key
= decode_special_symbol(
$value
);
39
}
else
{
40
$data
= htmlspecialchars_decode(
$data
);
41
}
42
return
$data
;
43
}
- PHP杂项 压缩HTML,去换行空格,注释
-
发表日期:2022-08-06 15:43:48 | 来源: | 分类:PHP杂项
-
示例1
1
function
quick_compress_html(
$html
)
2
{
3
$html
=
str_replace
([
"\r\n"
,
"\n"
,
"\t"
], [
''
,
''
,
''
],
$html
);
4
$_pattern
=
array
(
"/> *([^ ]*) *</"
,
"/[\s]+/"
,
"/<!--[^!]*-->/"
,
"/\" /"
,
"/ \"/"
,
"'/\*[^*]*\*/'"
,
"/ \/\*[^.]*\*\//"
);
5
$_replace
=
array
(
">\\1<"
,
" "
,
""
,
"\""
,
"\""
,
" "
,
" "
);
6
return
preg_replace(
$_pattern
,
$_replace
,
$html
);
7
}
- PHP杂项 获取文章html中的img标签图片src地址
-
发表日期:2022-08-06 15:42:56 | 来源: | 分类:PHP杂项
-
示例1
01
/**
02
* 获取文章中的图片
03
* @param $content
04
* @return mixed
05
*/
06
function
html_get_img(
$content
)
07
{
08
// 强力模式
09
// preg_match_all('#<[img|IMG].*?[src|SRC]=[$"|$\'|$\s]*([^"|^\'|^\s]*)[^>]*>#i', $content, $match);
10
preg_match_all(
'#<[img].*?[src]="([^"]*)[^>]*>#i'
,
$content
,
$match
);
11
return
$match
[1];
12
}
- PHP杂项 html过滤标签空格提取纯文本text
-
发表日期:2022-08-06 15:41:37 | 来源: | 分类:PHP杂项
-
示例1
01
/**
02
* html 转换成text 过滤html标签空格
03
* @param $html
04
* @param null|string $html_tag 要保留的标签
05
* @return string
06
*/
07
function
html2text(
$html
,
$html_tag
= null)
08
{
09
return
trim(preg_replace(
"/[ ]+/"
,
" "
,
str_replace
([
" "
,
" "
],
" "
,
strip_tags
(
$html
,
$html_tag
))));
10
}
- PHP杂项 自实现http_build_url 函数
-
发表日期:2022-08-06 15:39:34 | 来源: | 分类:PHP杂项
-
示例1
01
if
(!function_exists(
'http_build_url'
)) {
02
/**
03
* 构建URL
04
* @param string $url
05
* @param array $params
06
* @return string
07
*/
08
function
http_build_url(
$url
,
$params
)
09
{
10
if
(!
strstr
(
$url
,
'?'
))
$url
.=
"?"
;
11
12
if
((
$last
=
strrchr
(
$url
,
'&'
)) &&
$last
!==
'&'
)
$url
.=
"&"
;
13
14
return
$url
. http_build_query(
$params
);
15
}
16
}
- PHP杂项 获取浏览器类型版本及操作系统类型
-
发表日期:2022-08-06 15:31:18 | 来源: | 分类:PHP杂项
-
示例1
001
class
Agent
002
{
003
004
/**
005
* 获取客户端浏览器信息 添加win10 edge浏览器判断
006
* @param null
007
* @return string
008
*/
009
public
static
function
getBroswer()
010
{
011
if
(isset(
$_SERVER
[
'HTTP_USER_AGENT'
]))
012
{
013
$sys
=
$_SERVER
[
'HTTP_USER_AGENT'
];
//获取用户代理字符串
014
}
else
{
015
$sys
=
''
;
//获取用户代理字符串
016
}
017
018
if
(
stripos
(
$sys
,
"Firefox/"
) > 0) {
019
preg_match(
"/Firefox\/([^;)]+)+/i"
,
$sys
,
$b
);
020
$exp
[0] =
"Firefox"
;
021
$exp
[1] =
$b
[1];
//获取火狐浏览器的版本号
022
}
elseif
(
stripos
(
$sys
,
"Maxthon"
) > 0) {
023
preg_match(
"/Maxthon\/([\d\.]+)/"
,
$sys
,
$aoyou
);
024
$exp
[0] =
"傲游"
;
025
$exp
[1] =
$aoyou
[1];
026
}
elseif
(
stripos
(
$sys
,
"MSIE"
) > 0) {
027
preg_match(
"/MSIE\s+([^;)]+)+/i"
,
$sys
,
$ie
);
028
$exp
[0] =
"IE"
;
029
$exp
[1] =
$ie
[1];
//获取IE的版本号
030
}
elseif
(
stripos
(
$sys
,
"OPR"
) > 0) {
031
preg_match(
"/OPR\/([\d\.]+)/"
,
$sys
,
$opera
);
032
$exp
[0] =
"Opera"
;
033
$exp
[1] =
$opera
[1];
034
}
elseif
(
stripos
(
$sys
,
"Edge"
) > 0) {
035
//win10 Edge浏览器 添加了chrome内核标记 在判断Chrome之前匹配
036
preg_match(
"/Edge\/([\d\.]+)/"
,
$sys
,
$Edge
);
037
$exp
[0] =
"Edge"
;
038
$exp
[1] =
$Edge
[1];
039
}
elseif
(
stripos
(
$sys
,
"Chrome"
) > 0) {
040
preg_match(
"/Chrome\/([\d\.]+)/"
,
$sys
,
$google
);
041
$exp
[0] =
"Chrome"
;
042
$exp
[1] =
$google
[1];
//获取google chrome的版本号
043
}
elseif
(
stripos
(
$sys
,
'rv:'
) > 0 &&
stripos
(
$sys
,
'Gecko'
) > 0) {
044
preg_match(
"/rv:([\d\.]+)/"
,
$sys
,
$IE
);
045
$exp
[0] =
"IE"
;
046
$exp
[1] =
$IE
[1];
047
}
elseif
(
stripos
(
$sys
,
'Safari'
) > 0) {
048
preg_match(
"/safari\/([^\s]+)/i"
,
$sys
,
$safari
);
049
$exp
[0] =
"Safari"
;
050
$exp
[1] =
$safari
[1];
051
}
elseif
(
stripos
(
$sys
,
'ApiPOST'
) !== false) {
052
preg_match(
"/apipost\/([^\s]+)/i"
,
$sys
,
$ApiPOST
);
053
$exp
[0] =
"ApiPOST"
;
054
$exp
[1] =
"Runtime"
;
055
}
elseif
(
stripos
(
$sys
,
'PostmanRuntime'
) !== false) {
056
preg_match(
"/PostmanRuntime\/([^\s]+)/i"
,
$sys
,
$Postman
);
057
$exp
[0] =
"Postman"
;
058
$exp
[1] =
$Postman
[1];
059
}
elseif
(
stripos
(
$sys
,
'curl'
) !== false) {
060
preg_match(
"/curl\/([^\s]+)/i"
,
$sys
,
$curl
);
061
$exp
[0] =
"curl"
;
062
$exp
[1] =
$curl
[1];
063
}
else
{
064
$exp
[0] =
"未知"
;
065
$exp
[1] =
""
;
066
}
067
return
$exp
[0] .
'('
.
$exp
[1] .
')'
;
068
}
069
070
/**
071
* 获取客户端操作系统信息包括win10
072
* @param null
073
* @return string
074
*/
075
public
static
function
getOs()
076
{
077
if
(isset(
$_SERVER
[
'HTTP_USER_AGENT'
]))
078
{
079
$agent
=
$_SERVER
[
'HTTP_USER_AGENT'
];
//获取用户代理字符串
080
}
else
{
081
$agent
=
''
;
//获取用户代理字符串
082
}
083
084
if
(preg_match(
'/Windows Phone/i'
,
$agent
)) {
085
$os
=
'Windows Phone'
;
086
// } else if (preg_match('/win/i', $agent) && strpos($agent, '95')) {
087
// $os = 'Windows 95';
088
// } else if (preg_match('/win 9x/i', $agent) && strpos($agent, '4.90')) {
089
// $os = 'Windows ME';
090
// } else if (preg_match('/win/i', $agent) && preg_match('/98/i', $agent)) {
091
// $os = 'Windows 98';
092
}
else
if
(preg_match(
'/win/i'
,
$agent
) && preg_match(
'/nt 6.0/i'
,
$agent
)) {
093
$os
=
'Windows Vista'
;
094
}
else
if
(preg_match(
'/win/i'
,
$agent
) && preg_match(
'/nt 6.1/i'
,
$agent
)) {
095
$os
=
'Windows 7'
;
096
}
else
if
(preg_match(
'/win/i'
,
$agent
) && preg_match(
'/nt 6.2/i'
,
$agent
)) {
097
$os
=
'Windows 8'
;
098
}
else
if
(preg_match(
'/win/i'
,
$agent
) && preg_match(
'/nt 10.0/i'
,
$agent
)) {
099
$os
=
'Windows 10'
;#添加win10判断
100
}
else
if
(preg_match(
'/win/i'
,
$agent
) && preg_match(
'/nt 5.1/i'
,
$agent
)) {
101
$os
=
'Windows XP'
;
102
}
else
if
(preg_match(
'/win/i'
,
$agent
) && preg_match(
'/nt 5/i'
,
$agent
)) {
103
$os
=
'Windows 2000'
;
104
}
else
if
(preg_match(
'/win/i'
,
$agent
) && preg_match(
'/nt/i'
,
$agent
)) {
105
$os
=
'Windows NT'
;
106
}
else
if
(preg_match(
'/win/i'
,
$agent
) && preg_match(
'/32/i'
,
$agent
)) {
107
$os
=
'Windows 32'
;
108
}
else
if
(preg_match(
'/linux/i'
,
$agent
)) {
109
$os
=
'Linux'
;
110
}
else
if
(preg_match(
'/unix/i'
,
$agent
)) {
111
$os
=
'Unix'
;
112
}
else
if
(preg_match(
'/sun/i'
,
$agent
) && preg_match(
'/os/i'
,
$agent
)) {
113
$os
=
'SunOS'
;
114
}
else
if
(preg_match(
'/ibm/i'
,
$agent
) && preg_match(
'/os/i'
,
$agent
)) {
115
$os
=
'IBM OS/2'
;
116
}
else
if
(preg_match(
'/Mac/i'
,
$agent
)) {
117
$os
=
'Mac'
;
118
}
else
if
(preg_match(
'/PowerPC/i'
,
$agent
)) {
119
$os
=
'PowerPC'
;
120
}
else
if
(preg_match(
'/AIX/i'
,
$agent
)) {
121
$os
=
'AIX'
;
122
}
else
if
(preg_match(
'/HPUX/i'
,
$agent
)) {
123
$os
=
'HPUX'
;
124
}
else
if
(preg_match(
'/NetBSD/i'
,
$agent
)) {
125
$os
=
'NetBSD'
;
126
}
else
if
(preg_match(
'/BSD/i'
,
$agent
)) {
127
$os
=
'BSD'
;
128
}
else
if
(preg_match(
'/OSF1/i'
,
$agent
)) {
129
$os
=
'OSF1'
;
130
}
else
if
(preg_match(
'/IRIX/i'
,
$agent
)) {
131
$os
=
'IRIX'
;
132
}
else
if
(preg_match(
'/FreeBSD/i'
,
$agent
)) {
133
$os
=
'FreeBSD'
;
134
}
else
if
(preg_match(
'/teleport/i'
,
$agent
)) {
135
$os
=
'teleport'
;
136
}
else
if
(preg_match(
'/flashget/i'
,
$agent
)) {
137
$os
=
'flashget'
;
138
}
else
if
(preg_match(
'/webzip/i'
,
$agent
)) {
139
$os
=
'webzip'
;
140
}
else
if
(preg_match(
'/offline/i'
,
$agent
)) {
141
$os
=
'offline'
;
142
}
elseif
(preg_match(
'/ucweb|MQQBrowser|J2ME|IUC|3GW100|LG-MMS|i60|Motorola|MAUI|m9|ME860|maui|C8500|gt|k-touch|X8|htc|GT-S5660|UNTRUSTED|SCH|tianyu|lenovo|SAMSUNG/i'
,
$agent
)) {
143
$os
=
'mobile'
;
144
}
else
{
145
$os
=
'未知'
;
146
}
147
return
$os
;
148
}
149
}
- PHP杂项 curl请求及注意事项
-
发表日期:2022-08-06 15:26:25 | 来源: | 分类:PHP杂项
-
示例1
01
function
post(
$data
,
$url
)
02
{
03
04
$ch
= curl_init();
05
curl_setopt(
$ch
, CURLOPT_URL,
$url
);
06
curl_setopt(
$ch
, CURLOPT_CONNECTTIMEOUT, 20);
//连接超时时间
07
curl_setopt(
$ch
, CURLOPT_TIMEOUT, 30);
//请求超时时间
08
curl_setopt(
$ch
, CURLOPT_RETURNTRANSFER, true);
09
curl_setopt(
$ch
, CURLOPT_SSL_VERIFYPEER, false);
10
curl_setopt(
$ch
, CURLOPT_SSL_VERIFYHOST, false);
11
curl_setopt(
$ch
, CURLOPT_POST, true);
//POST请求
12
curl_setopt(
$ch
, CURLOPT_POSTFIELDS, http_build_query(
$data
));
13
14
//设置header头信息
15
curl_setopt(
$ch
, CURLOPT_HTTPHEADER, [
16
'Content-Type: application/json; charset=utf-8'
,
//json请求
17
'token:vXBrUZ7rWE5D4P4ENnwR6GFvaG2pVgul'
//比如登录信息/token信息/session信息
18
]);
19
20
$output
= curl_exec(
$ch
);
21
22
if
(
$error_code
= curl_errno(
$ch
)) {
//已经检查是否报错做进一步处理
23
$err
=
$error_code
.
' : '
. curl_error(
$ch
);
24
curl_close(
$ch
);
25
throw
new
Exception(
$err
);
26
}
27
28
curl_close(
$ch
);
29
return
$output
;
30
}
- PHP杂项 二维数组排序
-
发表日期:2022-08-06 15:15:16 | 来源: | 分类:PHP杂项
-
示例1
01
/**
02
* 升序降序排序
03
* @param $arr
04
* @param $keys 要排序的字段
05
* @param string $type asc desc
06
* @return array
07
*/
08
function
array_sort(
$arr
,
$keys
,
$type
=
'asc'
)
09
{
10
$keysvalue
=
$new_array
=
array
();
11
foreach
(
$arr
as
$k
=>
$v
) {
12
$keysvalue
[
$k
] =
$v
[
$keys
];
13
}
14
if
(
$type
==
'asc'
) {
15
asort(
$keysvalue
);
16
}
else
{
17
arsort(
$keysvalue
);
18
}
19
reset(
$keysvalue
);
20
foreach
(
$keysvalue
as
$k
=>
$v
) {
21
$new_array
[
$k
] =
$arr
[
$k
];
22
}
23
return
$new_array
;
24
}
- PHP杂项 文件字节大小换算KB、MB、GB、TB
-
发表日期:2022-08-06 15:11:53 | 来源: | 分类:PHP杂项
-
示例1
01
/**
02
* 字节换算
03
* @param int $size 字节长度
04
* @param int $digits 要保留几位小数
05
* @return string 转换后的文件大小
06
*/
07
function
byteToSize(
$size
,
$digits
= 2)
08
{
09
if
(
$size
== 0)
return
'0B'
;
10
$unit
=
array
(
''
,
'K'
,
'M'
,
'G'
,
'T'
,
'P'
);
11
// 单位数组,是必须1024进制依次的哦。
12
$base
= 1024;
13
// 对数的基数
14
$i
=
floor
(log(
$size
,
$base
));
15
// 字节数对1024取对数,值向下取整。 return
16
return
round
(
$size
/ pow(
$base
,
$i
),
$digits
) .
' '
.
$unit
[
$i
] .
'B'
;
17
}
- PHP杂项 数字金额转汉字大写
-
发表日期:2022-08-06 15:10:20 | 来源: | 分类:PHP杂项
-
示例1
01
function
amountToCn(
$num
)
02
{
03
$Decimal
=
$num
- (int)
$num
;
04
05
$c1
=
"零壹贰叁肆伍陆柒捌玖"
;
06
$c2
=
"分角元拾佰仟万拾佰仟亿"
;
07
//精确到分后面就不要了,所以只留两个小数位
08
$num
=
round
(
$num
, 2);
09
//将数字转化为整数
10
$num
=
$num
* 100;
11
if
(
strlen
(
$num
) > 10) {
12
return
"金额太大,请检查"
;
13
}
14
$i
= 0;
15
$c
=
""
;
16
while
(1) {
17
if
(
$i
== 0) {
18
//获取最后一位数字
19
$n
=
substr
(
$num
,
strlen
(
$num
) - 1, 1);
20
}
else
{
21
$n
=
$num
% 10;
22
}
23
//每次将最后一位数字转化为中文
24
$p1
=
substr
(
$c1
, 3 *
$n
, 3);
25
$p2
=
substr
(
$c2
, 3 *
$i
, 3);
26
if
(
$n
!=
'0'
|| (
$n
==
'0'
&& (
$p2
==
'亿'
||
$p2
==
'万'
||
$p2
==
'元'
))) {
27
$c
=
$p1
.
$p2
.
$c
;
28
}
else
{
29
$c
=
$p1
.
$c
;
30
}
31
$i
=
$i
+ 1;
32
//去掉数字最后一位了
33
$num
=
$num
/ 10;
34
$num
= (int)
$num
;
35
//结束循环
36
if
(
$num
== 0) {
37
break
;
38
}
39
}
40
$j
= 0;
41
$slen
=
strlen
(
$c
);
42
while
(
$j
<
$slen
) {
43
//utf8一个汉字相当3个字符
44
$m
=
substr
(
$c
,
$j
, 6);
45
//处理数字中很多0的情况,每次循环去掉一个汉字“零”
46
if
(
$m
==
'零元'
||
$m
==
'零万'
||
$m
==
'零亿'
||
$m
==
'零零'
) {
47
$left
=
substr
(
$c
, 0,
$j
);
48
$right
=
substr
(
$c
,
$j
+ 3);
49
$c
=
$left
.
$right
;
50
$j
=
$j
- 3;
51
$slen
=
$slen
- 3;
52
}
53
$j
=
$j
+ 3;
54
}
55
//这个是为了去掉类似23.0中最后一个“零”字
56
if
(
substr
(
$c
,
strlen
(
$c
) - 3, 3) ==
'零'
) {
57
$c
=
substr
(
$c
, 0,
strlen
(
$c
) - 3);
58
}
59
//将处理的汉字加上“整”
60
if
(
empty
(
$c
)) {
61
return
"零元整"
;
62
}
else
{
63
if
(
$Decimal
> 0){
64
return
$c
;
65
}
66
67
return
$c
.
"整"
;
68
}
69
}
- PHP杂项 验证身份证号是否有效
-
发表日期:2022-08-06 15:05:56 | 来源: | 分类:PHP杂项
-
示例1
01
public
function
isIdCard(
$id
)
02
{
03
$id
=
strtoupper
(
$id
);
04
$regx
=
"/(^\d{15}$)|(^\d{17}([0-9]|X)$)/"
;
05
$arr_split
=
array
();
06
if
(!preg_match(
$regx
,
$id
)) {
07
return
FALSE;
08
}
09
if
(15 ==
strlen
(
$id
))
//检查15位
10
{
11
$regx
=
"/^(\d{6})+(\d{2})+(\d{2})+(\d{2})+(\d{3})$/"
;
12
@preg_match(
$regx
,
$id
,
$arr_split
);
13
//检查生日日期是否正确
14
$dtm_birth
=
"19"
.
$arr_split
[2] .
'/'
.
$arr_split
[3] .
'/'
.
$arr_split
[4];
15
if
(!
strtotime
(
$dtm_birth
)) {
16
return
FALSE;
17
}
else
{
18
return
TRUE;
19
}
20
}
else
//检查18位
21
{
22
$regx
=
"/^(\d{6})+(\d{4})+(\d{2})+(\d{2})+(\d{3})([0-9]|X)$/"
;
23
@preg_match(
$regx
,
$id
,
$arr_split
);
24
$dtm_birth
=
$arr_split
[2] .
'/'
.
$arr_split
[3] .
'/'
.
$arr_split
[4];
25
if
(!
strtotime
(
$dtm_birth
))
//检查生日日期是否正确
26
{
27
return
FALSE;
28
}
else
{
29
//检验18位身份证的校验码是否正确。
30
//校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。
31
$arr_int
=
array
(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
32
$arr_ch
=
array
(
'1'
,
'0'
,
'X'
,
'9'
,
'8'
,
'7'
,
'6'
,
'5'
,
'4'
,
'3'
,
'2'
);
33
$sign
= 0;
34
for
(
$i
= 0;
$i
< 17;
$i
++) {
35
$b
= (int)
$id
{
$i
};
36
$w
=
$arr_int
[
$i
];
37
$sign
+=
$b
*
$w
;
38
}
39
$n
=
$sign
% 11;
40
$val_num
=
$arr_ch
[
$n
];
41
if
(
$val_num
!=
substr
(
$id
, 17, 1)) {
42
return
FALSE;
43
}
44
else
{
45
return
TRUE;
46
}
47
}
48
}
49
}
- PHP杂项 批量替换文件名
-
发表日期:2022-08-05 23:32:29 | 来源: | 分类:PHP杂项
-
示例1
01
<?php
02
// 效果:
03
// [canquick影视www.canquick.com]兄弟连DVD国语配音中英字幕无水印01集.mp4
04
// 兄弟连01集.mp4
05
06
$handle
= @opendir(
'./'
);
07
08
while
(
$file
= readdir(
$handle
)) {
09
// 将 "." 及 ".." 排除不显示
10
if
(
$file
!=
"."
&&
$file
!=
".."
) {
11
print_r(
$file
);
12
rename(
$file
,
str_replace
(
13
[
14
'[canquick影视www.canquick.com]'
,
15
'DVD国语配音中英字幕无水印'
,
16
],
17
[
18
''
,
19
''
,
20
],
21
$file
22
));
23
}
24
}
25
closedir
(
$handle
);
26
?>
- PHP杂项(34)
- PHP基础-李炎恢系列课程(20)
- 中文函数手册(0)
- 错误处理 函数(13)
- OPcache 函数(6)
- PHP 选项/信息 函数(54)
- Zip 函数(10)
- Hash 函数(15)
- OpenSSL 函数(63)
- Date/Time 函数(51)
- 目录函数(9)
- Fileinfo 函数(6)
- iconv 函数(11)
- 文件系统函数(81)
- 多字节字符串 函数(57)
- GD 和图像处理 函数(114)
- 可交换图像信息(5)
- Math 函数(50)
- 程序执行函数(11)
- PCNTL 函数(23)
- JSON 函数(4)
- SPL 函数(15)
- URL 函数(10)
- cURL 函数(32)
- 网络 函数(33)
- FTP 函数(36)
- Session 函数(23)
- PCRE 函数(11)
- PCRE 正则语法(19)
- 数组 函数(81)
- 类/对象 函数(18)
- 函数处理 函数(13)
- 变量处理 函数(37)
- SimpleXML 函数(3)
- 杂项 函数(31)
- 字符串 函数(101)