How to calculate the number of times a certain keyword appears in a string or array and display it?

Calendar 👁️ 59

In the daily work of content operation, we often need to conduct in-depth analysis of website content, one common requirement is to understand the frequency of a keyword appearing in a specific string or content block.This is crucial for SEO optimization, and it also helps us evaluate the relevance and quality of the content.AnQi CMS as an efficient enterprise-level content management system provides us with very convenient built-in tools to solve this problem, namely the powerfulcountfilter.

Get to knowcountFilter

countThe filter is a practical feature provided by Anqi CMS template engine, which is specifically used to calculate the number of times a keyword appears in a given string or array (also known as a slice) and returns an integer value directly.This process is very intuitive, no complex programming logic is required, just apply it in the template.

Its basic usage is:{{ 变量 | count:"关键词" }}.

For example, if we want to know how many times the word 'AnQiCMS' appears in a text, we can test it in this way:

{% set slogan = "安企CMS是一个强大的内容管理系统,安企CMS致力于提供高效解决方案。" %}
<p>关键词“安企CMS”在这段文本中出现了 {{ slogan | count:"安企CMS" }} 次。</p>

After running, the page will display that the keyword 'AnQi CMS' appears 2 times in this text, which clearly showscountthe function of the filter.

Apply flexibly in different types of content

countThe filter can be applied to various content scenarios on the website, whether it is fixed text, dynamic article content, or custom fields, it can handle it easily.

Find in normal text or custom single-line field

Apply directly to some fixed text content or custom single-line text fieldcountThe filter is the most direct way. Suppose you have a custom field on the back end that stores a brief introduction, and you want to count the number of times a word appears in it, you can directly call the field:

{% archiveDetail introduction with name="introduction" %}
{% set targetKeyword = "安企" %}
<p>文章简介中“{{ targetKeyword }}”出现了:{{ introduction | count:targetKeyword }} 次。</p>

IfintroductionThe content of the field is "AnQi CMS is excellent, AnQi CMS is trustworthy", then the output will be2.

Search within the article or page content

The main content of an article or page is usually the most abundant part of the website, and it is also the focus of keyword density analysis. The content of the articles or single pages of AnQi CMS can be accessed througharchiveDetailorpageDetailLabel retrieval and its content as inputcountInput to the filter.

Please note that the main text of the article often contains HTML tags.countThe filter will directly match and count within the original string containing HTML. If your goal is to count keywords in the visible text of the user, rather than text within tags or attributes, you can usestriptagsThe filter removes all HTML tags and then appliescountfilter.

For example, count the occurrences of "Go language" in the article body:

{% archiveDetail articleContent with name="Content" %}
{% set searchKeyword = "Go语言" %}
<p>关键词“{{ searchKeyword }}”在文章正文中出现了 <b>{{ articleContent | count:searchKeyword }}</b> 次。</p>

{# 如果只想统计纯文本中的关键词,可以先去除HTML标签 #}
<p>(纯文本统计)关键词“{{ searchKeyword }}”在文章纯文本中出现了 <b>{{ articleContent | striptags | count:searchKeyword }}</b> 次。</p>

Thus, you can choose according to your actual needs, whether to count in the complete content containing HTML or in plain text content.

Search in an array or list.

Besides strings,countThe filter also supports finding the number of occurrences of a keyword in an array or list. It is important to note that when applied to an array,countThe filter performs a 'full match' on elements, not a substring match.That is to say, an element of the array must match the keyword you provide exactly in order to be counted.

If you have a passsplitorfieldsA filter that splits the words from a string or is a custom multi-select field (such asarchiveParamsIf a field is multi-select, it can be applied to itcount.

For example, we have a list of article keywords and we want to know how many times the label “CMS” appears:

{% set articleKeywords = "安企CMS,CMS系统,Go语言CMS,CMS解决方案" | split:"," %} {# 假设这是一个由逗号分隔的关键词列表 #}
{% set keywordToCount = "CMS系统" %}
<p>关键词“{{ keywordToCount }}”在标签列表中出现了 {{ articleKeywords | count:keywordToCount }} 次。</p>

Heresplit:","Split the string into an array["安企CMS", "CMS系统", "Go语言CMS", "CMS解决方案"]Then,countThe filter will match elements in the array precisely. In this example, 'CMS system' will match 1 time.

Points to note

While usingcountWhen filtering, there are several minor details to pay attention to in order to ensure you get the expected results:

  • Case sensitive: countThe filter performs case-sensitive matching when matching. For example, "CMS" and "cms" are considered as

Related articles

How to determine if a string, array, or map contains a specific keyword and display the boolean result?

In AnQi CMS template development, we often need to dynamically adjust the display of the page based on the specific attributes or keywords of the content.For example, determine whether an article title contains a certain word, whether a product category is in a certain list, or whether a configuration item exists.The powerful template engine of AnQi CMS provides various filters and operators, which can help us easily implement these judgments and obtain clear boolean (True/False) results.Let's delve deeper into how to implement these flexible judgments in Anqi CMS.### Core Function

2025-11-08

How to display user registration or group details (such as username, level, avatar) on the front page?

In website operation, displaying rich user-related information on the front page is an effective way to enhance user experience and community activity.Whether it is displaying the author's avatar, the user's level, or the username at registration, these personalized details can make the website more vibrant.AnQiCMS as a powerful content management system provides us with very flexible template tags and data call capabilities, making the display of this information easy and simple.### Understand AnQiCMS user and group data In the AnQiCMS backend

2025-11-08

How to automatically convert URLs and email addresses in text to clickable links and display them?

In website content management, we often encounter scenarios where we need to display various links and email addresses in articles or pages.Manually converting this text to clickable HTML links is time-consuming and prone to errors, especially when the amount of content is large, and the workload is惊人的。AnQi CMS fully understands this pain point of users, built-in efficient and intelligent text processing mechanism, can automatically identify and convert URLs and email addresses, greatly improve the efficiency of content publishing and user browsing experience

2025-11-08

How to format a floating-point number to a specified precision (number of decimal places) for display?

In AnQiCMS, the flexibility of content display is one of its core advantages.When dealing with floating-point numbers that require precise display of decimal places, understanding how to utilize its powerful template engine function is particularly important.Ensure that numbers are presented in the expected format for displaying product prices, statistical data, or technical parameters, which can greatly enhance user experience and the professionalism of the website.The Anqi CMS template syntax borrows from the Django template engine, providing rich filters (Filters) to process and format data.Formatting for floating-point numbers

2025-11-08

How to remove spaces or specified characters from the beginning, end, or all positions of a string and then display it?

In website content operation, we often encounter situations where strings contain unnecessary spaces or specific characters.These seemingly minor issues may affect the display aesthetics, SEO effects, and user experience of the content.For example, a product title may display differently on different devices due to leading and trailing spaces, or a keyword list may be surrounded by additional symbols, affecting search engine recognition.

2025-11-08

How to implement automatic conversion of uploaded images to WebP format to optimize front-end loading and display speed?

In today's content operation, website loading speed is crucial for user experience and search engine rankings.Images are an important part of web content, and their loading performance is often a key factor affecting overall speed.AnQiCMS (AnQiCMS) is well aware of this and provides a convenient image optimization feature, especially converting images uploaded to WebP format automatically, which can significantly improve the display speed of the website's front-end.### WebP format: A speed booster for front-end loading WebP is an image format developed by Google

2025-11-08

How to batch regenerate all content thumbnails to fit the new frontend display size?

When the visual style of a website needs a complete overhaul, or to adapt to the ever-changing size of device displays, we often need to adjust the size of image thumbnails.AnQiCMS (AnQiCMS) fully understands this and provides a convenient and efficient solution for such needs.How to batch regenerate thumbnails for all content in response to adjustments in front-end display dimensions to ensure the aesthetic beauty and loading efficiency of the website, which is a concern for many operators.Why do you need to regenerate the thumbnail? Thumbnails play a crucial role in website operations

2025-11-08

How to set up the website to display specific notification information on the front end when it is in "closed site" status?

The website will always encounter times when it needs to be maintained, upgraded, or temporarily adjusted.At this moment, it is crucial for users to see a friendly and clear prompt instead of a cold error page, which is vital for the image and user experience of the website.Fortunately, AnQiCMS provides a very convenient and flexible "shutdown" function, allowing you to easily manage the maintenance status of the website and display customized notification information to front-end visitors.Next, let's take a look at how to set the "shutdown" status of the website in the AnQiCMS backend

2025-11-08