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

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

ZJ
ZJ
2019-11-24 / 0 评论 / 260 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2019年11月24日,已超过1614天没有更新,若内容或图片失效,请留言反馈。

   在制作emlog模版列表时,为了排版有时会需要判断文章内的外链图片与数据库上传的图片数量,通过判断图片数量就可以很好的美化列表模版了,蓝叶就学习着写了这两种统计文章图片数量的函数,有需要的把函数代码复制到emlog模板文件夹下的module.php文件里,然后在列表页或者内容页需要的地方写上调用代码即可。

lanye_imgcount函数是正则获取文章内的外链图片数量,调用代码<?php echo lanye_imgcount($content)?>

lanye_filecount函数是通过查询数据库统计文章内上传的图片数量,调用代码<?php echo lanye_filecount($logid)?>

<?php
function lanye_imgcount($content){
//正则获取文章内的外链图片数量
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;
}
?>
本文共 205 个字数,平均阅读时长 ≈ 1分钟
0

评论 (0)

取消