Code to obtain the physical address of the specified IP in PHP (regular expression) _php tips

Source: Internet
Author: User
Tags get ip trim
Build your own IP database for resources, and update inconvenience, why not use the existing IP query it? The following oneself wrote a get IP physical address of the PHP code (there is a certain flaw, please do not hesitate to enlighten)
Copy Code code as follows:

<! DOCTYPE html>
<title></title>
<meta charset= "UTF-8"/>
<meta name= "Author" content= ""/>
<meta name= "keywords" content= ""/>
<meta name= "description" content= ""/>
<body>
<?php
$IPADDR = "218.75.124.100";
$ip 138Addr = "http://www.ip138.com/ips8.asp?ip=". $ipAddr. " &action=2 ";
$contents = file_get_contents ($ip 138Addr);
$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> site master data:</li>";
Case 1: $str = "<li> reference data:</li>";
Case 2: $str = "<li> reference data two:</li>";
Case 3: $str = "<li> reference data three:</li>";
Case 4: $str = "<li> reference data four:</li>";
Case 5: $str = "<li> reference data six:</li>";
Case 6: $str = "<li> reference data seven:</li>";
}
$addrArray [0][$i] = Trim ($addrArray [0][$i], $str);
}
$addrArray [0][0] = Trim ($addrArray [0][0], ":");
echo "<pre>";
Print_r ($addrArray);
echo "</pre>";
?>
</body>

This code uses regular expressions to intercept part of the content after accessing http://www.ip138.com, where regular expressions are written by themselves.

At first, the following processing results appear garbled, if the $STR (remove the logo) defined by the string is not garbled, and then add a separate ":" Statement, you can achieve no garbled phenomenon, do not know why ...
Regular expression--gets the physical address of the specified IP (ii) CHINAZ data
The result of the previous IP access is not perfect, the reason is because ip138 IP resolution to address foreign addresses is not very detailed, so again write a chianz.com code.
Copy Code code as follows:

<! DOCTYPE html>
<title></title>
<meta charset= "UTF-8"/>
<meta name= "Author" content= ""/>
<meta name= "keywords" content= ""/>
<meta name= "description" content= ""/>
<body>
<?php
$IPADDR = "4.4.44.4";
$IPCHINAZADDR = "http://tool.chinaz.com/IP/?IP=". $ipAddr;
$contents = file_get_contents ($IPCHINAZADDR);
$pattern = "/\=\=\>\>.+\<\/strong\>\<br \/\>/";
$string = $contents;
Preg_match_all ($pattern, $string, $addrArray);
$num = count ($addrArray [0]);
$pattern = "/(==>>). + (==>>\s)/";
for ($i = 0; $i < $num; $i + +) {
$addrArray [0][$i] = preg_replace ($pattern, "", $addrArray [0][$i]);
}
echo "<pre>";
Print_r ($addrArray);
echo "</pre>";
?>
</body>

The result was also satisfactory

Above is I write two to obtain the IP Physical address code, as a beginner, it is OK to write simple code is also a very interesting thing. But after all, is a beginner, can not better pay attention to security, space utilization, time efficiency and other issues, but also hope you leave your footprints, brother thanks

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.