PHP IP address format detection function
Copy Code code as follows:
function Checkip ($IP) {
$ip = Str_replace ("", "", $IP);
$ip = Strtolower ($IP);
$ip = Str_replace ("http://", "", $IP);
$ip = Str_replace ("https://", "", $IP);
if (Preg_match ('/[a-za-z_-]/', $ip)) {
if (Preg_match ('/^) ([\w-]+\.) + ((COM) | (NET) | (org) | (gov\.cn) | (info) | (cc) | (com\.cn) | (net\.cn) | (org\.cn) | (name) | (Biz) | (TV) | (CN) | (mobi) | (name) | (SH) | (AC) | (IO) | (TW) | (com\.tw) | (HK) | (COM\.HK) | (WS) | (travel) | (US) | (tm) | (LA) | (me\.uk) | (org\.uk) | (ltd\.uk) | (plc\.uk) | (in) | (EU) | (IT) | (JP) | (CO) | (Me) | (MX) | (CA) | (AG) | (com\.co) | (net\.co) | (nom\.co) | (COM\.AG) | (NET\.AG) | (FR) | (ORG\.AG) | (AM) | (Asia) | (at) | (BE) | (BZ) | (COM\.BZ) | (NET\.BZ) | (net\.br) | (com\.br) | (DE) | (es) | (com\.es) | (nom\.es) | (org\.es) | (FM) | (GS) | (co\.in) | (firm\.in) | (gen\.in) | (ind\.in) | (net\.in) | (org\.in) | (Jobs) | (ms) | (com\.mx) | (NL) | (NU) | (co\.nz) | (net\.nz) | (org\.nz) | (TC) | (TK) | (org\.tw) | (idv\.tw) | (co\.uk) | (VG) | (AD) | (AE) | (AF) | (AI) | (AL) | (AN) | (AO) | (aq) | (AR) | (AS) | (AU) | (AW) | (AZ) | (BA) | (BB) | (BD) | (BF) | (BG) | (BH) | (BI) | (BJ) | (BM) | (BN) | (bo) | (BR) | (BS) | (BT) | (BV) | (BW) | (by) | (CD) | (CF) | (CG) | (CH) | (CI) | (CK) | (CL) | (cm) | (CR) | (CU) | (CV) | (CX) | (CY) | (CZ) | (DJ) | (DK) | (DM) | (DO) | (DZ) | (EC) | (ee) | (eg) | (er) | (ET) | (FI) | (FJ) | (FK) | (FO) | (GA) | (GD) | (GE) | (GF) | (GG) | (GH) | (GI) | (GL) | (GM) | (GN) | (GP) | (GQ) | (GR) | (GT) | (gu) | (GW) | (GY) | (HM) | (HN) | (HR) | (HT) | (HU) | (ID) | (IE) | (IL) | (IM) | (IQ) | (IR) | (IS) | (JE) | (JM) | (JO) | (KE) | (kg) | (KH) | (KI) | (km) | (kn) | (KR) | (kw) | (ky) | (KZ) | (lb) | (LC) | (LI) | (LK) | (LR) | (LS) | (LT) | (LU) | (LV) | (ly) | (MA) | (MC) | (MD) | (mg) | (MH) | (MK) | (ml) | (mm) | (MN) | (MO) | (MP) | (MQ) | (MR) | (MT) | (MU) | (MV) | (MW) | (My) | (MZ) | (NA) | (NC) | (NE) | (NF) | (NG) | (NI) | (NO) | (NP) | (NR) | (NZ) | (OM) | (PA) | (PE) | (PF) | (PG) | (ph) | (PK) | (PL) | (PM) | (PN) | (PR) | (PS) | (PT) | (PW) | (py) | (QA) | (RE) | (RO) | (RU) | (rw) | (SA) | (SB) | (SC) | (SD) | (SE) | (SG) | (SI) | (SK) | (SL) | (SM) | (SN) | (SR) | (ST) | (SV) | (SY) | (SZ) | (TD) | (TF) | (TG) | (TH) | (TJ) | (TL) | (TN) | (To) | (TR) | (TT) | (TZ) | (UA) | (UG) | (UK) | (UY) | (UZ) | (VA) | (VC) | (VE) | (vi) | (VN) | (VU) | (WF) | (Ye) | (YT) | (Yu) | (za) | (ZM) | (ZW)) $/i ', $ip)) {
return true;
}else{
return false;
}
}else{
$ipArray = Explode (".", $ip);
$j = count ($ipArray);
if ($j!=4)
{
return false;
}
For ($i =0 $i <4; $i + +) {
if (strlen ($ipArray [$i]) ==0 | | $ipArray [$i]>255) {
return false;
}
}
return true;
}
}
JS Code:
Copy Code code as follows:
function Checkip () {
var iparray,ip,j;
ip = document.ipform.ip.value;
if (Ip.indexof ("") >=0) {
IP = ip.replace (//g, "");
Document.ipform.ip.value = IP;
}
if (Ip.tolowercase (). IndexOf ("http://") ==0) {
ip = ip.slice (7);
Document.ipform.ip.value = IP;
}
if (Ip.tolowercase (). IndexOf ("https://") ==0) {
IP = ip.slice (8);
Document.ipform.ip.value = IP;
}
if (Ip.slice (ip.length-1) = = "/") {
ip = ip.slice (0,ip.length-1);
Document.ipform.ip.value = IP;
}
if (/[a-za-z_-]/.test (IP)) {
if (!/^ ([\w-]+\.) + ((COM) | (NET) | (org) | (gov\.cn) | (info) | (cc) | (com\.cn) | (net\.cn) | (org\.cn) | (name) | (Biz) | (TV) | (CN) | (mobi) | (name) | (SH) | (AC) | (IO) | (TW) | (com\.tw) | (HK) | (COM\.HK) | (WS) | (travel) | (US) | (tm) | (LA) | (me\.uk) | (org\.uk) | (ltd\.uk) | (plc\.uk) | (in) | (EU) | (IT) | (JP) | (CO) | (Me) | (MX) | (CA) | (AG) | (com\.co) | (net\.co) | (nom\.co) | (COM\.AG) | (NET\.AG) | (FR) | (ORG\.AG) | (AM) | (Asia) | (at) | (BE) | (BZ) | (COM\.BZ) | (NET\.BZ) | (net\.br) | (com\.br) | (DE) | (es) | (com\.es) | (nom\.es) | (org\.es) | (FM) | (GS) | (co\.in) | (firm\.in) | (gen\.in) | (ind\.in) | (net\.in) | (org\.in) | (Jobs) | (ms) | (com\.mx) | (NL) | (NU) | (co\.nz) | (net\.nz) | (org\.nz) | (TC) | (TK) | (org\.tw) | (idv\.tw) | (co\.uk) | (VG) | (AD) | (AE) | (AF) | (AI) | (AL) | (AN) | (AO) | (aq) | (AR) | (AS) | (AU) | (AW) | (AZ) | (BA) | (BB) | (BD) | (BF) | (BG) | (BH) | (BI) | (BJ) | (BM) | (BN) | (bo) | (BR) | (BS) | (BT) | (BV) | (BW) | (by) | (CD) | (CF) | (CG) | (CH) | (CI) | (CK) | (CL) | (cm) | (CR) | (CU) | (CV) | (CX) | (CY) | (CZ) | (DJ) | (DK) | (DM) | (DO) | (DZ) | (EC) | (ee) | (eg) | (er) | (ET) | (FI) | (FJ) | (FK) | (FO) | (GA) | (GD) | (GE) | (GF) | (GG) | (GH) | (GI) | (GL) | (GM) | (GN) | (GP) | (GQ) | (GR) | (GT) | (gu) | (GW) |(GY) | (HM) | (HN) | (HR) | (HT) | (HU) | (ID) | (IE) | (IL) | (IM) | (IQ) | (IR) | (IS) | (JE) | (JM) | (JO) | (KE) | (kg) | (KH) | (KI) | (km) | (kn) | (KR) | (kw) | (ky) | (KZ) | (lb) | (LC) | (LI) | (LK) | (LR) | (LS) | (LT) | (LU) | (LV) | (ly) | (MA) | (MC) | (MD) | (mg) | (MH) | (MK) | (ml) | (mm) | (MN) | (MO) | (MP) | (MQ) | (MR) | (MT) | (MU) | (MV) | (MW) | (My) | (MZ) | (NA) | (NC) | (NE) | (NF) | (NG) | (NI) | (NO) | (NP) | (NR) | (NZ) | (OM) | (PA) | (PE) | (PF) | (PG) | (ph) | (PK) | (PL) | (PM) | (PN) | (PR) | (PS) | (PT) | (PW) | (py) | (QA) | (RE) | (RO) | (RU) | (rw) | (SA) | (SB) | (SC) | (SD) | (SE) | (SG) | (SI) | (SK) | (SL) | (SM) | (SN) | (SR) | (ST) | (SV) | (SY) | (SZ) | (TD) | (TF) | (TG) | (TH) | (TJ) | (TL) | (TN) | (To) | (TR) | (TT) | (TZ) | (UA) | (UG) | (UK) | (UY) | (UZ) | (VA) | (VC) | (VE) | (vi) | (VN) | (VU) | (WF) | (Ye) | (YT) | (Yu) | (za) | (ZM) | (ZW)) $/i.test (IP)) {
Alert ("Not the correct domain name");
Document.ipform.ip.focus ();
return false;
}
}
else{
Iparray = Ip.split (".");
j = Iparray.length
if (j!=4)
{
Alert ("Not the correct IP");
Document.ipform.ip.focus ();
return false;
}
for (Var i=0;i<4;i++)
{
if (iparray[i].length==0 | | iparray[i]>255)
{
Alert ("Not the correct IP");
Document.ipform.ip.focus ();
return false;
}
}
}
}
Note: The function of the above line is very long, everyone copied after the attention not to branch.