Regular Expression -- Obtain the physical address of the specified IP Address

Source: Internet
Author: User

Common website modules may include registration, login, posting, comments, and comments. The most important thing is to obtain IP addresses and process data. If you build your own IP database to occupy resources and the update is inconvenient, why not use a ready-made IP address for query? Next I wrote a PHP code to obtain the IP address's physical address.Code(Please kindly advise me if you have any flaws)

 <!  Doctype html  >  
< HTML Lang = " En " >
< Head >
< Title > </ Title >
< Meta charset = " UTF-8 " />
< Meta name = " Author " Content = "" />
< Meta name = " Keywords " Content = "" />
< Meta name = " Description " Content = "" />
</ Head >
< Body >
<? PHP
$ Ipaddr = " 218.75.124.100 " ;
$ Ip138addr = " Http://www.ip138.com/ips8.asp? IP = " . $ Ipaddr . " & Amp; Action = 2 " ;
$ Contents = File_get_contents ( $ Ip138addr );
$ Pattern = " /\ <Li. +? \/Li \>/ " ;
$ String = $ Contents ;
Preg_match_all ( $ Pattern , $ String , $ Addrarray );
$ Num = Count ( $ Addrarray [ 0 ]);
For ( $ I = 0 ; $ I < $ Num ; $ I ++ ){
Echo $ Addrarray [ 0 ] [ $ I ] = Mb_convert_encoding ( $ Addrarray [ 0 ] [ $ I ] , " UTF-8 " , " Gb2312 " );
Switch ( $ I ){
Case 0 : $ Str = " <Li> master data of this site: </LI> " ; Break ;
Case 1 : $ Str = " <Li> reference data 1: </LI> " ; Break ;
Case 2 : $ Str = " <Li> reference data 2: </LI> " ; Break ;
Case 3 : $ Str = " <Li> reference data 3: </LI> " ; Break ;
Case 4 : $ Str = " <Li> reference data 4: </LI> " ; Break ;
Case 5 : $ Str = " <Li> reference data 6: </LI> " ; Break ;
Case 6 : $ Str = " <Li> reference data 7: </LI> " ; Break ;
}
$ Addrarray [ 0 ] [ $ I ] = Trim ( $ Addrarray [ 0 ] [ $ I ] , $ Str );
}
$ Addrarray [ 0 ] [ 0 ] = Trim ( $ Addrarray [ 0 ] [ 0 ] , " : " );
Echo " <PRE> " ;
Print_r ( $ Addrarray );
Echo " </PRE> " ;
?>
</ Body >
</ Html >

This Code uses access to the http://www.ip138.com using a regular expression to intercept part of the content, where the regular expression is self-written.

Fortunately, the following processing results contain garbled characters. If $ STR (the first and last character of the string is removed) is defined, no garbled characters are found. In addition, a separate ": "statement, you can achieve the phenomenon of no garbled, do not know why ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.