详情页

emlog获取文章外链图片数量与数据库上传图片数量

时间:2022年07月21日

编辑:佚名

在制作emlog模版列表时,为了排版有时会需要判断文章内的外链图片与数据库上传的图片数量,通过判断图片数量就可以很好的美化列表模版了,蓝叶就学习着写了这两种统计文章图片数量的函数,有需要的把函数代码复制到emlog模板文件夹下的module.php文件里,然后在列表页或者内容页需要的地方写上调用代码即可。
lanye_imgcount函数是正则获取文章内的外链图片数量,调用代码<?php echo lanye_imgcount($content)?>
lanye_filecount函数是通过查询数据库统计文章内上传的图片数量,调用代码<?php echo lanye_filecount($logid)?>
<?php
function lanye_imgcount($content){
//正则获取文章内的外链图片数量(源码下载www.lxh5068.com)
preg_match_all("|<img[^>]+src=\"([^>\"]+)\"?[^>]*>|is", $content, $imgarr);
$result = $imgarr[1];
return count($result);
}
function lanye_filecount($logid){
//查询数据库统计文章内上传的图片数量
$db = Database::getInstance();
$sql = "SELECT COUNT(*) AS `filepath` FROM ".DB_PREFIX."attachment WHERE blogid=$logid AND (`filepath` LIKE '%jpg' OR `filepath` LIKE '%gif' OR `filepath` LIKE '%png' OR `filepath` LIKE '%jpeg') and `filepath` not like '%thum-%' ORDER BY `aid` asc";
$result = $db->fetch_array($db->query($sql));
$count = $result['filepath'];
return $count;
}
?>
相关文章
猜你需要