zblog文章字数统计

随笔 5497 4年前 (2020-02-27)

将下列代码放在zb_users/theme/主题ID/template/post-single.php中要显示字数的位置:

举例文章$article-Content内容为:拓源网-www.toyean.com

方法一:

{php}echo mb_strlen(preg_replace(array("'<(.*?)>'is","'&nbsp;'","'\n\r'","' '","'\r'","'\n'"),'',$article->Content),'UTF-8'){/php}

以上代码会将中英文均计算为1个字符,输出结果为:18

方法二:

{php}echo mb_strwidth(preg_replace(array("'<(.*?)>'is","'&nbsp;'","'\n\r'","' '","'\r'","'\n'"),'',$article->Content),'UTF-8'){/php}

以上代码会将中文计算为2个字符,英文和标点计算为1个字符,输出结果为:21


常见的字数统计中,我们通常使用第二种方法统计文章字数。


分享给朋友: