In website operations, we often encounter scenarios where we need to handle some structured long numeric strings.For example, a product code may include the production date and batch information; an order number may imply a regional code and serial sequence; or it may be a unique identifier generated in specific business logic.These long digital strings often carry rich metadata, while we may only need specific position digital information for display, filtering, or further processing.
AnQiCMS provides a flexible and powerful template engine, combined with its built-in various filters, which can help us easily extract the required information from these long numeric strings.Below, let's take a detailed look at how to implement this requirement in the AnQiCMS template.
First, useget_digitExtracting a single digit with a filter
When you need to accurately extract from a long numeric stringAn individual number at a specific locationwhenget_digitThe filter is your ideal choice. It can help you quickly locate and obtain the target number.
Working principle:
get_digitThe filter will start from the number string you provideOn the right sideStart calculating the position, and the starting value at this position is1For example, a string12345of the rightmost5is located at position1,4is located at position2. Continue in this manner.
Usage:
{{ 你的长数字字符串 | get_digit:位置 }}
Actual Case:Suppose we have a product serial number10240315001where:
03represents the month15represents the date001Represents the production batch of the day
If we want to get the last digit representing the date in this serial number (i.e.,)15of5), we can do this:
{% set serial_number = "10240315001" %}
<!-- 提取倒数第 3 位的数字(从右往左数,'5'是第3位) -->
<p>日期的个位数:{{ serial_number | get_digit:3 }}</p>
The output result will be:5.
Please note,get_digitThe filter is mainly used to extract the numeric characters represented in the string. If位置it exceeds the actual length of the string, it will return the entire string.
Secondly, utilizingsliceThe filter extracts consecutive numeric segments
Withget_digitExtracting individual numbers differently, when you need to extracta continuous sequence of digitswhenslicethe filter is very useful. It provides more flexible range selection.
Working principle:
sliceThe filter allows you to slice any part of a string by specifying the start and end positions. Remember, its起始位置is based on0indices (i.e., the first character of a string is position0), while结束位置Yesdoes not includeIndex within the extracted result.