分类 php 下的文章

php操作html元素,使用PHP Simple HTML DOM Parser

今天需要一个操作html元素的的功能,记得php有xml的,但是用的真心少,在网上搜下教程,没想到还有PHP Simple HTML DOM Parser这个开源项目,看了下介绍,可以像jquery那样操作html,赶紧下载下来体验下得意
代码如下
header('Content-type:text/html;charset=gb2312;');
//首先引入类文件
require 'simple_html_dom.php';
//读取文件里面的内容
$str=file_get_contents('tbody.txt');
//实例化对象
$html=new simple_html_dom();
//存放取出数据的数组
$temp_arr=array();
//解析html字符串
$html->load($str);
//循环取出的变量,因为是要取出tr下面的某几个td里面的内容,所以这里先查找出所有tr
foreach ($html->find('tr') as $key => $value) {
$temp=array();
//第一行是边个的标题,跳过
if ($key==0) {
continue;
}
//循环tr下面的td
foreach ($value->children() as $key_1 => $value_1) {
//因为这个表格是不规则的,用到了rowspan,所以这里这样操作了下
if (isset($value_1->attr['class'])&&$value_1->attr['class']=='xxx') {
//find是找出当前元素下面的标签,后面没数字的话应该就是返回所有的,有数字的话就是返回指定的那一个
$temp[]=trim($value_1->find('a',0)->plaintext);
$temp[]=trim($value->children($key_1+3)->plaintext);
$temp[]=trim($value->children($key_1+4)->plaintext);
$temp[]=trim($value->children($key_1+5)->plaintext);
$temp_arr[]=$temp;
}
continue;
}
}
var_dump($temp_arr);
?>

中文文档网址:http://www.ecartchina.com/php-simple-html-dom/manual.htm

pdo使用记录

<?php
/**
 * Created by PhpStorm.
 * User: jin
 * Date: 2015/9/25
 * Time: 11:54
 */
class MysqlDb
{
    static $dbArr = array();
    private $conection = null;
    private $dsn = null;
    private $usernName = null;
    private $userPwd = null;
    private $parameter = null;
    function __construct($username,$pwd,$dbname,$host,$parameter = array())
    {
        $this->dsn='mysql:host='.$host.';dbname='.$dbname;
        $this->usernName = $username;
        $this->userPwd = $pwd;
        $this->parameter = $parameter;
        $this->connect();
    }
    private function connect()
    {
        //当初始化对象失败,也就是连接数据库失败时,会抛出PDOException异常
        try{
            //实例化对象
            $this->conection = new PDO($this->dsn,$this->usernName,$this->userPwd,$this->parameter);
            //设置编码
            $this->conection->exec('set names utf8');
            return true;
        }catch(PDOException $e){
            //结束程序,并打印错误信息
            error_log($e->getMessage(), 0);
            return $e->getMessage();
        }
    }
    //写入操作
    function excute($sql,$parameter = array())
    {
        try
        {
            $prepare = $this->conection->prepare($sql);
            $res = $prepare->execute($parameter);
        }
        catch(PDOException $e)
        {
            if($e->errorInfo[0] == 70100 || $e->errorInfo[0] == 2006){
                $count = 0;
                while(!$this->connect()){
                    sleep(1);
                    echo "数据库重新连接失败(try:{$count})\n";
                    $count++;
                };
                $res = $this->excute($sql, $parameter);
            }
            else
            {
                exit($e->errorInfo[2]);
            }
        }
        return $res;
    }
    //查询操作
    function query($sql,$parameter = array())
    {
        try
        {
            $prepare = $this->conection->prepare($sql);
            $prepare->execute($parameter);
            //设置返回的是索引数组
            $prepare->setFetchMode(PDO::FETCH_ASSOC);
            //取出结果数组
            $row = $prepare->fetchAll();
        }
        catch(PDOException $e)
        {
            if($e->errorInfo[0] == 70100 || $e->errorInfo[0] == 2006){
                $count = 0;
                while(!$this->connect()){
                    sleep(1);
                    echo "数据库重新连接失败(try:{$count})\n";
                    $count++;
                };
                $res = $this->query($sql, $parameter);
            }
            else
            {
                exit($e->errorInfo[2]);
            }
        }
        return $row;
    }
    //只查询第一条
    function find($sql,$parameter = array())
    {
        $row = $this->query($sql,$parameter);
        return current($row);
    }
    //关闭连接
    function close()
    {
        $this->conection = null;
    }
    function getLastInsertId()
    {
        return $this->conection->lastInsertId();
    }
}

获取文件时间信息

filemtime ( string filename )

返回文件上次被修改的时间,出错时返回 FALSE。时间以 Unix 时间戳的方式返回,可用于 date()。
例如:

$a=filemtime("log.txt");
echo "修改时间:".date("Y-m-d H:i:s",$a).";

filectime ( string filename )

返回文件上次 inode 被修改的时间,如果出错则返回 FALSE。时间以 Unix 时间戳的方式返回。
例如:

$a=filectime("log.txt");
echo "创建时间:".date("Y-m-d H:i:s",$a).";

fileatime ( string filename )

返回文件上次被访问的时间,如果出错则返回 FALSE。时间以 Unix 时间戳的方式返回。
例如:

$a=fileatime("log.txt");
echo "修改时间:".date("Y-m-d H:i:s",$a).";

页面最大过期时间

  1. php.ini
//在php.ini 中设置,如30秒
max_execution_time = 30
  1. [代码]ini_set()函数
//虚拟主机,且服务器允许使用ini_set()函数,可在PHP代码中设置:
ini_set('max_execution_time',864000);