Home > Article > Backend Development > How to use php to obtain region instance sharing through IP address
Many projects will need to obtain the user's geographical information, but sometimes it cannot be obtained directly through the geographical location interface, so what should we do? This article mainly introduces the method of using PHP to obtain the IP address and region through Chianz.com, which is a very practical technique for parsing IP addresses and regions.
The example code is as follows:
<!DOCTYPE html> <html> <head> <title></title> <meta charset="UTF-8" /> <meta name="author" content="" /> <meta name="keywords" content="" /> <meta name="description" content="" /> </head> <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 class="brush:php;toolbar:false">"; print_r($addrArray); echo ""; ?>