<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>无标题文档</title>
</head>
<body>
<?php
//mysql
$dsn = ‘mysql:host=localhost’.’;dbname=name’;
$pdo = new PDO($dsn, “username”, “ps”);
$pdo->query(“set names utf8”);
/////////////////////////////////////
$handle = fopen(“ips.txt”, “r”);
$i = 0;
if ($handle) {
while (!feof($handle))
{
/*
if ($i >100)
{
break;
}
$i++;
*/
$str = fgets($handle, 4096);
echo $str.”<br>”;
$ip_start = substr($str,0,15);
$ip_end = substr($str,16,15);
$ip_start = str_replace(” “,””,$ip_start);
$ip_end = str_replace(” “,””,$ip_end);
$county_region = substr($str,32,strlen($str)-1);
//$county_region = mb_convert_encoding($county_region, “UTF-8”, “GBK”);
echo $ip_start.”==”.$ip_end.”==”.$county_region.”<br>”;
$ip_start_to_long_int = ip_to_long_int($ip_start);
$ip_end_to_long_int = ip_to_long_int($ip_end);
//insert into mysql
$sql = “insert into `ips`(`ip_start`,`ip_start_number`,`ip_end`,`ip_end_number`,`info`) values( ‘$ip_start’,$ip_start_to_long_int ,’$ip_end’,$ip_end_to_long_int,’$county_region’)”;
$res = $pdo->exec($sql);
if ($res == 0)
{
echo $sql.”<br>”;
}
}
}
echo “ok”;
function ip_to_long_int($ip){
$arr = explode(‘.’,$ip);
$res = $arr[0]*255*255*255 + $arr[1]*255*255 + $arr[2]*255 + $arr[3];
return $res;
}
?>
</body>
</html>