各位同学,以下代码我是从网上找的:
<?php
//$body = "http://img3.douban.com/lpic/s4422081.jpg";
$body = "<img src='http://img3.douban.com/lpic/s4422081.jpg' />";
$img_array = array();
preg_match_all("/(src|SRC)=[\"|'| ]{0,}(http:\/\/(.*)\.(gif|jpg|jpeg|bmp|png))/isU",$body,$img_array);
$img_array = array_unique($img_array[2]);
set_time_limit(0);
$imgUrl = "images/".strftime("%Y%m%d",time());
$imgPath = $imgUrl;
$milliSecond = strftime("%H%M%S",time());
if(!is_dir($imgPath)) @mkdir($imgPath,0777);
foreach($img_array as $key =>$value)
{
$value = trim($value);
$get_file = @file_get_contents($value);
$rndFileName = $imgPath."/".$milliSecond.$key.".".substr($value,-3,3);
$fileurl = $imgUrl."/".$milliSecond.$key.".".substr($value,-3,3);
if($get_file)
{
$fp = @fopen($rndFileName,"w");
@fwrite($fp,$get_file);
@fclose($fp);
}
$body = ereg_replace($value,$fileurl,$body);
}
echo $body;
?>现在的情况是: $body = "<img src='http://img3.douban.com/lpic/s4422081.jpg' />"; 这样子的条件时,会下载,但是改成这样子的话: //$body = "http://img3.douban.com/lpic/s4422081.jpg"; 就不知道如何精简程序和改正则了,还望各位同学指教一下,谢谢
<?php
//$body = "http://img3.douban.com/lpic/s4422081.jpg";
$body = "<img src='http://img3.douban.com/lpic/s4422081.jpg' />";
$img_array = array();
preg_match_all("/(src|SRC)=[\"|'| ]{0,}(http:\/\/(.*)\.(gif|jpg|jpeg|bmp|png))/isU",$body,$img_array);
$img_array = array_unique($img_array[2]);
set_time_limit(0);
$imgUrl = "images/".strftime("%Y%m%d",time());
$imgPath = $imgUrl;
$milliSecond = strftime("%H%M%S",time());
if(!is_dir($imgPath)) @mkdir($imgPath,0777);
foreach($img_array as $key =>$value)
{
$value = trim($value);
$get_file = @file_get_contents($value);
$rndFileName = $imgPath."/".$milliSecond.$key.".".substr($value,-3,3);
$fileurl = $imgUrl."/".$milliSecond.$key.".".substr($value,-3,3);
if($get_file)
{
$fp = @fopen($rndFileName,"w");
@fwrite($fp,$get_file);
@fclose($fp);
}
$body = ereg_replace($value,$fileurl,$body);
}
echo $body;
?>现在的情况是: $body = "<img src='http://img3.douban.com/lpic/s4422081.jpg' />"; 这样子的条件时,会下载,但是改成这样子的话: //$body = "http://img3.douban.com/lpic/s4422081.jpg"; 就不知道如何精简程序和改正则了,还望各位同学指教一下,谢谢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货