Home >Backend Development >PHP Tutorial >php Get the first image example of article content_PHP tutorial

php Get the first image example of article content_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:43:42986browse

To use php to get the first picture among all the pictures in the article, we only need a simple regular expression. Let me share two examples with you.

First look at a function:

The code is as follows Copy code
 代码如下 复制代码

function getpic($str_img){
    preg_match_all("//isU",$str,$ereg);//正则表达式把图片的整个都获取出来了
    $img=$ereg[0][0];//图片
    $p="#src=('|")(.*)('|")#isU";//正则表达式
    preg_match_all ($p, $img, $img1);
    $img_path =$img1[2][0];//获取第一张图片路径 
    return $img_path;
    }
//假如数据库已打开,用$nr获取数据库中的新闻内容
$nr=$row_news["nr"];
$aa=getpic($nr_a);
if(!$aa){$aa="images/nopic.jpg";} //如果新闻中不存在图片,用默认的nopic.jpg替换

function getpic($str_img){
Preg_match_all("//isU",$str,$ereg);//Regular expression obtains the entire image
$img=$ereg[0][0];//Picture
$p="#src=('|")(.*)('|")#isU";//regular expression
Preg_match_all ($p, $img, $img1);
$img_path =$img1[2][0];//Get the first image path
Return $img_path;
}
//If the database is open, use $nr to get the news content in the database
$nr=$row_news["nr"];
$aa=getpic($nr_a);
if(!$aa){$aa="images/nopic.jpg";} //If there is no picture in the news, replace it with the default nopic.jpg

Look at another function that is relatively complicated

When working on a project, the design of the page sometimes leaves a place for the image featured in the article. However, sometimes, the article does not upload the image, so when it is displayed on the page, there is no image, and the style is ugly. If there is no uploaded image and the default image is selected, sometimes it will cause some misunderstandings; then consider whether to refine the issue of images in this article first: first determine whether there are uploaded images, and if so, display the uploaded images. If there is no picture, it will be judged whether there is a picture in the content. If there is, the first picture will be selected as the featured picture here. If there is no picture in the content, the default picture will be displayed here;

The following is the code for selecting the first image in the article:

The code is as follows Copy code

$obj=M("News");
$info=$obj->where('id=1')->find();
//Method 1************

$soContent = $info['content'];
$soImages = '~]* />~';
Preg_match_all( $soImages, $soContent, $thePics );
                $allPics = count($thePics[0]);
preg_match('//i',$thePics[0][0],$match );
               dump($thePics);
                    if( $allPics> 0){
                                                                                                                                                                                                                   echo "";//The name of the obtained image
            }
             else {
echo "No picture";
            }

//******************

$soContent = $info['content'];
                  $soImages = '~]* />~';
Preg_match_all( $soImages, $soContent, $thePics );
                $allPics = count($thePics[0]);
               dump($thePics);
If( $allPics> 0 ){
echo $thePics[0][0]; //The entire Img attribute obtained
              } else {
echo "No picture";
            }

//******************

                 $soImages = '~]* />~';
                $str=$info['content'];
                preg_match_all($soImages,$str,$ereg);//Regular expression obtains the entire image
               $img=$ereg[0][0];//Picture
                 $p="#src=('|")(.*)('|")#isU";//Regular expression
                preg_match_all ($p, $img, $img1);
                   $img_path =$img1[2][0];//Get the first picture path
                if(!$img_path){
                    $img_path="images/nopic.jpg";
                           } //If there is no picture in the news, replace it with the default nopic.jpg */
                  echo $img_path;

//***************88

$str=$info['content'];
                 preg_match_all("//isU",$str,$ereg);//Regular expression captures the entire image
               $img=$ereg[0][0];//Picture
                  $p="#src=('|")(.*)('|")#isU";//Regular expression
                preg_match_all ($p, $img, $img1);
                   $img_path =$img1[2][0];//Get the first picture path
                if(!$img_path){
                    $img_path="images/nopic.jpg";
                          } //If there is no picture in the news, replace it with the default nopic.jpg*/
                  echo $img_path;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633155.htmlTechArticleTo use php to get the first picture among all the pictures in the article, we only need a simple regular expression. It has been implemented. Let me share two examples with you. Let’s look at one first...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn