Home  >  Article  >  php教程  >  解析oui文件,做一个Mac网卡制造商查询工具

解析oui文件,做一个Mac网卡制造商查询工具

PHP中文网
PHP中文网Original
2016-05-23 16:40:141465browse

php代码

<?php
/**
 * @Author: hzwangzhiwei
 * @Date:   2015-09-15 13:16:36
 * @Last Modified by:   hzwangzhiwei
 * @Last Modified time: 2015-09-15 13:53:20
 */

$file = fopen("oui.txt", "r") or exit("Unable to open file!");
$cnt = 0;
$mac_dict = array();
while(!feof($file)) {
	$line = fgets($file);
	if(preg_match("/^[0-9A-Z]{6}/", $line)){    
    	//验证通过
    	$cnt ++;
    	//1. 解析出mac前24位
    	$tmp = explode("\t\t", $line);
    	$mac_24 = explode("    ", $tmp[0])[0];
    	if (array_key_exists($mac_24, $mac_dict)) {
	    	//2. 解析出公司名字
	    	echo $mac_24 . "
";
	    	$company = $tmp[1];
	    	$mac_dict[$mac_24][&#39;com&#39;] = $mac_dict[$mac_24][&#39;com&#39;] . &#39;/ &#39; . $company;

	    	//3. 这一行为公司地址
	    	$mac_dict[$mac_24][&#39;ad&#39;] = $mac_dict[$mac_24][&#39;ad&#39;] . &#39;/ &#39; . fgets($file);
	    	//4. 这一行为公司所在大区域与代号
			$mac_dict[$mac_24][&#39;re&#39;] = $mac_dict[$mac_24][&#39;re&#39;] . &#39;/ &#39; . fgets($file);
	    	//5. 这一行为为国别代号
	    	$mac_dict[$mac_24][&#39;co&#39;] = $mac_dict[$mac_24][&#39;co&#39;] . &#39;/ &#39; . fgets($file);
    	}
    	else {
    		$mac_dict[$mac_24] = array();

	    	//2. 解析出公司名字
	    	$company = $tmp[1];
	    	$mac_dict[$mac_24][&#39;com&#39;] = $company;

	    	//3. 这一行为公司地址
	    	$mac_dict[$mac_24][&#39;ad&#39;] = fgets($file);
	    	//4. 这一行为公司所在大区域与代号
			$mac_dict[$mac_24][&#39;re&#39;] = fgets($file);
	    	//5. 这一行为为国别代号
	    	$mac_dict[$mac_24][&#39;co&#39;] = fgets($file);
    	}
		

	} else{
		//跳过
	}
}
echo $cnt;
echo &#39;====&#39;;
echo count(array_keys($mac_dict));

fclose($file);

$serialize = serialize($mac_dict);
file_put_contents(&#39;oui.dict&#39;, $serialize);

$d = unserialize(file_get_contents(&#39;oui.dict&#39;));
echo count(array_keys($d));

print_r($d[&#39;080030&#39;]);
echo "-----";

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:利用Redis生成订单号Next article:简单的SESSION管理类