Method of Obtaining the First Picture of Article Content by php

  • 2021-07-26 07:02:55
  • OfStack

In this paper, an example is given to describe the method of obtaining the first picture of the article content by php. Share it for your reference. The specific analysis is as follows:

Using php to get the content of the article the first picture method is very simple, we most commonly used is the use of regular, interested friends can refer to 1 below this code.

The following is the code for selecting the first picture in the article:


$obj=M("News");
$info=$obj->where('id=1')->find();
// Method 1*********
$soContent = $info['content'];
$soImages = '~<img [^>]* />~';
preg_match_all( $soImages, $soContent, $thePics );
$allPics = count($thePics[0]);
preg_match('/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i',$thePics[0][0],$match);
dump($thePics);
if( $allPics> 0 ){
  echo "<img src='".$match[1]."' title='".$match[1]."'>";// Gets the name of the picture 
}
else {
  echo " No pictures ";
}
//**************
$soContent = $info['content'];
$soImages = '~<img [^>]* />~';
preg_match_all( $soImages, $soContent, $thePics );
$allPics = count($thePics[0]);
dump($thePics);
if( $allPics> 0 ){
  echo $thePics[0][0]; // Gets the entire Img Attribute 
} else {
  echo " No pictures ";
}
//**************
$soImages = '~<img [^>]* />~';
$str=$info['content'];
preg_match_all($soImages,$str,$ereg);// Regular expressions get the whole picture 
$img=$ereg[0][0];// Picture 
$p="#src=('|\")(.*)('|\")#isU";// Regular expression 
preg_match_all ($p, $img, $img1);
  $img_path =$img1[2][0];// Get the 1 Picture path 
if(!$img_path){
  $img_path="images/nopic.jpg";
} // If no picture exists in the news, use the default nopic.jpg Replace  */
echo $img_path;
//*************88
$str=$info['content'];
preg_match_all("/<img.*\>/isU",$str,$ereg);// Regular expressions get the whole picture 
$img=$ereg[0][0];// Picture 
$p="#src=('|\")(.*)('|\")#isU";// Regular expression 
preg_match_all ($p, $img, $img1);
  $img_path =$img1[2][0];// Get the 1 Picture path 
if(!$img_path){
  $img_path="images/nopic.jpg";
} // If no picture exists in the news, use the default nopic.jpg Replace  */
echo $img_path;

php Get the address of the first picture of the article html content

php to achieve the article html content of the first picture address, the example uses regular expressions to achieve, code for reference only. You can also get all the picture addresses in the article content with a little modification, and expand the specific business.


//  Notice that in this variable img Detail change at the end of label 
$str='<center>
    <img src="http://www.xxxx.com/1.jpeg">
    <img src="http://www.xxxx.com/2.jpeg" >
    <img src="http://www.xxxx.com/3.jpeg"/>
    <img src="http://www.xxxx.com/4.jpeg" />
   </center>';
echo get_html_first_imgurl($str);
exit;
/**
 *  Get the content of the article html Middle grade 1 Address of Zhang Picture 
 */
function get_html_first_imgurl($html){
  $pattern = '~]*[\s]?[\/]?[\s]?>~';
  preg_match_all($pattern, $html, $matches);// Regular expressions get the whole picture 
  $img_arr = $matches[0];// Array of all pictures 
  $first_img_url = "";
  if (!empty($img_arr)) {
    $first_img = $img_arr[0];
    $p="#src=('|\")(.*)('|\")#isU";// Regular expression 
    preg_match_all ($p, $first_img, $img_val);
    if(isset($img_val[2][0])){
      $first_img_url = $img_val[2][0]; // Get the 1 Address of Zhang Picture 
    }
  }
  return $first_img_url;
}

I hope this article is helpful to everyone's PHP programming.


Related articles: