This article mainly introduces functions for detecting ip addresses and domain names in php and js, so that you can detect ip addresses and domain names in programs. For more information, see
This article mainly introduces functions for detecting ip addresses and domain names in php and js, so that you can detect ip addresses and domain names in programs. For more information, see
Php ip address format Detection Function
The Code is 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:
The Code is 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 ("Incorrect domain name ");
Document. ipform. ip. focus ();
Return false;
}
}
Else {
IpArray = ip. split (".");
J = ipArray. length
If (j! = 4)
{
Alert ("Incorrect IP Address ");
Document. ipform. ip. focus ();
Return false;
}
For (var I = 0; I <4; I ++)
{
If (ipArray [I]. length = 0 | ipArray [I]> 255)
{
Alert ("Incorrect IP Address ");
Document. ipform. ip. focus ();
Return false;
}
}
}
}
Note: The single row of the above function is very long. After copying, be sure not to split it.