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...