php正则匹配文章中的远程图片地址并下载图片至本地,
使用php的正则表达式来实现:
$content = '这里是文章内容,这里插入一张图片测试 <img src="/static/imghwm/default1.png" data-src="XXXXXXXXXXXXXXXXXXXX" class="lazy" alt="php正则匹配文章中的远程图片地址并下载图片至本地," >'; $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);
上面就将远程图片给匹配出来了,我们需要将其保持到本地。这里需要注意两点:
1.图片保存路径(图片存储目录)
2.实际访问图片地址
下面是完整实例:(你可以保存到本地服务器修改相应地方进行测试)
php保存远程图片到本地,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 = '这里是文章内容,这里插入一张图片测试 <img src="/static/imghwm/default1.png" data-src="XXXXXXXXXXXXXXXXXXXX" class="lazy" alt="php正则匹配文章中的远程图片地址并下载图片至本地," >'; $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; ?>
以上就是php正则匹配文章中的远程图片地址并下载图片至本地的实现技巧,希望对大家的学习有所帮助。

Outils d'IA chauds

Undresser.AI Undress
Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover
Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool
Images de déshabillage gratuites

Clothoff.io
Dissolvant de vêtements AI

AI Hentai Generator
Générez AI Hentai gratuitement.

Article chaud

Outils chauds

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Dreamweaver Mac
Outils de développement Web visuel

Télécharger la version Mac de l'éditeur Atom
L'éditeur open source le plus populaire

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

MinGW - GNU minimaliste pour Windows
Ce projet est en cours de migration vers osdn.net/projects/mingw, vous pouvez continuer à nous suivre là-bas. MinGW : un port Windows natif de GNU Compiler Collection (GCC), des bibliothèques d'importation et des fichiers d'en-tête librement distribuables pour la création d'applications Windows natives ; inclut des extensions du runtime MSVC pour prendre en charge la fonctionnalité C99. Tous les logiciels MinGW peuvent fonctionner sur les plates-formes Windows 64 bits.