function code for remote pictures in PHP via regular expression download _php tips

Source: Internet
Author: User
Tags curl
This procedure is actually part of the "Thief procedure". This part of the program just download the remote picture of the paragraph, the program is written relatively simple, most of the comments are made.
Copy Code code as follows:

if (Preg_match_all ("/http://[^")]+[.jpg|. Gif|. Jpeg|. Png]+/ui ", Stripcslashes ($content), $aliurl)) {
$i = 0; Multiple files + +
while (the list ($key, $v) = each ($aliurl [0])) {
echo $v. " <br/> ";
$filetype = PathInfo ($v, pathinfo_extension); Get suffix Name
$FF = @file_get_contents ($v); Get the contents of the 2 file
if (!stripos ($v, "jb51.net")) {//Judge whether it is a picture under your own website
if (!empty ($FF)) {//Get to file do the following
$dir = "upload/". Date ("Ymd"). " /";//Specify a new storage path
if (!file_exists ($dir)) {//To determine whether the directory exists
@mkdir ($dir, 511,true); Create multilevel directories, 511 to decimal is 777 with executable permission
}
$NFN = $dir. Date ("Ymdhis"). $i. "." $filetype; Build a new name for the file
$NF = @fopen ($nfn, "w"); Create a file
Fwrite ($NF, $FF); Write to File
Fclose ($NF); Close File
$i + +; Multi-file + +
echo " ";
$content = Str_replace ($v, $NFN, $content);//Replace parameters in content
}else{//can't get a picture and replace it with a default picture
$content = Str_replace ($v, "/upload/201204/20120417213810742.gif", $content);//Replace parameters in content
}
}
}
}

PHP Downloads pictures to local implementation code through regular expressions
Copy Code code as follows:

<?php
/*
Author:ssh_kobe
date:20110602
Shortage: If the picture path in the Web page is not an absolute path, you cannot crawl
*/
Set_time_limit (0);//crawl is not limited by time

$URL = ' http://pp.baidu.com/';//any URL

Get_pic ($URL);

function Get_pic ($pic _url) {
Get Picture binary stream
$data =curlget ($pic _url);
/* Use regular expression to get picture link * *
$pattern _src = '/<[img| Img].*?src=[\ ' |\ '] (. *?: [\.gif|\.jpg])) [\ ' |\ '].*? [\/]?>/';
$num = Preg_match_all ($pattern _src, $data, $match _src);
$arr _src= $match _src[1];//get an array of pictures
Get_name ($arr _src);

echo "<br>finished!!!";
return 0;
}

/* Get the picture type, and save it to the same directory as the file * *
function get_name ($pic _arr)
{
Picture type
$pattern _type = '/( jpg|bmp|jpeg|gif|png))/';

foreach ($pic _arr as $pic _item) {//loop out the address of each picture
$num = Preg_match_all ($pattern _type, $pic _item, $match _type);
$pic _name = Get_unique (). $match a microsecond timestamp name when _type[1][0];//changed
Save a picture as a stream
$write _fd = @fopen ($pic _name, "WB");
@fwrite ($write _fd, Curlget ($pic _item));
@fclose ($write _fd);
echo "[ok]..!";
}
return 0;
}

Get unique IDs through microsecond time
function Get_unique () {
List ($msec, $sec) = Explode ("", Microtime ());
Return $sec. Intval ($msec *1000000);
}

Crawl Web content
function Curlget ($url) {
$url =str_replace (' & ', ' & ', $url);
$curl = Curl_init ();
curl_setopt ($curl, Curlopt_url, $url);
curl_setopt ($curl, Curlopt_header, false);

curl_setopt ($curl, Curlopt_referer, $url);
curl_setopt ($curl, Curlopt_useragent, "mozilla/4.0" (compatible; MSIE 6.0; seaport/1.2; Windows NT 5.1; SV1; infopath.2) ");
curl_setopt ($curl, Curlopt_cookiejar, ' cookie.txt ');
curl_setopt ($curl, Curlopt_cookiefile, ' cookie.txt ');
curl_setopt ($curl, Curlopt_returntransfer, 1);
curl_setopt ($curl, curlopt_followlocation, 0);
$values = curl_exec ($curl);
Curl_close ($curl);
return $values;
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.