How to extract specific number information from a long numeric string in AnQiCMS template?

Calendar 👁️ 73

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.

Related articles

How to precisely control the display of floating-point numbers in the AnQiCMS template, such as retaining two decimal places?

In website content operation, the way numbers are presented often affects user experience and the accuracy of information.Especially for floating-point numbers, such as product prices, statistics, and ratings, it is common to require accuracy to several decimal places or rounding according to business needs.AnQiCMS is a powerful template system that provides us with a flexible way to handle these data.Today, let's delve into how to efficiently and accurately control the display of floating-point numbers in the AnQiCMS template.

2025-11-08

How to split a line of text content (such as a tag string) into an array of individual words for processing in AnQiCMS?

In the practice of AnQiCMS content management, we often encounter scenarios where it is necessary to split a seemingly simple line of text into smaller, more independent 'words' for fine-grained processing.For example, the document tag (Tag), keyword list, or multiple values separated by a specific symbol in custom fields.The core of this requirement lies in converting a string into an array that can be traversed and manipulated individually.

2025-11-08

The `escape` and `escapejs` filters in AnQiCMS are applicable to which HTML/JS escaping scenarios?

In AnQiCMS template development, it is very important to understand and properly use escape filters to ensure website security and correct content display.The system uses a template engine syntax similar to Django, which means it takes some security measures by default when handling variable output.Today, let's talk about the `escape` and `escapejs` filters to see in which scenarios they can be used.

2025-11-08

How to quickly view the detailed structure and value of complex variables during debugging in AnQiCMS template?

During AnQiCMS template development, we often need to understand what data and structure a variable contains internally, especially when dealing with complex data objects or debugging template issues.Sometimes, direct output of a variable can only yield a simple value or error message, and cannot delve into its detailed composition.At this point, it is particularly important to master some effective methods for viewing the complete structure and value of variables.### The Challenge of AnQiCMS Template Debugging The template syntax of AnQiCMS is versatile, whether it is the system built-in `archive`

2025-11-08

How to find the first occurrence index of a character or substring in the AnQiCMS template?

During the process of displaying website content or template development, we often encounter situations where we need to process specific text, such as checking if a keyword exists or locating the first occurrence of a character or substring.The template engine of AnQiCMS (AnQiCMS) provides a series of powerful filters (Filters) to help us complete these tasks efficiently.Today, let's discuss how to use the `index` filter to accurately find the position index of the first occurrence of a character or substring in the AnQiCMS template.Understand

2025-11-08

In AnQiCMS template, how to convert a string number into an actual `integer` or `float` type?

When working with the Anqi CMS template to display content and make logical judgments, we often encounter situations where we need to perform operations on numbers.However, data obtained from a database or content model, even if it looks like a number in the background, is sometimes passed as a string (``string``) at the template level.If this is directly performed arithmetic operations or numerical comparisons, unexpected results may occur.

2025-11-08

How to concatenate the tag array in AnQiCMS template into a string with a custom delimiter?

In AnQiCMS template development, we often need to display a set of data obtained from the database, such as the multiple tags (Tags) of articles, in a user-friendly manner rather than simply listing them.One of the most common needs is to concatenate these tags into a string with a custom separator, such as AnQiCMS's powerful template engine provides a flexible way to achieve this goal.

2025-11-08

How to get the total length of elements in a string, array, or key-value pair in AnQiCMS template?

In AnQi CMS template design, dynamically obtaining the total length of elements in a string, array, or key-value pair is a very practical feature. It can help us flexibly display information on the page, such as showing the number of products, the number of comments, or adjusting the layout according to the content length.The Anqi CMS template engine provides simple and efficient filters to meet these requirements.### Core Tool: `length` Filter The most direct and universal method to obtain the total number of elements in any string, array (slice), or key-value pair (map/struct) is to use

2025-11-08