本文于2024年11月26日修改,修改部分以红色表示。
自从解决了每篇文章字数统计的问题之后,老郭就想着有没有什么办法可以统计整个博客的字数。网上推荐的比较多的是使用Simple Blog Stats插件,很轻巧但是统计功能很强大,于是老郭就按照了一个。但是安装好了之后问题出现了,按照这个插件的统计,老郭博客一共只有16000多个字,这显然是不正确的,老郭已经发了120多篇文章了,如果按照这个数字,那平均每篇文章才100多个字。

但是关于这个插件字数统计错误的问题,网上似乎很少有人提到,更不要说解决的办法了,老郭只能自己摸索了。老郭在插件文件编辑器中打开Simple Blog Stats插件的simple-blog-stats.php文件,在大概第809行的位置找到了有关整个博客字数统计的注释:// number of words all post。然后在注释下面第851行和第871行的位置发现了问题,这里使用的函数是str_word_count($content, 0),而str_word_count函数是用来统计单词数量的,用这个函数来统计中文字数,那出现差错也就是在所难免的了。

于是老郭对851行和871行进行了修改,将str_word_count函数换成了mb_strlen函数。
$count += mb_strlen(preg_replace('/s/','',html_entity_decode(strip_tags($post->post_content))),'UTF-8')
以上代码在统计字数的时候,如果遇到文章中有代码,会把代码也统计进去,导致文章字数统计不准确。为避免此问题,将“// number of words all post”下面一段有关字数统计的代码替换成以下的:
function sbs_word_count_all($wrap) { $disable = apply_filters('sbs_word_count_all_disable', false); if ($disable) return; global $sbs_options; $cache = isset($sbs_options['sbs_enable_cache']) ? $sbs_options['sbs_enable_cache'] : false; $limit = apply_filters('sbs_get_posts_limit', -1); $post_type = apply_filters('sbs_word_count_all_post_type', 'any'); $args = array( 'post_type' => $post_type, 'post_status' => 'publish', 'posts_per_page' => $limit, 'fields' => 'ids', ); $posts = get_posts($args); if (!$posts) return; if ($cache) { if (false === ($count = get_transient('sbs_word_count'))) { $count = 0; foreach ($posts as $id) { $post = get_post($id); $content = isset($post->post_content) ? $post->post_content : null; // 去除短代码 [sbs_word_count] 和 HTML 标签 $content = preg_replace('/(\[sbs_word_count(.*)\])/', '', $content); // 去除代码块(匹配 <code> 和 <pre> 标签,以及 Markdown 代码块) $content = preg_replace('/<pre.*?>.*?<\/pre>|<code.*?>.*?<\/code>|```.*?```/is', '', $content); $content = strip_tags($content); // 去除 HTML 标签 // 统计汉字字符数 $chinese_count = mb_strlen(preg_replace('/[^\x{4e00}-\x{9fa5}]/u', '', $content), 'UTF-8'); // 统计英文单词数及标点符号 preg_match_all('/[a-zA-Z0-9]+|[[:punct:]]/u', $content, $matches); $english_count = count($matches[0]); $count += $chinese_count + $english_count; } set_transient('sbs_word_count', number_format(floatval($count)), 12 * HOUR_IN_SECONDS); } } else { $count = 0; foreach ($posts as $id) { $post = get_post($id); $content = isset($post->post_content) ? $post->post_content : null; // 去除短代码 [sbs_word_count] 和 HTML 标签 $content = preg_replace('/(\[sbs_word_count(.*)\])/', '', $content); // 去除代码块(匹配 <code> 和 <pre> 标签,以及 Markdown 代码块) $content = preg_replace('/<pre.*?>.*?<\/pre>|<code.*?>.*?<\/code>|```.*?```/is', '', $content); $content = strip_tags($content); // 去除 HTML 标签 // 统计汉字字符数 $chinese_count = mb_strlen(preg_replace('/[^\x{4e00}-\x{9fa5}]/u', '', $content), 'UTF-8'); // 统计英文单词数及标点符号 preg_match_all('/[a-zA-Z0-9]+|[[:punct:]]/u', $content, $matches); $english_count = count($matches[0]); $count += $chinese_count + $english_count; } } wp_reset_postdata(); $count = is_int($count) ? $count : 0; $output = number_format($count); $output = ($wrap) ? $sbs_options['count_words_all_before'] . $output . $sbs_options['count_words_all_after'] : $output; return $output; } add_shortcode('sbs_word_count_all', 'sbs_word_count_all');
重新刷新博客后台,老郭发现字数统计已经变成正确的了,问题得到了解决。

《解决Simple Blog Stats插件字数统计错误的问题》留言数:0