预览模式: 普通 | 列表

php采集常见方法和函数

$url="http://www.xxx.com/";
$user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
$cookiefile="/tmp/getgb.txt";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookiefile);
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
//curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
$result = curl_exec ($ch);

$result=str_replace(array(),array(),$result);

preg_match_all("/\d/",$result,$matches);

<?php
//获得当前的脚本网址
function get_php_url(){
        if(!empty($_SERVER["REQUEST_URI"])){
                $scriptName = $_SERVER["REQUEST_URI"];
                $nowurl = $scriptName;
        }else{
                $scriptName = $_SERVER["PHP_SELF"];
                if(empty($_SERVER["QUERY_STRING"])) $nowurl = $scriptName;
                else $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];
        }
        return $nowurl;
}
//把全角数字转为半角数字
function GetAlabNum($fnum){
        $nums = array("0","1","2","3","4","5","6","7","8","9");
        $fnums = "0123456789";
        for($i=0;$i<=9;$i++) $fnum = str_replace($nums[$i],$fnums[$i],$fnum);
        $fnum = ereg_replace("[^0-9\.]|^0{1,}","",$fnum);
        if($fnum=="") $fnum=0;
        return $fnum;
}
//去除HTML标记
function Text2Html($txt){
        $txt = str_replace(" "," ",$txt);
        $txt = str_replace("<","&lt;",$txt);
        $txt = str_replace(">","&gt;",$txt);
        $txt = preg_replace("/[\r\n]{1,}/isU","<br/>\r\n",$txt);
        return $txt;
}

查看更多...

Tags: php

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 2424

 广告位

↑返回顶部↑