在制作emlog模版列表时,为了排版有时会需要判断文章内的外链图片与数据库上传的图片数量,通过判断图片数量就可以很好的美化列表模版了,蓝叶就学习着写了这两种统计文章图片数量的函数,有需要的把函数代码复制到emlog模板文件夹下的module.php文件里,然后在列表页或者内容页需要的地方写上调用代码即可。
1、lanye_imgcount函数是正则获取文章内的外链图片数量,调用代码<?php echo lanye_imgcount($content)?>
2、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;
}
?> 
原文链接:https://www.qiquanji.com/post/8685.html
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
 
                        微信扫码关注
更新实时通知
