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 look at a function:
| The Code is as follows: |
Copy code |
|
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: |
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 "}
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; |