PHP obtains the code example of the first image of each article on the website,

Source: Internet
Author: User

PHP obtains the code example of the first image of each article on the website,

<? Php $ temp = mt_rand (1, 4); $ pattern = "/<[img | IMG]. *? Src = [\ '| \ "] (. *? (? : [\. Gif | \. jpg | \. png]) [\ '| \ "]. *? [\/]?> /"; $ Content = $ article-> Content; // The article content preg_match_all ($ pattern, $ content, $ matchContent ); if (isset ($ matchContent [1] [0]) {$ temp = $ matchContent [1] [0];} else {$ temp = "images/random/$temp.jpg"; // four jpg files, named 1, 2, 4}?>

The above code calls the first image of the article by default. When there is no image in the article, the first image of the topic style/images/random/1.jpg1_2.jpg1_3.jpg1_4.jpg is randomly called. If you do not want to call a random image, you can modify it as follows:

<? Php $ pattern = "/<[img | IMG]. *? Src = [\ '| \ "] (. *? (? : [\. Gif | \. jpg | \. png]) [\ '| \ "]. *? [\/]?> /"; $ Content = $ article-> Content; // The article content preg_match_all ($ pattern, $ content, $ matchContent ); if (isset ($ matchContent [1] [0]) {$ temp = $ matchContent [1] [0];} else {$ temp = ". /images/no-image.jpg "; // place a jpg image named no-image in the corresponding position}?>

Use the first image in the document. If there are no images in the document, use the custom image no-image.jpg.

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.