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.