search
Homephp教程php手册php把采集内容中图片地址下载并替换成本地地址,

php把采集内容中图片地址下载并替换成本地地址,

Jun 13, 2016 am 09:23 AM
phpdownloadcontentpictureaddressBundleReplace withlocalcollection

php把采集内容中图片地址下载并替换成本地地址,

把字符串中地址全部获取到一个数组我们利用preg_match_all函数

代码如下 复制代码
$str='

';
$pattern="//";
preg_match_all($pattern,$str,$match);print_r($match);
?>

结果

Array
(
[0] => Array
(
[0] =>
)
[1] => Array
(
[0] => upfiles/2009/07/1246430143_1.jpg
)
)

有了上面核心函数下面就好做了

 

代码如下 复制代码
/**
* 获取替换文章中的图片路径
* @param string $xstr 内容
* @param string $keyword 创建照片的文件名
* @param string $oriweb 网址
* @return string
*
*/
function replaceimg($xstr,$keyword, $oriweb){

//保存路径
$d = date('Ymd', time());
$dirslsitss = '/var/www/weblist/uploads/'.$keyword.'/'.$d;//分类是否存在
if(www.111cn.net)(!is_dir($dirslsitss)) {
@mkdir($dirslsitss, 0777);
}

//匹配图片的src
preg_match_all('#php把采集内容中图片地址下载并替换成本地地址,]*>#i', $xstr, $match);

foreach($match[1] as $imgurl){

$imgurl = $imgurl;

if(is_int(strpos($imgurl, 'http'))){
$arcurl = $imgurl;
} else {
$arcurl = $oriweb.$imgurl;
}
$img=file_get_contents($arcurl);


if(!empty($img)) {

//保存图片到服务器
$fileimgname = time()."-".rand(1000,9999).".jpg";
$filecachs=$dirslsitss."/".$fileimgname;
$fanhuistr = file_put_contents( $filecachs, $img );
$saveimgfile = "/uploads/$keyword"."/".$d."/".$fileimgname;


$xstr=str_replace($imgurl,$saveimgfile,$xstr);
}
}
return $xstr;
}

from:http://www.111cn.net/phper/php-cy/48607.htm

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version