<address id="ousso"></address>
<form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
  1. php語言

    php采集文章中的圖片獲取替換到本地實例

    時間:2024-12-18 16:10:33 php語言 我要投稿
    • 相關推薦

    php采集文章中的圖片獲取替換到本地實例

      導語:PHP中如何把圖片替換到本地中,你知道這樣的程序要怎么寫嗎?以下的是對php采集文章中的圖片獲取替換到本地的實現代碼進行了詳細的分析介紹,有需要的朋友可以參考一下。

      代碼如下:

      /**

      * 獲取替換文章中的圖片路徑

      * @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(!is_dir($dirslsitss)) {

      @mkdir($dirslsitss, 0777);

      }

      //匹配圖片的src

      preg_match_all('#<img.*?src="([^"]*)"[^>]*>#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;

      }

    【php采集文章中的圖片獲取替換到本地實例】相關文章:

    php中fsockopen用法實例06-20

    php多個文件及圖片上傳實例詳解08-02

    php獲取新浪微博數據API的實例代碼08-06

    PHP中數組的分組排序實例11-14

    php中return的用法實例分析10-27

    php中獲取數組長度的方法10-13

    php中實現回刪功能實例10-03

    PHP中檢測ajax請求的代碼實例10-25

    php畫圖實例07-16

    <address id="ousso"></address>
    <form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
    1. 日日做夜狠狠爱欧美黑人