php 采集常用函数_PHP 采集常用函数整理

/*

$content = file_get_contents($url);

$content = iconv("GBK","UTF-8",$content);

print_r($match);

*/

$url = "http://www.phpchina.com/";

$body = fopen_url($url);

preg_match_all('|

(.*?)
|is',$body,$match);

preg_match_all('|]*href=/"([^/"]+)/"/s*title="([^/"]+)"[^<>]*>([^<>]+)/a>|i', $match[1][0], $matches);

print_r($matches); //或match_links($match[1][0])

/**

*获取远程文件内容

*@param $url 文件http地址

*/

function fopen_url($url)

{

if (function_exists('file_get_contents')) {

$file_content = @file_get_contents($url);

} elseif (ini_get('allow_url_fopen') && ($file = @fopen($url, 'rb'))){

$i = 0;

while (!feof($file) && $i++ < 1000) {

$file_content .= strtolower(fread($file, 4096));

}

fclose($file);

} elseif (function_exists('curl_init')) {

$curl_handle = curl_init();

curl_setopt($curl_handle, CURLOPT_URL, $url);

curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2);

curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1);

curl_setopt($curl_handle, CURLOPT_FAILONERROR,1);

curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Trackback Spam Check'); //引用垃圾邮件检查

$file_content = curl_exec($curl_handle);

curl_close($curl_handle);

} else {

$file_content = '';

}

return $file_content;

}

/**

* 提取链接

*Array

(

[link] => Array

(

[0] => http://www.phpchina.com/?action-viewnews-itemid-37454

[1] => http://www.phpchina.com/?action-viewnews-itemid-37465

)

[content] => Array

(

[0] => 微软最顶级平台技术会议PDC10隆重

[1] => 解读微软PDC10要点 看云到端的战略...

)

[all] => Array

(

[0] => 微软最顶级平台技术会议PDC10隆重 

[1] => ]+))[^>]*>?(.*?)'isx",$document,$links);

while(list($key,$val) = each($links[2])) {

if(!empty($val))

$match['link'][] = $val;

}

while(list($key,$val) = each($links[3])) {

if(!empty($val))

$match['link'][] = $val;

}

while(list($key,$val) = each($links[4])) {

if(!empty($val))

$match['content'][] = $val;

}

while(list($key,$val) = each($links[0])) {

if(!empty($val))

$match['all'][] = $val;

}

return $match;

}

// ####################### 获取文件流并转换成字符串 #######################

function openfile($url)

{

if(file($url)){

$str = file($url);

$count = count($str);

for ($i=0;$i

$file .= $str[$i];

}

return $file;

} else { die("文件打开失败!"); }

}

// ####################### 切分字符串 #######################

function cut($start,$end,$file){

$content=explode($start,$file);

$content=explode($end,$content[1]);

return $content[0];

}

// ####################### 清除垃圾代码 #######################

function del($start,$end,$content){

$del=cut($start,$end,$content);

$content=str_replace($del,"",$content);

$content=str_replace($start.$end,"",$content);

return $content;

}

// ####################### 分析域名 #######################

function getname($url)

{

$referer = preg_replace("/https?:([^//]+).*/i", "//1", $url);

$referer = str_replace("www.", "", $referer);

return $referer;

}

// ####################### 清除HTML代码table #######################

function clstable($content)

{

$clscontent= preg_replace("/

return $clscontent;

}

// ####################### 清除HTML代码script #######################

function clsscript($content)

{

$clscontent= preg_replace("/

]*?>.*?/script>/si", "", $content);

return $clscontent;

}

// ####################### 清除HTML代码div #######################

function clsdiv($content)

{

$clscontent= preg_replace("/

]*?>.*?/div>/si", "", $content);

return $clscontent;

}

// ####################### 清除HTML代码iframe #######################

function clsifr($content)

{

$clscontent= preg_replace("/]*?>.*?/IFRAME>/si", "", $content);

return $clscontent;

}

// ####################### 清除HTML代码tr,td #######################

function clstrtd($content)

{

$clscontent= preg_replace("/

]*?>.*?/td>/si", "", $content);

$clscontent= preg_replace("/

]*?>.*?/tr>/si", "", $clscontent);

$clscontent= preg_replace("/

]*?>/si","",$clscontent);

$clscontent= preg_replace("/

]*?>/si","",$clscontent);

$clscontent= preg_replace("//tr>/si","",$clscontent);

$clscontent= preg_replace("//td>/si","",$clscontent);

return $clscontent;

}

// ####################### 清除HTML代码超链接 #######################

function clsa($content)

{

$clscontent= preg_replace("/]*?>.*?/a>/si", "", $content);

return $clscontent;

}

// ####################### 彻底清除所有HTML代码#######################

function clearhtml($content)

{

$search = array ("'

// -->'si", // 去掉 javascript

"']*?>'si", // 去掉 HTML 标记

"'([/r/n])[/s]+'", // 去掉空白字符

"'&(quot|#34);'i", // 替换 HTML 实体

"'&(amp|#38);'i",

"'&(lt|#60);'i",

"'&(gt|#62);'i",

"'&(nbsp|#160);'i",

"'&(iexcl|#161);'i",

"'&(cent|#162);'i",

"'&(pound|#163);'i",

"'&(copy|#169);'i",

"'(/d+);'e"); // 作为 PHP 代码运行

$replace = array ("",

"",

"//1",

"/"",

"&",

"

">",

" ",

chr(161),

chr(162),

chr(163),

chr(169),

"chr(//1)");

$text = preg_replace ($search, $replace, $content);

return $text;

}

// ####################### 写入缓存文件 #######################

function writetocache($cachedir,$cachename, $cachedata = '') {

$cachedir = './'.$cachedir.'/';

$cachefile = $cachedir.$cachename.'.php';

if(!is_dir($cachedir)) {

@mkdir($cachedir, 0777);

}

if(!is_dir($cachedir)) {

@mkdir($cachedir, 0777);

}

if(@$fp = fopen($cachefile, 'wb')) {

@fwrite($fp, $cachedata);

@fclose($fp);

@chmod($cachefile, 0777);

} else {

echo 'Can not write to cache files, please check directory ./cache/ .';

exit;

}

}

// ####################### 获取文件里的html链接 #######################

function geturl($re,$ufile,$rep1,$rep2){

preg_match_all ($re,$ufile,$out, PREG_PATTERN_ORDER);

$result=count($out[1]);

$i=0;

while($i

{

$outs[$i]=str_replace($rep1,$rep2,$out[1][$i]);

$i++;

}

//合并相同的链接并重新索引...

$reout=array();

$reout=resetar($outs);

return $reout;

}

// ####################### 切分文件流 #######################

function cut($start,$end,$file){

$content=explode($start,$file);

$content=explode($end,$content[1]);

return $content[0];

}

// ####################### 清除垃圾代码 #######################

function del($start,$end,$content){

$del=cut($start,$end,$content);

$content=str_replace($del,"",$content);

$content=str_replace($start.$end,"",$content);

return $content;

}

// ####################### 清除数组里的重复值并重新索引数组 #######################

function resetar($outs){

$reout=array();

$reouts=array();

$reout=array_unique($outs);

foreach($reout as $key=>$value){

array_push($reouts,$value);

}

return $reouts;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP采集Lazada是一种使用PHP编程语言来获取Lazada网站上的数据的方法。Lazada是一个知名的电子商务平台,它提供各种产品的在线购物服务。使用PHP采集Lazada的目的是为了获取Lazada上的商品信息,如商品名称、价格、描述、评价等。 PHP采集Lazada的步骤如下: 1. 首先,需要使用PHP的curl函数建立一个与Lazada网站的连接。curl函数可以模拟浏览器,发送HTTP请求并接收响应。 2. 通过curl函数发送HTTP请求获取Lazada网站的HTML源码。可以通过设置curl选项来指定请求的URL和其他必要参数。 3. 使用PHP的正则表达式函数或DOM解析器来解析HTML源码,提取感兴趣的数据。例如,可以使用正则表达式来匹配商品名称和价格的标签,并从中提取相关信息。 4. 对获取的数据进行处理和存储。可以将数据存储到数据库、Excel文档或其他格式中,以便后续使用。 5. 应该添加错误处理机制,以处理可能出现的错误,如网络连接故障、HTML解析错误等。 6. 根据需要,可以添加其他功能,如自动化采集、分页浏览等。 7. 需要定时运行采集脚本,以保持数据的最新性。 总之,PHP采集Lazada是一种利用PHP编程语言来从Lazada网站上获取数据的方法。通过建立与Lazada的连接,发送HTTP请求并解析响应,我们可以获取Lazada上的商品信息,并对其进行处理和存储。这种方法可以帮助我们快速获取所需的数据,并用于后续分析、使用或展示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值