PHP code example to obtain the first image of each article in the website,
1 2 3 4 5 6 7 8 9 10 11 12 | <?php
$temp =mt_rand(1,4);
$pattern = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/" ;
$content = $article ->Content;
preg_match_all( $pattern , $content , $matchContent );
if (isset( $matchContent [1][0])){
$temp = $matchContent [1][0];
} else {
$temp = "images/random/$temp.jpg" ;
}
?>
|
Copy after login
The above code calls the first image of the article by default. When the article has no image, it randomly calls the 1.jpg, 2.jpg, 3.jpg, and 4.jpg images under the theme style/images/random/. If you don’t want to call a random picture, you can modify it:
1 2 3 4 5 6 7 8 9 10 11 | <?php
$pattern = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/" ;
$content = $article ->Content;
preg_match_all( $pattern , $content , $matchContent );
if (isset( $matchContent [1][0])){
$temp = $matchContent [1][0];
} else {
$temp = "./images/no-image.jpg" ;
}
?>
|
Copy after login
Call the first image of the article. If the article has no image, call the default image no-image.jpg
http://www.bkjia.com/PHPjc/1133119.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1133119.htmlTechArticlePHP code example to get the first picture of each article in the website, php $temp=mt_rand(1,4 ); $pattern="/[img|IMG].*src=['|"](.*(:[.gif|.jpg|.png]))['|"].*[/]/" ; $content = $arti...