php基础功能实现

需求
1.读取网页内容,方式不限制,能找到多种方式更好

<?php
//读取网页内容
header("content-type:text/html;charset=utf-8");
$url = "http://www.sina.com.cn/";
//1
$content = file_get_contents($url);
echo $content;

//2
$cookie = tempnam("/tmp", "CURLCOOKIE"); //建立一个具有唯一文件名的文件
$timeout = 5;
$ch = curl_init();
//在HTTP请求中包含一个”user-agent”头的字符串
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");
//想用PHP取回的URL地址
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1");
curl_setopt($ch, CURLOPT_PROXYPORT, "8087");
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
//传递一个包含HTTP cookie的头连接。
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
//设置这个选项为一个非零值(象 “Location: “)的头,服务器会把它当做HTTP头的一部分发送(注意这是递归的,PHP将发送形如 “Location: “的头)。
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//header中“Accept-Encoding: ”部分的内容,支持的编码格式为:"identity","deflate","gzip"。如果设置为空字符串,则表示支持所有的编码格式
curl_setopt($ch, CURLOPT_ENCODING, "");
//在启用CURLOPT_RETURNTRANSFER时候将获取数据返回
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//自动设置header中的referer信息
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
//针对https url
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//在发起连接前等待的时间,如果设置为0,则不等待。
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//设置curl允许执行的最长秒数
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
//指定最多的HTTP重定向的数量,这个选项是和CURLOPT_FOLLOWLOCATION一起使用的
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
$content = curl_exec($ch);
$resp = curl_getinfo($ch); //获取一个cURL连接资源句柄的信息
curl_close($ch);
echo $content;

//3
$f = fopen($url, "r");
if($f) {
$content = stream_get_contents($f,1024*1024);
echo $content;
}

2.读写文件,一次性读取写入,追加内容到文件尾部,转义字符提示,换行符”\r\n”

<?php
//读写文件,一次性读取写入,追加内容到文件尾部
$f = fopen("text.txt", "r");
$fw = fopen("output.txt", "a+");
while(!feof($f)) {
$temp = fgets($f);
echo $temp."
";
fwrite($fw, $temp);
}

fclose($f);
fclose($fw);

3.创建目录,删除文件,移动文件

<?php
//创建目录,删除文件,移动文件
if(!is_dir("work")) {
$re = mkdir("work",0777,true);
if($re) {
echo "目录创建成功";
}else {
echo "目录创建失败";
}

}else {
echo "目录已存在";

//删除文件
//rmdir("work");

//移动文件
rename("output.txt", "work/output.txt");
}

4.寻找A字符串的B字符串位置,替换A字符串的xxx为ccc,倒找A字符串中B字符串位置
A字符串为:aaaxxxaabcccbaaa
B字符串为b

<?php
$a = "aaaxxxaabcccbaaa";
$b = "b";
//寻找A字符串的B字符串位置
$pos = strpos($a, $b);
echo $pos;

//替换A字符串的xxx为ccc
$newstr = str_ireplace("xxx", "ccc", $a);

//倒找A字符串中B字符串位置
$pos = strrpos($a, $b);
echo $pos;

5.pdo方式操作数据库,涉及selectinsertupdatedelete操作

<?php
class pdo {

    private static $pdo;
    private static $dbtype;
    private static $dbhost = "localhost";
    private static $dbuser = "root";
    private static $dbpass = "";
    private static $database = "data";
    private static $conn;

    function getInstance() {
        if(self::$pdo) {
            return self::$pdo;
        }else {
            self::$pdo = new pdo();
            return self::$pdo;
        }
    }

    function connect() {
        $dbtype = self::$dbtype;
        $host = self::$dbhost;
        $dbname = self::$database;
        $user = self::$dbuser;
        $pass = self::$dbpass;
        try {
            self::$conn = new PDO("{$dbtype}:host={$host};dbname={$dbname}",$user,$pass);
            self::$conn->exec("set names utf8");
        }catch (PDOException $e) {
            die('Error:'.$e->getMessage());
        }
    }

    function query($sql) {
        self::$conn->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER);
        return self::$conn->query($sql);
    }

    function fetchAll($sql) {
        $rows = $this->query($sql);
        if($rows) {
            $rows->setFetchMode(PDO::FETCH_ASSOC);  //设置获取结果集为关联数组形式
            return $rows->fetchAll();
        }
        return false;
    }

    function preFetchAll($sql) {
        $rs = self::$conn->prepare($sql);
        $rs->execute();
        while($row = $rs->fetch()) {
           $rows[] = $row;
        }
        return $rows;
    }
    /*
     * data : array("name"=>"111","age"=>"222")
     * */
    function insert($table, $data) {
        foreach ($data as $key=>$value) {
            $keys[] = $key;
            $values[] = ":".$value;
        }
        $keysStr = implode(",", $keys);
        $valueStr = implode(",", $values);
        $sql = "insert into {$table}($keysStr) values({$valueStr})";
        return self::$conn->exec($sql);
    }
    /*
     * data : array("name"=>"111","age"=>"222")
     * */
    function update($table, $data, $where) {
        foreach ($data as $key=>$value) {
            $arr[] = "`".$key."`='".$value."'";
        }
        $str = implode(",", $arr);
        $sql = "update {$table} set {$str}".($where==null?"":$where);
        return self::$conn->exec($sql);
    }

    function delete($table, $where) {
        $sql = "delete from {$table} {$where}";
        return self::$conn->exec($sql);
    }
}
如果您觉得本文对您有用,欢迎捐赠或留言~
微信支付
支付宝

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注