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, so
content|index:keyword_cnwill return6. - “,”(3 bytes), “AnQiCMS”(7 bytes), so
6+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), so
content|index:keyword_enwill return15. - The website operation was not found, so
content|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