In website operation, we often encounter the need to handle some structured long numeric string scenarios.For example, a product code may include the production date and batch information;A order number may imply a regional code and serial number;Or some unique identifier generated in a specific business logic.These long digital strings often carry rich metadata, and 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, we will take a detailed look at how to implement this requirement in the AnQiCMS template.

1. Useget_digitFilter to extract a single number

When you need to accurately extract a single digit from a long numeric stringat a specific positionthen,get_digitThe filter is your ideal choice. It can help you quickly locate and retrieve the target number.

Working principle: get_digitThe filter will start from the numeric string you provided.RightAnd the starting position of this position is.1For example, the string12345is on the rightmost side.5Located at the position1,4Located at the position2and so on.

How to use:

{{ 你的长数字字符串 | get_digit:位置 }}

Actual case:Suppose we have a product serial number10240315001where:

  • 03Represents the month
  • 15Represents the date
  • 001Representing the production batch of the day

If we want to get the last digit of the date represented by this serial number (i.e.15of5This can be done in this way:

{% set serial_number = "10240315001" %}
<!-- 提取倒数第 3 位的数字(从右往左数,'5'是第3位) -->
<p>日期的个位数:{{ serial_number | get_digit:3 }}</p>

The output 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.

Second, usingsliceFilter to extract continuous numeric segments

withget_digitExtract different single numbers when you need to extract from a long numeric stringA continuous sequence of numeric digitsthen,sliceThe filter is very practical. It provides a more flexible range of truncation.

Working principle: sliceThe filter allows you to truncate any part of the string by specifying the start and end positions. Remember, its起始位置is based on0index (i.e., the first character of the string is the position0),while结束位置IsDo not includein the index of the substring.