首页
Search
1
朋友圈又被财神占领
7 阅读
2
入手慈云香港BGP云服务器
7 阅读
3
淘宝的客服真业余
5 阅读
4
意外险变相涨价
4 阅读
5
老郭玩域名之第一次出售域名
4 阅读
网络
数码
家居
购物
金融
汽车
旅游
健康
游戏
通信
美食
娱乐
教育
登录
找到
2
篇与
字数
相关的结果
2024-11-27
修正博客字数统计不准确的问题
为了便于博客的统计和阅读,老郭分别设置了博客整体字数统计和每篇文章字数统计的功能,详见《为使用子比主题的博客文章添加字数统计》和《解决Simple Blog Stats插件字数统计错误的问题》。不过,由于之前使用的代码都是从网上找来之后老郭手动修改的,虽然能够实现基本的功能,但却不太完善。最主要的问题之一就是对于含有代码的文章字数统计出入太大,例如《给博客加上重要日子倒计时》这篇文章,虽然正文只有不到300个字,但是由于文章里有大段的代码,导致最后统计的时候把代码也给算了进去,结果就变成了超过3000个字,足足相差了10倍。 虽然这个问题对于博客的运行没有什么影响,但总让人看着不那么舒服。于是老郭只得再一次求助于AI,经过多次的修改,终于算是较为圆满的解决了这一个问题。不得不说,现在AI对于编程来说确实是太方便了,不然以老郭的水平,是不可能完成这项工作的。 具体的修改内容,老郭已经放在原来的两篇文章里了,这里就不重复了,有兴趣的可以看看。解决问题的思路就是汉字和中文标点按字数统计,英文按单词统计,代码里的英文和中文注释都不统计。只要把这些信息和原来的代码告诉AI,很快就能得到正确的新代码,而且还能顺带纠正原来代码里面的一些语法错误,另外,AI还贴心的加上了许多的中文注释,方便老郭对代码进行理解。
网络
# 代码
# 字数
# AI
# 统计
老郭
1年前
0
1
0
2023-12-12
解决Simple Blog Stats插件字数统计错误的问题
本文于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'); 重新刷新博客后台,老郭发现字数统计已经变成正确的了,问题得到了解决。
网络
# 错误
# 插件
# 字数
老郭
2年前
0
1
0