SoFunction
Updated on 2025-03-10

PHP obtains the code example of the first image of each article in the website

<?php 
$temp=mt_rand(1,4); 
$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/"; 
$content = $article->Content; //Article contentpreg_match_all($pattern,$content,$matchContent); 
if(isset($matchContent[1][0])){ 
  $temp=$matchContent[1][0]; 
}else{ 
  $temp="images/random/$";//You need to place 4 jpg files in the corresponding location, with names 1, 2, 3, 4} 
 
?> 

The above code calls the first image of the article by default. When there is no picture in the article, randomly call the , , , , , , . . If you don't want to call random images, you can modify them:

<?php 
$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/"; 
$content = $article->Content; //Article contentpreg_match_all($pattern,$content,$matchContent); 
if(isset($matchContent[1][0])){ 
  $temp=$matchContent[1][0]; 
}else{ 
  $temp="./images/";//Place a jpg image named no-image in the corresponding position} 
 
?> 

Call the first picture of the article. If the article has no pictures, call the default picture.