Let's take a look at php to get the regular expressions of all images in the article.
The code is as follows: |
Copy code |
$ Body = "China WEB first stop "; Preg_match_all ("/(src | SRC) = [" | '|] {0,} (http ://(. *). (gif | jpg | jpeg | png)/isU ", $ body, $ img_array ); $ Img_array = array_unique ($ img_array [2]); Echo "captured image :"; Print_r ($ img_array ); |
The output result cannot be the image address of your code.
Result./notfound.jpg
The principle is that we can use preg_match_all to obtain the image. However, there will be a difference in wordpress because the image address rules are different. The code is as follows:
The code is as follows: |
Copy code |
<? Php // Search for and obtain images from the document $ SoImages = '~ ] */> ~ '; Preg_match_all ($ SoImages, $ post-> post_content, $ Images ); $ PictureAmount = count ($ Images [0]); // Obtain the number of Images // Process all images For ($ I = 0; $ I <$ PictureAmount; $ I ++ ){ Echo $ Images [0] [$ I] // process the image. The statement can be customized. The first image is $ Images [0] [0]. } // Use the switch control statement to process the first image. You can also use if... Else... The elseif statement is used for implementation. You can customize the logic. Switch ($ PictureAmount> 0 ){ Case 1: Echo $ Images [0] [0]; // output the first image Break; Default: $ ImagesUrl = bloginfo ('stylesheet _ directory'). "/images/1.jpg"; // Default output image when there is no image in the article } /* Get the image link path code in the article * First, use the first statement to search for and retrieve images from the article. * Then, analyze the image code by running a regular expression and obtain matching characters. * Only one of the regular expression codes is listed below. If you are interested, you can write your own */ // Obtain an image. For example, obtain the first image $ Images [0] [0]. The sample code is as follows: $ ImgUrl = $ Images [0] [0]; $ SoImgAddress = "/] *>/I "; // regular expression statement Preg_match ($ SoImgAddress, $ ImgUrl, $ imagesurl); // analysis Echo $ imagesurl [1]; // obtain and process the path:/intl/zh-CN/images/logo_cn.gif ?> |