Wordpress obtains all the image implementation programs of the document.

Source: Internet
Author: User
Tags regular expression

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

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.