Warning: Illegal string offset 'top' in /www/wwwroot/hellyhua.com/wp-content/themes/Snape/single.php on line 45

小编最近在使用帝国CMS做一个网站的时候,想调用文章正文字数,在网上搜索了一下,找到一篇文章,介绍下几种统计方法代码,如下所示:

1、

<?=strlen($navinfor[newstext])?>
统计:8022 个字

2、

<?=strlen(strip_tags($navinfor[newstext]))?>
加上过滤后

统计:1559 个字

3、

<?=strlen(htmlspecialchars(strip_tags(str_replace('&nbsp;','',str_replace('&ldquo;','',str_replace('&rdquo;','',$navinfor[newstext]))))))?>
加上空格等过滤后

统计:1217 个字

4、

<?=strlen(htmlspecialchars(strip_tags(str_replace('&nbsp;','',str_replace('&ldquo;','',str_replace('&rdquo;','',str_replace(egetzy('rn'),'',str_replace(egetzy('<br />'),'',$navinfor[newstext]))))))))?>
加上空格等+html标签等过滤后

统计:1141 个字

看到以上方法,很是高兴,直接采用第四种代码,结果调用出来的字数是8000多,小编有点吃惊,把文章内容放到Notepad中,查看统计字数才2000多。这说明小编使用的代码没有正确统计文章字数。

于是小编又找了找,在帝国论坛上看到有人遇到和小编一样的问题,往下翻看到一位高手贴出了一段代码,小编试了一下,前台显示的字数正好是2000多,说明这代码是正确的。这段代码如下:

<?=mb_strlen(htmlspecialchars(strip_tags(str_replace('&nbsp;','',str_replace('&ldquo;','',str_replace('&rdquo;','',str_replace(egetzy('rn'),'',str_replace(egetzy('<br />'),'',$navinfor[newstext]))))))),'gbk')?>

mb_strlen函数使用说明:
gbk编码,用 mb_strlen($navinfor[befrom],'gbk')
utf-8编码,用 mb_strlen($navinfor[befrom],'utf8')

高手还注释了函数说明,小编网站的编码是utf8,将上面的代码中的gbk换成utf8,使用效果符合小编的要求,在这里分享给大家,希望对大家有帮助


Warning: Illegal string offset 'footer' in /www/wwwroot/hellyhua.com/wp-content/themes/Snape/single.php on line 49