php implements the method of obtaining the first picture of the article content,
The example in this article describes the method of obtaining the first picture of the article content in PHP. Share it with everyone for your reference. The specific analysis is as follows:
The method of using PHP to obtain the first picture of the article content is very simple. The most commonly used method is to use regular expressions. Interested friends can refer to the following code.
The following is the code for selecting the first image in the article:
Copy code The code is as follows:
$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 "";//The name of the obtained image
}
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]; //The entire Img attribute obtained
} else {
echo "No picture";
}
//****************
$soImages = '~]* />~';
$str=$info['content'];
preg_match_all($soImages,$str,$ereg);//Regular expression obtains the entire image
$img=$ereg[0][0];//Picture
$p="#src=('|")(.*)('|")#isU";//regular expression
preg_match_all ($p, $img, $img1);
$img_path =$img1[2][0];//Get the first image 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 expression captures the entire image
$img=$ereg[0][0];//Picture
$p="#src=('|")(.*)('|")#isU";//regular expression
preg_match_all ($p, $img, $img1);
$img_path =$img1[2][0];//Get the first image 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;
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/906118.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/906118.htmlTechArticlephp method to obtain the first picture of the article content. This article describes the php method to obtain the first picture of the article content. Picture method. Share it with everyone for your reference. The specific analysis is as follows:...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn