php获取文章的第一张图片,php 获取文章内容的第一张图片示例

$obj=M("News");

$info=$obj->where('id=1')->find();

//方法1*********

$soContent = $info['content'];

$soImages = '~

66022.htm]* />~';

preg_match_all( $soImages, $soContent, $thePics );

$allPics = count($thePics[0]);

preg_match('//i',$thePics[0][0],$match);

dump($thePics);

if( $allPics> 0 ){

    echo "%22.%24match%5B1%5D.%22";//获取的图片名称

}

else {

echo "没有图片";

}

//**************

$soContent = $info['content'];

$soImages = '~

66022.htm]* />~';

preg_match_all( $soImages, $soContent, $thePics );

$allPics = count($thePics[0]);

dump($thePics);

if( $allPics> 0 ){

echo $thePics[0][0]; //获取的整个Img属性

} else {

echo "没有图片";

}

//**************

$soImages = '~

66022.htm]* />~';

$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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值