[phpstudy]php 获取文章内容的第一张图片示例

更新时间:2019-07-10    来源:php常用代码    手机版     字体:

【www.bbyears.com--php常用代码】

以下是关于选取文章中第一张图片的代码:

 代码如下

$obj=M("News");
$info=$obj->where("id=1")->find();
//方法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 "";//获取的图片名称
}
else {
    echo "没有图片";
}
//**************

$soContent = $info["content"];
$soImages = "~]* />~";
preg_match_all( $soImages, $soContent, $thePics );
$allPics = count($thePics[0]);
dump($thePics);
if( $allPics> 0 ){
    echo $thePics[0][0]; //获取的整个Img属性
} else {
    echo "没有图片";
}
//**************

$soImages = "~]* />~";
$str=$info["content"];
preg_match_all($soImages,$str,$ereg);//正则表达式把图片的整个都获取出来了
$img=$ereg[0][0];//图片
$p="#src=("|\")(.*)("|\")#isU";//正则表达式
preg_match_all ($p, $img, $img1);
   $img_path =$img1[2][0];//获取第一张图片路径
if(!$img_path){
    $img_path="images/nopic.jpg";
} //如果新闻中不存在图片,用默认的nopic.jpg替换 */
echo $img_path;
//*************88

$str=$info["content"];
preg_match_all("//isU",$str,$ereg);//正则表达式把图片的整个都获取出来了
$img=$ereg[0][0];//图片
$p="#src=("|\")(.*)("|\")#isU";//正则表达式
preg_match_all ($p, $img, $img1);
   $img_path =$img1[2][0];//获取第一张图片路径
if(!$img_path){
    $img_path="images/nopic.jpg";
} //如果新闻中不存在图片,用默认的nopic.jpg替换 */
echo $img_path;

本文来源:http://www.bbyears.com/jiaocheng/57347.html

热门标签

更多>>

本类排行