PHP Get the image address in CSS file and download to local method, _php tutorial

Source: Internet
Author: User
Tags php programming

PHP Gets the image address in the CSS file and downloads it to the local method,


This example describes how PHP gets the image address in the CSS file and downloads it locally. Share to everyone for your reference.

The specific implementation code is as follows:

The

copy Code code is as follows:
/**
* get picture address in CSS, and save to local
*/
Class Getincssimage
{
/**
* Picture warranty Save the
* @param $cssUrl The URL address of the CSS
* @param the directory where the picture is saved
* @return void
*/
static public function sav Eimage ($CSSURL, $dir)
{
$content = file_get_contents ($CSSURL);
$patterns = '/images (. *). (jpg|gif|png)/'; The regular transforms the
Preg_match_all ($patterns, $content, $matches) according to different addresses;
$imagesUrls = $matches [0];
if (!is_dir ($dir))
mkdir (DirName (__file__). '/'. $dir, 0777);
foreach ($imagesUrls as $image)
{
Ob_start ();
$imageUrl = "http://www.xxxx.com/". $image;//This address fills in the address you want to crawl
ReadFile ($IMAGEURL);
$img = ob_get_contents ();
Ob_end_clean ();
$size = strlen ($img);
$localImage = $dir. STRCHR ($image, '/');//Save to local image address
$fp = fopen ($localImage, ' a ');
Fwrite ($fp, $img);
Fclose ($FP);
}
}
}
$content = Getincssimage::saveimage ('/css/css.css ', ' image ');

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/920612.html www.bkjia.com true http://www.bkjia.com/PHPjc/920612.html techarticle PHP Gets the image address of the CSS file and download to the local method, this article describes the PHP get the image address in the CSS file and download to the local method. Share to everyone for your reference. ...

  • 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.