$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; |