php 擷取文章內容的第一張圖片執行個體

來源:互聯網
上載者:User

首先看一個函數:

 代碼如下 複製代碼

function getpic($str_img){
    preg_match_all("/<img.*>/isU",$str,$ereg);//Regex把圖片的整個都擷取出來了
    $img=$ereg[0][0];//圖片
    $p="#src=('|")(.*)('|")#isU";//Regex
    preg_match_all ($p, $img, $img1);
    $img_path =$img1[2][0];//擷取第一張圖片路徑 
    return $img_path;
    }
//假如資料庫已開啟,用$nr擷取資料庫中的新聞內容
$nr=$row_news["nr"];
$aa=getpic($nr_a);
if(!$aa){$aa="images/nopic.jpg";} //如果新聞中不存在圖片,用預設的nopic.jpg替換

再看一個函數相對比較複雜了

在做項目的時候,頁面的設計有時會留有文章特色映像的位置,可是有時,這篇文章反而沒有上傳圖片,則在頁面中顯示的時候則是沒有圖片,樣式上很難看,如果單純是沒有上傳圖片選用預設圖片的時候,有時會引起一些誤解;則在考慮是不是先對這個文章圖片的問題細化處理:先判斷是否有上傳的圖片,如果有則顯示上傳的圖片,沒有則判斷內容中是否有圖片,有則選取第一張圖片作為此處的精選圖片,如果連內容中也沒有圖片的話,則在此處顯示預設圖片;

以下是關於選取文章中第一張圖片的代碼:

 代碼如下 複製代碼

$obj=M("News");
$info=$obj->where('id=1')->find();
//方法1*********

            $soContent = $info['content'];
            $soImages = '~<img [^>]* />~';
            preg_match_all( $soImages, $soContent, $thePics );
            $allPics = count($thePics[0]);
            preg_match('/<img.+src="?(.+.(jpg|gif|bmp|bnp|png))"?.+>/i',$thePics[0][0],$match);
            dump($thePics);
            if( $allPics> 0 ){
                echo "<img src='".$match[1]."' title='".$match[1]."'>";//擷取的圖片名稱
            }
            else {
                echo "沒有圖片";
            }

//**************

            $soContent = $info['content'];
            $soImages = '~<img [^>]* />~';
            preg_match_all( $soImages, $soContent, $thePics );
            $allPics = count($thePics[0]);
            dump($thePics);
            if( $allPics> 0 ){
                echo $thePics[0][0]; //擷取的整個Img屬性
            } else {
                echo "沒有圖片";
            }

//**************

            $soImages = '~<img [^>]* />~';
            $str=$info['content'];
            preg_match_all($soImages,$str,$ereg);//Regex把圖片的整個都擷取出來了
            $img=$ereg[0][0];//圖片
            $p="#src=('|")(.*)('|")#isU";//Regex
            preg_match_all ($p, $img, $img1);
               $img_path =$img1[2][0];//擷取第一張圖片路徑
            if(!$img_path){
                $img_path="images/nopic.jpg";
            } //如果新聞中不存在圖片,用預設的nopic.jpg替換 */
            echo $img_path;

//*************88

            $str=$info['content'];
            preg_match_all("/<img.*>/isU",$str,$ereg);//Regex把圖片的整個都擷取出來了
            $img=$ereg[0][0];//圖片
            $p="#src=('|")(.*)('|")#isU";//Regex
            preg_match_all ($p, $img, $img1);
               $img_path =$img1[2][0];//擷取第一張圖片路徑
            if(!$img_path){
                $img_path="images/nopic.jpg";
            } //如果新聞中不存在圖片,用預設的nopic.jpg替換 */
            echo $img_path;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.