Skip to content

Commit bb93eba

Browse files
committed
support more extensive task URL like domain or IPv4 or IPv6 with optional port number
1 parent 82acc93 commit bb93eba

File tree

1 file changed

+41
-21
lines changed

1 file changed

+41
-21
lines changed

src/Kernel/Library/Helper/Tool.php

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,54 @@ static public function checkUrl($url = '')
2222
{
2323
if(empty($url) || !is_string($url)) return false;
2424

25-
$result = \filter_var($url, FILTER_VALIDATE_URL);
25+
//port pattern
26+
$port_mode = "([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])";
27+
28+
//support domain with optional port number
29+
$pattern = "/^((http|https):\/\/){1}[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,15}(:$port_mode)?(\/\S*)?$/is";
30+
preg_match($pattern, $url, $domain);
31+
if(!empty($domain[0])) return true;
32+
33+
//support IPv4 with optional port number
34+
$pattern = "/^((http|https):\/\/){1}";
35+
$pattern .= "([1-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])";
36+
$pattern .= "\.([0-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])";
37+
$pattern .= "(:$port_mode)?(\/\S*)?$/is";
38+
preg_match($pattern, $url, $ip4);
39+
if(!empty($ip4[0])) return true;
40+
41+
//support IPv6 with optional port number
42+
$pattern = "/^((http|https):\/\/){1}(\[[0-9a-fA-F:]+\])(:$port_mode)?(\/\S*)?$/is";
43+
preg_match($pattern, $url, $ip6);
44+
if(!empty($ip6[0])) return true;
2645

27-
return false !== $result ? true : false;
46+
return false;
2847
}
29-
/**
30-
* 检测电子邮件
31-
*
32-
* @param string $email
33-
*
34-
* @return boolean
35-
*/
36-
static public function checkEmail($email)
37-
{
48+
49+
/**
50+
* 检测电子邮件
51+
*
52+
* @param string $email
53+
*
54+
* @return boolean
55+
*/
56+
static public function checkEmail($email)
57+
{
3858
if(empty($email)) return false;
3959

40-
if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $email))
41-
{
42-
list($username, $domain)= explode('@', $email);
60+
if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $email))
61+
{
62+
list($username, $domain)= explode('@', $email);
4363

44-
if(!self::checkdnsrr($domain,'MX'))
45-
{
46-
return false;
47-
}
64+
if(!self::checkdnsrr($domain,'MX'))
65+
{
66+
return false;
67+
}
4868

49-
return true;
50-
}
69+
return true;
70+
}
5171

52-
return false;
72+
return false;
5373
}
5474

5575
/**

0 commit comments

Comments
 (0)