In content operation, we often need to manage and process the text content on the website in a refined manner.Whether for content moderation, dynamic display, or SEO optimization, sometimes we need to know the position of the first occurrence of a specific keyword in a text.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?

Understanding the position of keywords within strings has multiple practical values during the content publishing and maintenance process:

  • Content review and sensitive word management:Quickly locate sensitive words or inappropriate expressions in articles, which helps to correct them in time and ensure content compliance.
  • [en] Dynamic content display:In certain page designs, we may need to truncate text, highlight parts of the content, or provide a more personalized reading experience based on the position of specific keywords.For example, only display a preview text segment before or after the keywords.
  • Data analysis and extraction:When it comes to extracting specific pattern information from a large amount of unstructured text (such as user comments, product descriptions), the precise position of keywords is crucial in constructing data parsing logic.
  • SEO Optimization Assistant:Although AnQi CMS is built with powerful SEO tools, understanding the specific position of keywords within page content can help us adjust content layout more accurately and enhance keyword density and relevance in certain advanced optimization strategies.

【en】Security CMS provides with its powerful template engine,)indexa filter to elegantly solve this problem.

【en】Security CMS solution:indexFilter

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

Basic syntax:

{{ obj|index:关键词 }}

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

It is worth noting that when usingindexWhen using a filter, especially when dealing with Chinese content, there is an important detail to understand:

  • indexThe filter returns isByte positionThe comma is not about character position, but about context.This means that for English characters, a character usually occupies one byte, so the character position and byte position are the same.But for Chinese characters, a Chinese character usually occupies 3 bytes.indexThe returned position may not match the character position you count 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 templates in the Anqi CMS.indexFilter.

Example 1: Basic Search

Assuming we have a piece of text 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 run results:

  • 欢迎使用安企CMS,AnQiCMS是您的内容管理好帮手。
    • Welcome (3 bytes) + Use (3 bytes) = 6 bytes
    • 'AnQi CMS' starts from the 6th byte, socontent|index:keyword_cnit will return6.
    • Comma (3 bytes), AnQiCMS (7 bytes), so6+3+3+7 = 19.
    • “AnQiCMS” starts from the 15th byte (6 + 3 + 6, if we count the length of AnQiCMS as 6, then it is 3 + 3 + 6 + 3 = 15), socontent|index:keyword_enit will return15.
    • The website operation was not found, socontent|index:keyword_not_foundit will 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 practical applications, 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 specific keyword in the article summary or only display the content before and after the keyword.

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