本文主要和大家介紹了php獲取文章內容第一張圖片的方法,涉及php針對圖片的正則匹配操作技巧,需要的朋友可以參考下,希望能幫助到大家。
<?php $temp=mt_rand(1,4); $pattern="/<[img|IMG].*?src="http://m.argcandargv.com/skin/default/image/lazy.gif" class="lazy" original="http://m.argcandargv.com/skin/default/image/nopic.gif" = $article->Content; //文章內容 preg_match_all($pattern,$content,$matchContent); if(isset($matchContent[1][0])){ $temp=$matchContent[1][0]; }else{ $temp="images/random/$temp.jpg";//需要在相應位置放置4張jpg的文件,名稱為1,2,3,4 } ?>
以上代碼默認調用文章首張圖片,當文章沒有圖片的時候,隨機調用主題style/images/random/下的1.jpg、2.jpg、3.jpg、4.jpg圖片。如果不想調用隨機圖片,可以修改一下:
<?php $pattern="/<[img|IMG].*?src="http://m.argcandargv.com/skin/default/image/lazy.gif" class="lazy" original="http://m.argcandargv.com/skin/default/image/nopic.gif" = $article->Content; //文章內容 preg_match_all($pattern,$content,$matchContent); if(isset($matchContent[1][0])){ $temp=$matchContent[1][0]; }else{ $temp="./images/no-image.jpg";//在相應位置放置一張命名為no-image的jpg圖片 } ?>
調用文章首張圖片,如果文章沒有圖片就調用默認圖片no-image.jpg
相關推薦:
php 獲取圖片信息的方法實例詳解
分享一個JS實現獲取圖片大小和預覽的方法
PHP獲取圖片詳細信息方法
以上就是php如何獲取文章內容第一張圖片的詳細內容,更多請關注php中文網其它相關文章!