Home Backend Development PHP Tutorial mysql regular expression PHP downloads images to local implementation code through regular expressions

mysql regular expression PHP downloads images to local implementation code through regular expressions

Jul 29, 2016 am 08:46 AM

Copy code The code is as follows:


<?php
/*
author: ssh_kobe
date: 20110602
shortage: If the image path in the web page is not an absolute path, it cannot be crawled
*/
set_time_limit (0); //Fetching is not limited by time
$URL='http://pp.baidu.com/';//Any URL
get_pic($URL);
function get_pic($pic_url) {
/ /Get the image binary stream
$data=CurlGet($pic_url);
/*Use regular expressions to get the image link*/
$pattern_src = '/<[img|IMG].*?src=['|"] (.*?(?:[.gif|.jpg]))['|"].*?[/]?>/';
$num = preg_match_all($pattern_src, $data, $match_src);
$arr_src=$match_src[1];//Get the image array
get_name($arr_src);
echo "<br>finished!!!";
return 0;
}
/*Get the image type and add it Save 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 to get the address of each picture
$num = preg_match_all($pattern_type, $pic_item, $match_type);
$pic_name = get_unique().$match_type[1][0];/ /Change the microsecond timestamp naming
//Save the picture in the form of a stream
$write_fd = @fopen($pic_name,"wb");
@fwrite($write_fd, CurlGet($pic_item));
@fclose( $write_fd);
echo "[OK]..!";
}
return 0;
}
//Get the unique ID through microsecond time
function get_unique(){
list($msec, $sec) = explode (" ",microtime());
return $sec.intval($msec*1000000);
}
//Capture 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);
curl_close($curl);
return $values;
}
?>

The above introduces the mysql regular expression PHP implementation code for downloading images to the local through regular expressions, including the content of mysql regular expressions. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles