分类分类
更新时间:2026-05-10 22:22:24作者:sa
php 获取文章内容的第一张图片方法非常的简单,我们最常用的是使用了正则了,下面与一聚教程小伙伴一起来看看吧。
以下是关于选取文章中第一张图片的代码:
$obj=M("News");
$info=$obj->where('id=1')->find();
//方法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]."'>";//获取的图片名称
}
else {
echo "没有图片";
}
//**************
$soContent = $info['content'];
$soImages = '~<img [^>]* />~';
preg_match_all( $soImages, $soContent, $thePics );
$allPics = count($thePics[0]);
dump($thePics);
if( $allPics> 0 ){
echo $thePics[0][0]; //获取的整个Img属性
} else {
echo "没有图片";
}
//**************
$soImages = '~<img [^>]* />~';
$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("/<img.*\>/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;
相关
修真情缘角色扮演303.33 MBv1.0.02026-05-10
下载我的小独角兽女孩休闲益智380.89 MBv1.0.232026-05-10
下载超级种田男孩手机版经营养成1.31Gv1.0.92026-05-10
下载不必要的实验冒险游戏64.9 MBv1.1.592026-05-10
下载军师联盟策略游戏163.2 MBv0.0.12026-05-10
下载翻天喜地凑大钱手机版休闲益智138.72 MBv1.0.92026-05-10
下载东离剑游纪手游动作射击1.6Gv1.4.22026-05-10
下载天猫养车商家版App学习办公74.95 MBv0.6.12026-05-10
下载掌上命运方舟App手游辅助137.48 MBv1.9.12026-05-10
下载退役军人服务APP生活服务67.16 MBv1.2.52026-05-10
下载屯漫漫画APP资讯阅读23.06 MBv2.0.32026-05-10
下载蛋仔派对蛋壳App手游辅助185.73 MBv0.0.22026-05-10
下载









