解决Simple Blog Stats插件字数统计错误的问题

Auth:老郭       Date:2023/12/12       Cat:网络       Word:共 520 个字符/单词       Views:50

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

解决Simple Blog Stats插件字数统计错误的问题 - 第1张图片

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

解决Simple Blog Stats插件字数统计错误的问题 - 第2张图片

于是老郭对851行和871行进行了修改,将str_​word_​count函数换成了mb_​strlen函数。

$count += mb_​strlen(preg_​replace('/s/','',html_​entity_​decode(strip_​tags($post->post_​content))),'UTF-8')
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插件字数统计错误的问题 - 第3张图片

老郭博客文章均为原创,本文地址 https://lgbk.cn/archives/917.html,转载请以链接形式注明出处。

作者:

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

发表留言