To use PHP to get all the pictures in the article in the first picture, we just need a simple regular expression can be achieved, the following small series to share two examples of it.
First look at a function:
| The code is as follows |
Copy Code |
function Getpic ($str _img) { Preg_match_all ("//isu", $str, $ereg);//Regular expressions get the whole picture out. $img = $ereg [0][0];//pictures $p = "#src = (' |") (.*) ('|") #isU ";//Regular expression Preg_match_all ($p, $img, $img 1); $img _path = $img 1[2][0];//Get the first picture path return $img _path; } If the database is open, use $NR to get the news content in the database $NR = $row _news["nr"]; $aa =getpic ($nr _a); if (! $aa) {$aa = "images/nopic.jpg";}//If there is no picture in the news, replace it with the default nopic.jpg |
Look at a function that's relatively complex.
In the project, the design of the page will sometimes leave the location of the article featured images, but sometimes, this article instead of uploading pictures, then the page is displayed when there is no picture, the style is very difficult to see, if the simple is not upload pictures to choose the default image, sometimes cause some misunderstanding In consideration is not first to this article picture of the Problem refinement processing: first determine whether there is an uploaded image, if there is a picture uploaded, no judge whether there is a picture in the content, there is the first image as a feature here, if there is no picture in the content, then the default image is shown here;
Here's the code for selecting the first picture in the article:
| The code is as follows |
Copy Code |
$obj =m ("News"); $info = $obj->where (' id=1 ')->find (); Method 1********* $soContent = $info [' content ']; $soImages = ' ~]*/>~ '; Preg_match_all ($soImages, $soContent, $thePics); $allPics = count ($thePics [0]); Preg_match ('//i ', $thePics [0][0], $match); Dump ($thePics); if ($allPics > 0) { echo "";//Get the picture name } else { echo "No picture"; } //************** $soContent = $info [' content ']; $soImages = ' ~]*/>~ '; Preg_match_all ($soImages, $soContent, $thePics); $allPics = count ($thePics [0]); Dump ($thePics); if ($allPics > 0) { echo $thePics [0][0]; Gets the entire IMG property } else { echo "No picture"; } //************** $soImages = ' ~]*/>~ '; $STR = $info [' content ']; Preg_match_all ($soImages, $str, $ereg);//Regular expressions get the whole picture. $img = $ereg [0][0];//pictures $p = "#src = (' |") (.*) ('|") #isU ";//Regular expression Preg_match_all ($p, $img, $img 1); $img _path = $img 1[2][0];//Get the first picture path if (! $img _path) { $img _path= "Images/nopic.jpg"; }//If there is no picture in the news, replace it with the default nopic.jpg */ echo $img _path; 88 $STR = $info [' content ']; Preg_match_all ("//isu", $str, $ereg);//Regular expressions get the whole picture out. $img = $ereg [0][0];//pictures $p = "#src = (' |") (.*) ('|") #isU ";//Regular expression Preg_match_all ($p, $img, $img 1); $img _path = $img 1[2][0];//Get the first picture path if (! $img _path) { $img _path= "Images/nopic.jpg"; }//If there is no picture in the news, replace it with the default nopic.jpg */ echo $img _path; |
http://www.bkjia.com/PHPjc/633155.html www.bkjia.com true http://www.bkjia.com/PHPjc/633155.html techarticle to use PHP to get all the pictures in the article in the first picture, we just need a simple regular expression can be achieved, the following small series to share two examples of it. First look at a ...