分享PHP小技巧 PHP獲取中國IP段
大家在學(xué)習(xí)了PHP語言之后,肯定都想運用這項功能功能強大的語言露上兩手,從中得到學(xué)有所成的滿足感。今天我們?yōu)榇蠹姨峁┮粋€PHP語言的小技巧,PHP獲取中國IP段的實現(xiàn)。#t#
去這個地址下載全球ip段http://ftp.apnic.net/apnic/dbase/data/country-ipv4.lst,里面cn是中國ip段范圍
新建一個php文件叫cnip.php,PHP獲取中國IP段具體代碼示例:
- < ?php
 - $filefile = file('./country-ipv4.lst');
 - $handle = fopen('./cnip.txt', 'a');
 - if($handle) {
 - foreach ($file as $key => $val) {
 - if (strpos($val, '#') !== 0) {
 - $ipLines = explode(' ', $val);
 - if ($ipLines[6] == 'cn') {
 - fputs($handle, $ipLines[0]. '-'. $ipLines[2]. "\n");
 - }
 - }
 - }
 - }
 - ?>
 
打開cnip.txt 中國的ip段范圍就完成了PHP獲取中國IP段,
如果是在window環(huán)境下,將 fputs($handle, $ipLines[0]. '-'. $ipLines[2]. "\n"); 中的"\n"改為"\r\n"即可換行。















 
 
 
 
 
 
 