300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > PHP获取手机号归属地 PHP获取最新手机号段属地类

PHP获取手机号归属地 PHP获取最新手机号段属地类

时间:2023-07-19 02:52:20

相关推荐

PHP获取手机号归属地 PHP获取最新手机号段属地类



<?php

/**

php获取手机号段归属地类

@

@link

@date/4/9

*/

class MobileCity {

/**

* 缓存文件保存目录

*/

public $CACHE_DIR;

/**

* 手机号段归属地文本文件

*/

public $MOBILE_FILE;

/**

* 文本文件每行间隔符

*/

public $ROW_SPACE;

/**

* 文本文件列数据字段间隔符

*/

public $LINE_SPACE;

/**

* 文本文件数据格式对应表字段

*

* @var string

*/

public $FORMAT;

public function __construct()

{

$this->CACHE_DIR = './mobile_city';

$this->MOBILE_FILE = './Mobile.txt';

$this->ROW_SPACE = "\r\n";

$this->LINE_SPACE = ",";

//1,"1300000","山东","济南","中国联通","0531","250000"

//按间隔符切开从0开始对应字段

//可以根据需要生成字段,不需要注释

$this->FORMAT = array(

0=>'',

1=>'city_mobile',

2=>'city_province',

3=>'city_city',

//4=>'city_com',

//5=>'city_code',

//6=>'city_zip',

);

}

/**

* 文本文件生成缓存文件

* @return array

*/

public function writeCache()

{

if( !is_file($this->MOBILE_FILE) )

{

//return '文件不存在!';

die('文件不存在!');

}

$data = $this->getFile($this->MOBILE_FILE);

$rows = explode($this->ROW_SPACE,$data);

unset($data);

foreach( $rows as $j=>$row )

{

$row = str_replace(array('"',"'"),'',$row);

$line = explode($this->LINE_SPACE,$row);

unset($rows[$j]);

unset($line_data);

foreach( $line as $k=>$d )

{

if( $d != '' && $this->FORMAT[$k] )

{

$line_data[$this->FORMAT[$k]] = $d;

}

}

$mobile = $line_data[city_mobile];

$pre = substr($mobile,0,3);

unset($line_data[city_mobile]);

$data_array[$pre][$mobile]=$line_data;

}

unset($rows);

foreach( $data_array as $pre=>$row )

{

$this->writeCacheFile($this->CACHE_DIR,$pre,$row);

unset($data_array[$pre]);

}

}

/**

* 创建多级目录

*

* @param string $dir 目录

* @param string $mode 目录权限

* @return true

*/

protected function mkdirs($dir,$mode=0777){

if(!is_dir($dir)){

$this->mkdirs(dirname($dir), $mode);

return mkdir($dir, $mode);

}

return true;

}

/**

* 读文件内容

*

* @param string $file 文件

* @return true

*/

protected function getFile($file){

$f = fopen($file,"r");

$d = fread($f,filesize($file));

fclose($f);

return $d;

}

/**

* 写文件内容

*

* @param string $file 文件

* @param string $data 数据

* @param string $m 访问类型

* @return true

*/

protected function putFile($file,$data,$m='w'){

$this->mkdirs(dirname($file));

$f = fopen($file,$m);

$d = fwrite($f,$data);

fclose($f);

}

/**

* 数组转数组字符串

*

* @param string $obj 数组

* @param string $mod

* @return array

*/

protected function ArrayToString($obj,$mod=1){

if(empty($obj))return "array()";

$objType=gettype($obj);

if ($objType=='array') {

$objstring = "array(";

foreach ($obj as $objkey=>$objv) {

if($mod==1)$objstring .= is_numeric($objkey) && strlen($objkey)<8 && $objkey{0}!='0' ? $objkey.'=>':"'".$objkey."'=>";

$vtype =gettype($objv);

if ($vtype=='integer') {

$objstring .="$objv,";

}else if ($vtype=='double'){

$objstring .="$objv,";

}else if ($vtype=='string'){

$objstring .="'".$objv."',";

}else if ($vtype=='array'){

$objstring .=$this->ArrayToString($objv).",\n";

}else if ($vtype=='object'){

$objstring .=$this->ArrayToString($objv).",\n";

}else{

$objstring .="'".$objv."',";

}

}

return substr($objstring,0,-1).")";

}

}

/**

* 写PHP数组缓存文件

*

* @param string $dir 目录

* @param string $file 文件

* @param string $data 数据

*/

protected function writeCacheFile($dir,$file,$data){

$data = $this->ArrayToString($data);

$data = "<?php\n\$mobile_city_cae = ".$data.";\n?>";

$this->putFile($dir."/".$file.'.cae.php',$data);

}

/**

* 获取手机归属地

*

* @param string $m 手机号

* @return array

*/

function getMobileCity($m)

{

$pre = substr($m,0,3);

$pre_mobile = substr($m,0,7);

$file = $this->CACHE_DIR.'/'.$pre.'.cae.php';

if( is_file($file) )

{

include $file;

}

if( $mobile_city_cae[$pre_mobile] )

{

return $mobile_city_cae[$pre_mobile];

}

return array('city_province'=>'');

}

}

/**/

//$ROOT = dirname(__FILE__);

//$Mcity = new MobileCity();

//缓存文件目录

//$Mcity->CACHE_DIR = $ROOT.'/mobile_city';

//第一次生成缓存文件

//$Mcity->MOBILE_FILE = $ROOT.'/Mobile.txt';

//$Mcity->writeCache();

//print_r($Mcity->getMobileCity('15900001111'));

?>

PHP获取手机号归属地源代码和号段数据下载

/detail/haohao2345/9828511

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。