In daily content operations, we often encounter scenarios where we need to perform statistical analysis on content, such as counting the number of times specific keywords appear, checking the frequency of repetition of certain elements in the content. AnQiCMS provides many practical template filters to help us achieve these functions, among whichcountThe filter is a powerful tool for counting. When dealing with content containing Chinese characters, it is particularly important to understandcounthow the filter performs counting.

countThe core function and basic usage of the filter

countThe filter is mainly used to count the number of occurrences of a certain 'keyword' in a line of string or an array (Array/Slice). Its basic usage is very intuitive:

{{ obj|count:关键词 }}

HereobjRepresent the string or array variable you want to count, and关键词then is the specific text you want to count the occurrences of.

For example, if we want to know how many times 'CMS' appears in a sentence:

{{"欢迎使用安企CMS(AnQiCMS)"|count:"CMS"}}

The output of this code will be:2This indicates:countThe filter can accurately identify and count the frequency of target keywords in a string.

Unveiling the counting logic of Chinese string

AnQiCMS'countThe filter shows good compatibility and intelligence in counting Chinese string characters. It does not simply match by bytes, but is based oncharacter sequence matchingCounting in.This means that regardless of whether the search keyword is English or Chinese, the system will treat it as a complete text unit and search for a complete matching sequence in the target string.

For example, if we have a string containing Chinese characters and we want to count the occurrence of a certain Chinese string:

{{"安企CMS是安企科技开发的安企CMS内容管理系统"|count:"安企"}}

This code will return:2This clearly shows,countThe filter can correctly identify and count the occurrences of Chinese character strings without being affected by character encoding or multi-byte representation.It is friendly to the Unicode character set and can accurately match and count Chinese characters just like English.

The difference in counting between strings and arrays

However, it is worth noting thatcountThe filter has a slightly different counting logic when dealing with strings and arrays:

  1. Counting in strings:WhenobjWhen it is a string,countThe filter searches the entire string关键词The number of occurrences of the substring. As shown in the example above, even if the keyword is embedded in a longer string, it will be counted as long as the sequence matches.

  2. Counting in an array:Whenobjwhen it is an array,countThe filter will check if each element of the array iscompletely equal to 关键词It will not search for substrings within the array elements. If there is an element in the array that is exactly the same as the关键词string, then that element will be counted once.

    Let's look at an array example, suppose we first usefieldsThe filter splits the string into an array by spaces:

    {% set values = "splits the string 安企CMS"|fields %}
    

    At thisvaluesThe array contains["splits", "the", "string", "安企CMS"].

    If we try to count"the":

    {{values|count:"the"}}
    

    The result is1, because there is an element that is"the".

    but if we try to count"安企":

    {{values|count:"安企"}}
    

    the result will be0. This is because there is no element that isperfectequals"安企"of, although there is an element that is"安企CMS", but it is not"安企"an exact match.

Application scenarios in practice

MastercountThe counting logic of the filter, is actually helpful for website operations and template development:

  • Content Quality Assessment:Quickly count the frequency of a core keyword in an article to assist in optimizing keyword density.
  • Duplicate Content Detection:Check if there are any overly repetitive phrases in the user's submitted content.
  • Dynamic content display:Dynamically adjust the display priority or style of content based on the number of times it appears in a list according to certain conditions.
  • Data analysis:In the background log or a specific data field, count the number of times a particular event occurs.

In short, AnQiCMS'scountThe filter is a powerful tool and is friendly to Chinese characters.Understand how it counts specifically under different data types (strings and arrays), which can help us use it more accurately for content statistics and data processing, thereby better managing and operating website content.


Frequently Asked Questions (FAQ)

Q1: Why am I using in an array containing Chinese?countFilter, but did not get the result I expected?A1:countThe filter requires the array elements to match the provided 'keyword' string exactly.It does not search for substrings within array elements."安企CMS"And you search"安企"The result will be 0, because it needs an exact match"安企"As an independent element.

Q2:countCan a filter be used to calculate the total number of characters in a Chinese sentence?A2:countThe filter is used to count the occurrences of specific "keywords", not the total number of characters. If you need to calculate the total number of characters in a Chinese sentence, you should uselengthA filter that can correctly identify and calculate the number of Chinese characters encoded in UTF-8, such as{{ "你好世界"|length }}will return4.

Q3: If I need to count the number of different words appearing in a segment of Chinese text,countCan the filter do that?A3:countThe filter cannot directly count the number of all different words.It can only count for a single "keyword" specified explicitly.splitSplit text into word groups) and perform more complex processing of the loop logic of templates, or use the keyword library or content material management function of AnQiCMS backend for auxiliary analysis.