Content;//文章内容preg_m ; 更新日期:2025/2/13 华山资源网" />

当前位置:首页 >> 网络编程

PHP获取网站中各文章的第一张图片的代码示例

<"/<[img|IMG].*"](.*"].*"; 
$content = $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 
} 
 
"htmlcode">
<"/<[img|IMG].*"](.*"].*"; 
$content = $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