分享PHP小技巧 PHP獲取中國(guó)IP段
大家在學(xué)習(xí)了PHP語(yǔ)言之后,肯定都想運(yùn)用這項(xiàng)功能功能強(qiáng)大的語(yǔ)言露上兩手,從中得到學(xué)有所成的滿足感。今天我們?yōu)榇蠹姨峁┮粋€(gè)PHP語(yǔ)言的小技巧,PHP獲取中國(guó)IP段的實(shí)現(xiàn)。#t#
去這個(gè)地址下載全球ip段http://ftp.apnic.net/apnic/dbase/data/country-ipv4.lst,里面cn是中國(guó)ip段范圍
新建一個(gè)php文件叫cnip.php,PHP獲取中國(guó)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");
- }
- }
- }
- }
- ?>
打開(kāi)cnip.txt 中國(guó)的ip段范圍就完成了PHP獲取中國(guó)IP段,
如果是在window環(huán)境下,將 fputs($handle, $ipLines[0]. '-'. $ipLines[2]. "\n"); 中的"\n"改為"\r\n"即可換行。