Home > Article > Backend Development > Detailed explanation of the method of obtaining the first picture of the article content in PHP
This article mainly introduces the method of php to obtain the first picture of the article content, involving php's regular matching operation skills for pictures. Friends who need it can refer to it
The details are as follows:
<?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";//需要在相应位置放置4张jpg的文件,名称为1,2,3,4 } ?>
The above code calls the first picture of the article by default. When the article has no picture, it randomly calls 1.jpg, 2.jpg, and 3 under the theme style/images/random/ .jpg, 4.jpg pictures. If you don’t want to call a random picture, you can modify it:
<?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";//在相应位置放置一张命名为no-image的jpg图片 } ?>
Call the first picture of the article. If the article has no picture, call the default picture no-image.jpg
Related recommendations:
PHP implementationHow to get the main color of the image
PHPGet the imageWidth and height, size, image type, img attribute for layout
php Detailed example of method to obtain imageinformation
The above is the detailed content of Detailed explanation of the method of obtaining the first picture of the article content in PHP. For more information, please follow other related articles on the PHP Chinese website!