In website operation, we often encounter situations where we need to handle various long string IDs, such as product SKUs, order numbers, and user IDs.These ID strings typically contain multiple pieces of information, and we may only need part of the numbers for page display, report statistics, or business logic judgments.AnQiCMS (AnQiCMS) powerful template engine provides us with flexible tools, which can easily meet such needs.
Auto CMS template string processing tool: Filter
AnQiCMS's template engine is built-in with rich 'Filters', which are like little tools, and they can process variables in templates in various ways, including string truncation, formatting, conversion, and so on.To extract the part of the numeric string from the ID, we will mainly rely on these filters.
Core strategy one: Precise extraction, extract the required numbers (slice过滤器)
sliceThe filter is the most direct and effective method for processing long strings. It allows you to extract any part of the string based on the start and end positions.
Working principle:
slicefilters throughobj|slice:"start:end"to use it.
obj:represents the long ID string variable you want to operate."start:end":specifies the range to be截取.startIs the starting index (counting from 0),endis the end index (does not include the character at this position).
Useful scenario examples:Assuming your order ID string isORDER-20231026-VIP12345-001where:
ORDER-Is a fixed prefix20231026Is a dateVIP12345Is a member code001Is an order serial number
1. Extract the order serial number “001”:流水号“001”位于整个字符串的最后三位。我们可以从特定的索引位置开始截取。
要提取001,它在ORDER-20231026-VIP12345-001中的起始索引是25(Position of '0'), end index is28(Not included, i.e., the end of the string).
{% set order_id = "ORDER-20231026-VIP12345-001" %}
{% set serial_number = order_id|slice:"25:28" %}
<div>订单流水号:{{ serial_number }}</div>
Output:订单流水号:001
2. Extract member code '12345':Member code “VIP12345” is located in the middle of the string. We can first extract the part containing “VIP”, and then process it.
To extract12345,它在ORDER-20231026-VIP12345-001中的起始索引是19(The position of 'V'), the end index is25(The next position of '5').
{% set order_id = "ORDER-20231026-VIP12345-001" %}
{% set vip_code_with_prefix = order_id|slice:"19:25" %} {# 截取 "VIP12345" #}
{% set vip_number_part = vip_code_with_prefix|slice:"3:" %} {# 从 "VIP" 后面开始截取 "12345" #}
<div>会员编号:{{ vip_number_part }}</div>
Output:会员编号:12345
- Tip:
sliceThe filter is very flexible,startorendAll of them can be omitted. For example,obj|slice:"5:"It will extract from index 5 to the end of the string;obj|slice:":10"It will截取from the beginning to index 9.
Core strategy two: Extracted numbers: used for display and logical judgment
once you have passed throughsliceThe filter successfully extracted the required numerical part, which can then be used for display or logical judgment.
1. For display:You can simply output the variable directly in the template.
{% set order_id = "ORDER-20231026-VIP12345-001" %}
{% set serial_number = order_id|slice:"25:28" %}
<p>您的订单号是:{{ order_id }},流水号为:<span style="font-weight: bold;">{{ serial_number }}</span></p>
Output:您的订单号是:ORDER-20231026-VIP12345-001,流水号为:001
2. Used for logical judgment:If you need to compare the extracted numbers numerically (for example, to judge size), you first need to useintegerThe filter converts it from a string to an integer. The Anqi CMS template engine supports basic logical judgment tags