Home  >  Article  >  Backend Development  >  PHP regular match the remote image address in the article and download the image to the local_PHP tutorial

PHP regular match the remote image address in the article and download the image to the local_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:12:21899browse

Today I am doing a simple collection program that requires downloading the content of the other party's website, and then saving the pictures in the content on the local server. Now I will introduce to you my specific operation method. Downloading pictures mainly uses the file_get_contents function. The specific method as follows.


Here we use PHP regular expressions to achieve:

The code is as follows
 代码如下 复制代码
$content = '这里是文章内容,这里插入一张图片测试 ';
$content = stripslashes ( $content );
$img_array = array ();
// 匹配所有远程图片
preg_match_all ( "/(src|SRC)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|bmp|png))/isU", $content, $img_array );
// 匹配出来的不重复图片
$img_array = array_unique ( $img_array [2] );
print_r($img_array);
Copy code

$content = 'Here is the content of the article, insert a picture here to test';

$content = stripslashes ( $content );

$img_array = array ();

// Match all remotes Picture

preg_match_all ( "/(src|SRC)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|bmp|png))/isU", $content, $img_array );

// Matched unique images

$img_array = array_unique ( $img_array [2] );
 代码如下 复制代码





php保存远程图片到本地,php正则匹配文章中的图片地址


//保存文章中远程图片到本地
//作者:yanue;
// 文件保存目录路径(请更换为你自己的路径, 你可以echo一下)
$save_path = $_SERVER ['DOCUMENT_ROOT'] . 'swfupload/attached/';
// 文件保存目录URL
$save_url = '/swfupload/attached/';
$save_path = realpath ( $save_path ) . '/';
// 图片存储目录
$imgPath = $save_path . date ( "Ymd" );
$imgUrl = $save_url . date ( "Ymd" );

// 创建文件夹
if (! is_dir ( $imgPath )) {
 @mkdir ( $imgPath, 0777 );
}
$content = '这里是文章内容,这里插入一张图片测试 ';
$content = stripslashes ( $content );
$img_array = array ();
// 匹配所有远程图片
preg_match_all ( "/(src|SRC)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|bmp|png))/isU", $content, $img_array );
// 匹配出来的不重复图片
$img_array = array_unique ( $img_array [2] );
print_r($img_array);
// 时间无限制
set_time_limit ( 0 );
foreach ( $img_array as $key => $value ) {
 $value = trim ( $value );
 // 读取远程图片
 $get_file = @file_get_contents ( $value );
 // 保存到本地图片名称
 $imgname = date ( "YmdHis" ) . '_' . rand ( 10000, 99999 ) . "." . substr ( $value, - 3, 3 );
 // 保存到本地的实际文件地址(包含路径和名称)
 $fileName = $imgPath . '/' . $imgname;
 // 实际访问的地址
 $fileurl = $imgUrl . "/" . $imgname;
 // 文件写入
 if ($get_file) {
  $fp = @fopen ( $fileName, "w" );
  @fwrite ( $fp, $get_file );
  @fclose ( $fp );
 }
 // 替换原来的图片地址
 $content = ereg_replace ( $value, $fileurl, $content );
}
echo $content;
?>

print_r($img_array);
The remote image is matched above, we need to keep it locally. Two points need to be noted here: 1. Image saving path (image storage directory) 2. Actual access to the image address The following is a complete example: (You can save it to the local server and modify the corresponding place for testing)
The code is as follows Copy code
php saves the remote image to the local, php regular match in the article Image address//Save the remote image in the article to local location<🎜>//Author: yanue; <🎜>// File saving directory path (please change it to your own path, you can echo it)<🎜>$save_path = $_SERVER ['DOCUMENT_ROOT'] . 'swfupload/attached/';<🎜>// File Save directory URL<🎜>$save_url = '/swfupload/attached/';<🎜>$save_path = realpath ( $save_path ) . '/';<🎜>// Image storage directory<🎜>$imgPath = $save_path . date ( "Ymd" );<🎜>$imgUrl = $save_url . date ( "Ymd" );<🎜><🎜>// Create folder<🎜>if (! is_dir ( $imgPath )) {<🎜> @mkdir ( $imgPath, 0777 );<🎜>}<🎜>$content = 'Here is the article content, insert a picture here to test $> $Content = Stripslashes ($ Content); (); > // match all remote pictures preg_match_all ( "/(src|SRC)=["|'| ]{0,}(http://(.*).(gif|jpg|jpeg|bmp|png))/isU", $content, $img_array );// Matched unique images$img_array = array_unique ( $img_array [2] );print_r($img_array);// Unlimited timeset_time_limit ( 0 );foreach ( $img_array as $key => $value ) { $value = trim ( $value ); // Read remote image $get_file = @file_get_contents ( $value ); // Save to local image name $imgname = date ( "YmdHis" ) . '_' . rand ( 10000, 99999 ) . "." . substr ( $value, - 3, 3); // Actual file address saved to local (including path and name) $fileName = $imgPath . '/' . $imgname; // Actual accessed address $ fileurl = $imgUrl . "/" . $imgname; // File writing if ($get_file) { $fp = @fopen ( $fileName, "w" ); @ fwrite ( $fp, $get_file ); @fclose ( $fp ); } // Replace the original image address $content = ereg_replace ( $value, $fileurl, $content ) ;}echo $content;?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444581.htmlTechArticleToday I am doing a simple collection program that requires downloading the content of the other party’s website and then saving the images in the content locally. On the server, let me introduce to you my specific operation method...
Statement:
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