How to get the position of a keyword in a string or array in Anqi CMS template?
indexThe filter can calculate the position of a keyword in a line of a string or an array (array/slice).If a string contains multiple keywords to search for, then index returns the position of the first occurrence.If not found, return -1. Note: If there are Chinese characters in the string, one Chinese character counts as 3 positions when calculating the position.
Usage method
indexHow to use the filter:
{{obj|index:关键词}}
For example, to judge欢迎使用安企CMS(AnQiCMS)in the calculationCMSIf the position appears, you can write it like this:
{{"欢迎使用安企CMS(AnQiCMS)"|index:"CMS"}}
# 显示结果
18
Example Demonstration
Calculate the position of a keyword in a line of text
Directly output the result:
{{"欢迎使用安企CMS(AnQiCMS)"|index:"CMS"}}
# 显示结果
18
Calculate the position of a keyword in an array
The array needs the keyword to be equal to the value of the array or the index, it cannot be a partial match.
{% set values = "splits the string 安企CMS"|fields %}
{{values|index:"the"}}
# 显示结果
1
{% set values = "splits the string 安企CMS"|fields %}
{{values|index:"安企"}}
# 显示结果
-1