PHP obtains the code example of the first image of each article on the website ,. PHP code example for obtaining the first image of each article on the website, php $ tempmt_rand (); $ pattern [img | IMG]. * src [|] (. * (: 2.16.gif |. jpg |. png]) [|]. * []; $ content $ arti PHP obtains the sample code 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.
Authorization, php $ temp = mt_rand (); $ pattern = "/[img | IMG]. * src = [\ '| \ "] (. *(:[\. gif | \. jpg | \. png]) [\ '| \ "]. * [\/]/"; $ content = $ arti...