PHP to download and replace the cost address of the image in the collected content, _php tutorial

Source: Internet
Author: User

PHP downloads and replaces the address of the cost of the image in the collected content.


Get all the addresses in the string to an array we use the Preg_match_all function

Code to copy code as follows
$str = '

';
$pattern = "/<[img| Img].*?src=[' | "] (.*? (?: [. gif|. JPG])) [' | ']. *? [/]?>/];
Preg_match_all ($pattern, $str, $match);p Rint_r ($match);
?>

Results

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

With the core function above, it's good to do it.

Code to copy code as follows
/**
* Get the picture path in the replacement article
* @param string $xstr content
* @param string $keyword Create a file name for the photo
* @param string $oriweb URL
* @return String
*
*/
function replaceimg ($xstr, $keyword, $oriweb) {

Save path
$d = Date (' Ymd ', Time ());
$DIRSLSITSS = '/var/www/weblist/uploads/'. $keyword. ' /'. $d;//whether the classification exists
if (www.111cn.net) (!is_dir ($DIRSLSITSS)) {
@mkdir ($DIRSLSITSS, 0777);
}

Matching images of SRC
Preg_match_all (' #]*> #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)) {

Save picture to Server
$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




http://www.bkjia.com/PHPjc/898821.html www.bkjia.com true http://www.bkjia.com/PHPjc/898821.html techarticle PHP Downloads the image address in the collection and replaces the cost address to get all the addresses in the string to an array we use the Preg_match_all function code to copy the code as follows? PHP $ ...

  • Related Article

    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.