PHP Gets the code example for the first picture of each article in the site, _php tutorial

Source: Internet
Author: User

PHP Gets the code example for the first picture of each article in the Web site,


<?php $temp =mt_rand (1,4); $pattern = "/<[img| Img].*?src=[\ ' |\ "] (. *? (?: [\.gif|\.jpg|\.png])) [\ ' |\ '].*? [\/]?>/]; $content = $article->content; Article content Preg_match_all ($pattern, $content, $matchContent); if (Isset ($matchContent [1][0])) {   $temp = $matchContent [1][0];} else{   $temp = "images/random/$temp. jpg";//need to place 4 JPG files in the appropriate location with the name 1,2,3,4}  

The above code by default calls the first picture of the article, when the article does not have a picture, random call theme style/images/random/under the 1.jpg, 2.jpg, 3.jpg, 4.jpg pictures. If you don't want to call a random picture, you can modify it:

<?php $pattern = "/<[img| Img].*?src=[\ ' |\ "] (. *? (?: [\.gif|\.jpg|\.png])) [\ ' |\ '].*? [\/]?>/]; $content = $article->content; 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 appropriate location}  

Call the first picture of the article, if the article does not have a picture to call the default picture no-image.jpg

http://www.bkjia.com/PHPjc/1133119.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133119.html techarticle PHP Gets the code example for the first picture of each article in the website, php $temp =mt_rand (1,4); $pattern = "/[img| Img].*src=[\ ' |\ "] (. * (: [\.gif|\.jpg|\.png])) [\ ' |\ '].*[\/]/'; $content = $arti ...

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