php file_get_contents函数轻松采集ip138数据

 更新时间:2016年11月25日 15:57  点击:2042

<?php教程
//全国,判断条件是$REQUEST_URI是否含有html
if (!strpos($_SERVER["REQUEST_URI"],".html"))
{
$page="http://qq.ip138.com/weather/";
$html = file_get_contents($page,'r');
$pattern="/<B>全国主要城市、县当天和未来五天天气趋势预报在线查询</B>(.*?)<center style="padding:3px">/si";
//正则匹配之间的html
preg_match($pattern,$html,$pg);
echo "";
//正则替换远程地址为本地地址
$p=preg_replace('//weather/(w+)/index.htm/', 'tq.php/$1.html', $pg[1]);
echo $p;
}
//省,判断条件是$REQUEST_URI是否含有?
else if(!strpos($_SERVER["REQUEST_URI"],"?")){
//yoyo推荐的使用分割获得数据,这里是获得省份名称
$province=explode("/",$_SERVER["REQUEST_URI"]);
$province=explode(".",$province[count($province)-1]);
$province=$province[0];
//被注释掉的是我自己写出来的正则,感觉写的不好,但效果等同上面
//preg_match('/[^/]+[.(html)]$/',$_SERVER["REQUEST_URI"],$pro);
//$province=preg_replace('/.html/','',$pro[0]);
$page="http://qq.ip138.com/weather/".$province."/index.htm";
//获取html数据之前先尝试打开页面,防止恶意输入地址导致出错
if (!@fopen($page, "r")) {
die("对不起,该地址不存在!<a href=javascript教程:history.back(1)>点击这里返回</a>");
exit(0);
}
$html = file_get_contents($page,'r');
$pattern="/五天天气趋势预报</B>(.*?)请输入输入市/si";
preg_match($pattern,$html,$pg);
echo "";
//正则替换,获取省份,城市
$p=preg_replace('//weather/(w+)/(w+).htm/', '$2.html?pro=$1', $pg[1]);
echo $p;
}
else {
//市,通过get传递省份
$pro=$_REQUEST['pro'];
$city=explode("/",$_SERVER["REQUEST_URI"]);
$city=explode(".",$city[count($city)-1]);
$city=$city[0];
//preg_match('/[^/]+[.(html)]+[?]/',$_SERVER["REQUEST_URI"],$cit);
//$city=preg_replace('/.html?/','',$cit[0]);
$page="http://qq.ip138.com/weather/".$pro."/".$city.".htm";
if (!@fopen($page, "r")) {
die("对不起,该地址不存在!<a href=javascript:history.back(1)>点击这里返回</a>");
exit(0);
}
$html = file_get_contents($page,'r');
$pattern="/五天天气趋势预报</B>(.*?)请输入输入市/si";
preg_match($pattern,$html,$pg);
echo "";
//获取真实的图片地址
$p=preg_replace('//image//', 'http://qq.ip138.com/image/', $pg[1]);
echo $p;
}
?>

class cacheClearFile{
 
 var $dir = '111cn.Net'; 
 
 function __construct()
 {
  $this->listFils(); 
 }  

  
 function listFils()
 {
  if(is_dir($this->dir))
  { 
   if($dir_file=opendir($this->dir))
   {
    while(($dir_list=readdir($dir_file))!==false)
    {
     if($dir_list!="." && $dir_list!="..")
     {
      if( is_file($dir_list)
      {
       unlink($dir_list);
      }
      else
      {
       $this->dir =$dir_list;
       listFils();
      }     
     }
    }
   }else{
    echo("目录不能打开");
   }
  }
  else
  {
   echo("不是目录");
  }
 }
}

//实例调用方法

new cacheClearFile();

/*
只用了一句就OK了,因为我们用了构造函数所以只在创建类他就会自动给执行了。
本站原创文章转载注明出处 www.111cn.net 中国WEB第一站

php是外国人做的,在中文支持上有些地方做得不完美,就在我们文件上传时就发现了,上传中文竟然不成功,
下面我们来看看实例与解决方法吧。
*/
$filepath ='upfile/';

if ($_FILES['file']['error']==0)
{
    
 if( move_uploaded_file($_FILES['file']['tmp_name'],$filepath.$_FILES['file']['name']))
 {
  echo '文件上传成功';
 }
 else
 {
  echo '文件上传失败';
 }
}


/*
.....
上面是一段简单的文件上传代码,传英语的是没有问题,但是中文的就不会成功了,好了,那我们如何处理中文文件名上传失败问题呢。
方法很简单就是对上传的文件名重命名就OK了,下面我们把程序修改一下。如下。
*/

if ($_FILES['file']['error']==0){
   
    $ext = end(explode('.',$_FILES['file']['name']));
 $fileRandName = time(); //根据当前时间生成一个字符串
 
 if( move_uploaded_file($_FILES['file']['tmp_name'],$filepath.$fileRandName.'.'.$ext))
 {
  echo '文件上传成功';
 }
 else
 {
  echo '文件上传失败';
 }
}

//这样不管你上传的是什么文件都OK了。
//本站原创文章转载注明来自www.111cn.net

 $time = '2010-1-11';
 
 //定义一个日间我相把把它变成2010-01-1
 
 $splitDate = explode("-",$time);
 
 //进行拆分以"-"分开
 
 $stime = mktime(0,0,0,$splitDate[1],$splitDate[2],$splitDate[0]);
 
 //再用mktime把它转换成时间载
 
 if( intval( $splitDate[1] )<10 && substr( $splitDate[1],0,1) !='0' )
 {
  $splitDate[1] = '0'.$splitDate[1];
 }
 //对月分取一个数字判断如果是01这种格式就不操作反之就加个0
 
 if( intval( $splitDate[2] )<10 && substr( $splitDate[2],0,1) !='0' )
 {
  $splitDate[2] = '0'.$splitDate[2];
 }
 //对日期作同样的作了,小于10就补0
 
 /*
  函数分析:
   explode 使用一个字符串分割另一个字符串 array explode ( string separator, string string [, int limit] )
   <a href=/phper/21/101d7c9a91356a428c8039c03dd4500b.htm>mktime</a>  函数返回一个日期的 Unix 时间戳。 mktime(hour,minute,second,month,day,year,is_dst)
   <a href=/phper/18/9351c693420d88336920eb2c12fca245.htm>intval</a> int intval ( mixed var [, int base])
通过使用特定的进制转换(默认是十进制),返回变量 var 的 integer 数值。

   substr  函数返回字符串的一部分 substr(string,start,length)
   
 */
 ?>

本站原创教程,转载注明来自www.111cn.net


/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;

//close the zip -- done!
$zip->close();

//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
/***** Example Usage ***/
$files=array('file1.jpg', 'file2.jpg', 'file3.gif');
create_zip($files, 'myzipfile.zip', true);


17. PHP解压缩 Zip 文件


/**********************
*@file - path to zip file
*@destination - destination directory for unzipped files
*/
function unzip_file($file, $destination){
// create object
$zip = new ZipArchive() ;
// open archive
if ($zip->open($file) !== TRUE) {
die ('Could not open archive');
}
// extract contents to destination directory
$zip->extractTo($destination);
// close archive
$zip->close();
echo 'Archive extracted to directory';
}

[!--infotagslink--]

相关文章

  • php正确禁用eval函数与误区介绍

    eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
  • php中eval()函数操作数组的方法

    在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
  • Python astype(np.float)函数使用方法解析

    这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08
  • php 中file_get_contents超时问题的解决方法

    file_get_contents超时我知道最多的原因就是你机器访问远程机器过慢,导致php脚本超时了,但也有其它很多原因,下面我来总结file_get_contents超时问题的解决方法总结。...2016-11-25
  • Python中的imread()函数用法说明

    这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • php file_get_contents 设置代理抓取页面示例

    file_get_contents函数在php中可以直接打开本地文件也可以直接抓取远程服务器文件,如果简单的采集我们可以使用file_get_contents直接来操作,如果有防采集我们可能需要...2016-11-25
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • 金额阿拉伯数字转换为中文的自定义函数

    CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • Android开发中findViewById()函数用法与简化

    findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20
  • PHP用strstr()函数阻止垃圾评论(通过判断a标记)

    strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
  • php报错file_get_contents(): php_network_getaddresses问题

    本文章来为各位介绍一篇关于file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known...错误解决办法。 昨天,服务器的DN...2016-11-25
  • PHP函数分享之curl方式取得数据、模拟登陆、POST数据

    废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07
  • php中的foreach函数的2种用法

    Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28
  • C语言中free函数的使用详解

    free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
  • PHP函数strip_tags的一个bug浅析

    PHP 函数 strip_tags 提供了从字符串中去除 HTML 和 PHP 标记的功能,该函数尝试返回给定的字符串 str 去除空字符、HTML 和 PHP 标记后的结果。由于 strip_tags() 无法实际验证 HTML,不完整或者破损标签将导致更多的数...2014-05-31
  • PHP file_get_contents设置超时处理方法

    file_get_contents的超时处理话说,从PHP5开始,file_get_content已经支持context了(手册上写着:5.0.0 Added the context support. ),也就是说,从5.0开始,file_get_contents其实也可以POST数据。今天说的这篇是讲超时的,确实在...2013-10-04
  • PHP加密解密函数详解

    分享一个PHP加密解密的函数,此函数实现了对部分变量值的加密的功能。 加密代码如下: /* *功能:对字符串进行加密处理 *参数一:需要加密的内容 *参数二:密钥 */ function passport_encrypt($str,$key){ //加密函数 srand(...2015-10-30
  • SQL Server中row_number函数的常见用法示例详解

    这篇文章主要给大家介绍了关于SQL Server中row_number函数的常见用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-08