AnQiCMS provides a series of practical and powerful template filters, helping us to process the content in various ways. Among them,wordcountThe filter is a commonly used tool in content operation, which aims to count the number of 'words' in a string. However, when our string contains numbers, special symbols, or even Chinese characters, wordcountHow is the statistics carried out? This is where many users may feel confused when using it, and we will discuss its internal logic in detail next.

wordcountThe core statistics mechanism of the filter is very intuitive: it mainlyuses spaces as the only delimiter for wordsThis means that any continuous sequence of non-space characters, regardless of whether it contains numbers, letters, punctuation marks, or other special characters, will bewordcountconsidered as a separate 'word'.

  • A combination of numbers and letters
  • Special symbols and punctuationWhen the string contains hyphens (-), underscores (_), or exclamation marks (!))question mark(?When special symbols such as these are present, as long as they are not separated by spaces from letters or numbers, they are considered part of the word.like “Hello-World!”The word 'will' is counted as a single word because hyphens and exclamation marks do not act as separators.AnQiCMS!It is also a word.Only when these symbols are preceded and followed by spaces can they become separators between words.
  • Consecutive spacesIf multiple consecutive spaces occur in a string (for example, "Hello u0026nbsp;u0026nbsp;u0026nbsp; World"),wordcountIt will automatically treat it as a word separator and will not increase the word count due to multiple spaces. It will intelligently treat them as a logical separator.
  • Chinese characterFor pure Chinese strings, as there is usually no space between Chinese characters, for example, '安企内容管理系统',wordcountIt will treat the whole as a single word.Only when Chinese character strings explicitly contain English characters or numbers, and there are spaces between these English characters or numbers, will they be separated according to the spaces.For example, "Anqi CMS System" would be counted as 3 words.

Let us understand better through several specific exampleswordcountThe statistical logic of:

Input string Expected output (word count) Explanation
"Hello World" 2 HelloandWorldseparated by spaces.
"Hello-World!" 1 -and!It does not constitute a space, soHello-World!it is counted as a single word.
"AnQiCMS V3.0" 2 AnQiCMSandV3.0separated by spaces.
"12345" 1 Pure numbers, without spaces.
"这是一个 AnQiCMS 教程。" 3 这是一个,AnQiCMS,教程。Separated by spaces, punctuation is attached to the word.
"GoLang v1.20" 2 GoLangandv1.20separated by spaces.
"安企CMS功能丰富!" 1 Pure Chinese with punctuation, without spaces, considered as one word.
""(Empty string) 0 Empty string, no content.

These examples clearly demonstratewordcountHow the filter strictly follows the principle of counting with spaces as delimiters.

UnderstandingwordcountThe logic of filters using spaces as the main delimiter has important guiding significance for us in content operation and template design. For English or other languages that use spaces to separate words, wordcountCan provide relatively accurate word statistics.When numbers, special symbols, or Chinese characters are closely connected with English words and there is no space between them, they will be treated as a whole.This means that if your goal is to count 'entries' rather than 'words' in the strict sense, this logic may align with your expectations.

However, as Chinese is usually not separated by spaces between words,wordcountThe filter often treats an entire paragraph of Chinese text as a single word when counting, unless there are explicit English or numeric words separated by spaces, which may differ from our usual understanding of 'word' counting.If you need a more precise Chinese word count, you may need to combine other methods (such as using front-end JavaScript libraries for tokenization, or pre-processing during content generation).

If your content contains a large number of special symbols or numbers that need to be counted separately but are not separated by spaces (for example,产品-ID-123You may need to count as 3 words), you may need to replace these special symbols with spaces (for example)wordcountUse it before the filterreplaceThe filter will replace these special symbols with spaces (for example){{ your_string|replace:"-, "|wordcount }}), and then perform)wordcountthe count.

AnQiCMSwordcountThe filter provides a quick tool for counting the number of words in a string with its concise 'space-separated' logic.Whether it is dealing with mixed text containing numbers, special symbols, or pure alphabetical sequences, as long as you master its core mechanism, you can make more effective use of this filter to meet the needs of content display and operation.