Php: getting the first image of the article content _ PHP Tutorial

Source: Internet
Author: User
Tags ereg
Php retrieves the first image of the article. To use php to obtain the first image in all the pictures in the article, we only need a simple regular expression. the following small series will share with you two examples. First, let's take a look at the first image in all the pictures in the article using php. we only need a simple regular expression to implement it. let's share two examples with you.

First, let's look at a function:

The code is as follows:

Function getpic ($ str_img ){
Preg_match_all ("// isU", $ str, $ ereg); // the regular expression retrieves the entire image.
$ Img = $ ereg [0] [0]; // Picture
$ P = "# src = ('|") (. *) (' | ") # isU"; // regular expression
Preg_match_all ($ p, $ img, $ img1 );
$ Img_path = $ img1 [2] [0]; // obtain the first image path
Return $ img_path;
}
// If the database has been opened, use $ nr to get news from the database
$ Nr = $ row_news ["nr"];
$ Aa = getpic ($ nr_a );
If (! $ Aa) {$ aa = "images/nopic.jpg";} // if no image exists in the news, replace it with your nopic.jpg

It is relatively complicated to look at a function.

During Project creation, the page design sometimes leaves the position of the special image of the article. However, sometimes, this article does not upload images, so there is no image displayed on the page, it is difficult to see the style. if the default image is not used for image uploading, some misunderstandings may occur. consider whether to refine the problem of the image in this article: first, determine whether an uploaded image exists. If yes, the uploaded image is displayed. If no, determine whether there is an image in the content. If yes, select the first image as the featured image, if no image is displayed in the connected content, the default image is displayed here;

The following code selects the first image in the document:

The code is as follows:

$ 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 image name.
}
Else {
Echo "no image ";
}

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

$ SoContent = $ info ['content'];
$ SoImages = '~] */> ~ ';
Preg_match_all ($ soImages, $ soContent, $ thePics );
$ AllPics = count ($ thePics [0]);
Dump ($ thePics );
If ($ allPics> 0 ){
Echo $ thePics [0] [0]; // Obtain the entire Img attribute
} Else {
Echo "no image ";
}

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

$ SoImages = '~] */> ~ ';
$ Str = $ info ['content'];
Preg_match_all ($ soImages, $ str, $ ereg); // the regular expression retrieves the entire image.
$ Img = $ ereg [0] [0]; // Picture
$ P = "# src = ('|") (. *) (' | ") # isU"; // regular expression
Preg_match_all ($ p, $ img, $ img1 );
$ Img_path = $ img1 [2] [0]; // obtain the first image path
If (! $ Img_path ){
$ Img_path = "images/nopic.jpg ";
} // If no image exists in the news, replace it with your nopic.jpg */
Echo $ img_path;

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

$ Str = $ info ['content'];
Preg_match_all ("// isU", $ str, $ ereg); // the regular expression retrieves the entire image.
$ Img = $ ereg [0] [0]; // Picture
$ P = "# src = ('|") (. *) (' | ") # isU"; // regular expression
Preg_match_all ($ p, $ img, $ img1 );
$ Img_path = $ img1 [2] [0]; // obtain the first image path
If (! $ Img_path ){
$ Img_path = "images/nopic.jpg ";
} // If no image exists in the news, replace it with your nopic.jpg */
Echo $ img_path;

Bytes. First look at...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.