How to get the first occurrence position of a keyword in a string in AnQiCMS

Calendar 👁️ 56

In content operation, we often need to manage and process the text content on the website in a refined manner.In order to content review, dynamic display, or SEO optimization, we sometimes need to know the first occurrence position of a specific keyword in a text.AnQiCMS (AnQiCMS) is an efficient content management system that provides a convenient way to help us meet this need.

Understanding the requirement: Why do we need to find the position of the keyword?

In the process of content publishing and maintenance, understanding the position of keywords in strings has multiple practical values:

  • Content review and sensitive word management:Quickly locate sensitive words or inappropriate expressions in articles, which helps in timely correction and ensures content compliance.
  • Dynamic content display:In some page designs, we may need to truncate text, highlight parts of content, or provide users with a more personalized reading experience based on the position of specific keywords.For example, only display a preview text before or after the keyword.
  • Data analysis and extraction: When it is necessary to extract specific pattern information from a large amount of unstructured text (such as user comments, product descriptions), the precise location of the keyword is crucial for constructing data parsing logic.
  • SEO Optimization Aid:Although Anqi CMS is built-in with powerful SEO tools, understanding the specific location of keywords in page content can help us adjust the content layout more accurately in some advanced optimization strategies, improving keyword density and relevance.

Our Anqi CMS providesindexa filter to elegantly solve this problem.

The solution of Anqi CMS:indexFilter

In the template design of Anqi CMS, we can useindexA filter to get the starting position of the first occurrence of a keyword in the target string. This filter is very intuitive and efficient, it will return the starting position of the keyword match (counting from 0), if the keyword is not found, it will return-1.

Basic syntax:

{{ obj|index:关键词 }}

Here, objRepresents the original string you want to search for, and关键词is the substring you want to find.

It is worth noting that when usingindexWhen filtering, especially when involving Chinese content, there is an important detail to understand:

  • indexThe filter returns:Byte positionThis is not character position. This means that for English characters, one character usually occupies one byte, so character position and byte position are the same.But for Chinese characters, a Chinese character usually occupies 3 bytes.Therefore, if the string you are searching for contains Chinese characters,indexThe position returned may not match the number of characters you can see with your eyes, which is normal behavior because it is calculated based on the underlying byte stream.

Actual operation example

Let's look at several specific examples to see how to use it in AnQi CMS templatesindexfilter.

Example 1: Basic Search

Suppose we have a piece of article content and we want to find the first occurrence positions of “AnQiCMS” and “AnQiCMS”.

{% set content = "欢迎使用安企CMS,AnQiCMS是您的内容管理好帮手。" %}
{% set keyword_cn = "安企CMS" %}
{% set keyword_en = "AnQiCMS" %}
{% set keyword_not_found = "网站运营" %}

<p>关键词“{{ keyword_cn }}”在内容中首次出现的位置:{{ content|index:keyword_cn }}</p>
<p>关键词“{{ keyword_en }}”在内容中首次出现的位置:{{ content|index:keyword_en }}</p>
<p>关键词“{{ keyword_not_found }}”在内容中首次出现的位置:{{ content|index:keyword_not_found }}</p>

Explanation of the running results:

  • 欢迎使用安企CMS,AnQiCMS是您的内容管理好帮手。
    • “Welcome” (3 bytes) + “Use” (3 bytes) = 6 bytes
    • “AnQiCMS” starts from the 6th byte, socontent|index:keyword_cnwill return6.
    • “,”(3 bytes), “AnQiCMS”(7 bytes), so6+3+3+7 = 19.
    • “AnQiCMS” starts from the 15th byte (6 + 3 + 6, if the length of AnQiCMS is counted as 6, then it is 3 + 3 + 6 + 3 = 15), socontent|index:keyword_enwill return15.
    • The website operation was not found, socontent|index:keyword_not_foundwill return-1.

This result fully reflects the rule of Chinese being calculated in 3-byte units.

Example two: combining conditional judgment and text truncation

In practice, we often need to decide how to display content based on whether a keyword appears and its position.For example, we want to highlight a keyword in the article summary or only display part of the content before and after the keyword.

`twig {% set articlebody = “AnQi CMS is an efficient content management system that supports multiple sites and languages, AnQi CMS makes operations simpler.” %} {% set target

Related articles

What role can the `contain` filter play in the context of sensitive word filtering?

In today's era where content is king, website operators are facing the dual challenge of actively publishing high-quality content while strictly controlling content security and compliance.Especially on platforms where user-generated content is increasing, how to efficiently and accurately filter sensitive words has become a key link in maintaining a healthy website ecosystem and protecting brand reputation.AnQiCMS (AnQiCMS) fully understands this need and provides many content security management functions, among which the `contain` filter can play a surprisingly practical role in the sensitive word filtering scenario.### The Foundation of Content Security

2025-11-08

How to check if a specific element exists in an array within a template

In the daily operation and template customization of AnQi CMS, we often encounter situations where we need to dynamically adjust the display of the page according to the data content.One common and practical requirement is to determine whether a specific element exists in an array (or list) within a template.This is crucial for realizing personalized recommendations, conditional content display, and even for functions such as permission control.The Anqi CMS adopts a template engine syntax similar to Django, providing rich tags and filters to handle data.When we get some data set from the backend, such as the list of article tags

2025-11-08

How to judge whether a line of text in the AnQiCMS template contains a certain keyword?

In website operation, we often encounter such needs: to decide how to display page elements based on whether the content contains a specific word, such as highlighting some text, or only displaying a module when certain conditions are met.The AnQiCMS template system provides flexible filter functions to help us easily implement these keyword-based judgments.This article will delve into how to efficiently and accurately determine whether a line of text in the AnQiCMS template contains a certain keyword.### Use `contain`

2025-11-08

What are the practices when using the `count` filter to calculate keyword density?

In website content operation, the reasonable use of keywords is an important factor in improving search engine visibility.We not only need to ensure that the core keywords are reflected in the content, but also need to pay attention to their frequency of occurrence, avoiding overloading or underusing.AnQiCMS provides a variety of powerful content management tools, among which the `count` filter is a very practical feature that can help us effectively count the number of occurrences of keywords in articles.###

2025-11-08

What is the speciality of the `index` filter for Chinese position calculation when processing multi-language content?

## A clever approach: The unique features of `index` filter and Chinese position calculation in AnQi CMS multilingual content When managing multilingual content with AnQi CMS, the flexible use of its powerful template engine and rich filters can greatly improve content operation efficiency.Among them, the `index` filter is a very practical tool that helps us quickly locate the first occurrence of a specific substring in a string.However, when our content involves Chinese characters, the `index` filter has some unique behavior in position calculation.

2025-11-08

How to count the character length of article titles, descriptions, or custom fields?

When managing content on AnQi CMS, we often need to pay attention to the character length of article titles, descriptions, or custom fields.This concerns not only SEO optimization, ensuring that the title and description are in line with the practices of search engines, but also affects the reading experience of users on the search results page or within the website's internal list.How can you easily count the character length of this content in Anqi CMS templates?The Anqi CMS built-in template engine provides many practical filters (filters), among which the `length` filter is our tool for counting character length

2025-11-08

What is the difference between the `length` filter and the `length_is` filter in terms of content length judgment?

In AnQiCMS template design, we often need to judge or obtain the length of content in order to flexibly control the display of content.The `length` filter and `length_is` filter are born for this purpose.Although they are all related to 'length', in actual use, their functions and application scenarios are obviously different.Understanding these subtle differences can help us build templates more efficiently and accurately.### `length` filter: Content length 'counter' `length`

2025-11-08

How to get the number of elements in a list or key-value pair in AnQiCMS template?

AnQiCMS (AnQiCMS) makes website content display intuitive and efficient with its flexible template engine.In website operation, we often need to dynamically adjust the page layout, display different content, and even perform complex logical judgments based on the number of elements in a list or key-value pair (Map).Mastering how to obtain the number of these elements in a template is an indispensable skill for advanced customization and optimization.Luckyly, AnQiCMS template syntax provides a variety of convenient ways to help us achieve this goal.### Clever Utilization

2025-11-08