During the template development process of AnQiCMS, we often use various filters to process and format the data.get_digitThe filter is one of them, its main function is to extract the specified position digit from a number. However, when we pass a Chinese string as input toget_digitThe filter's performance may differ from our intuitive expectations.
get_digitThe basic function of the filter.
First, let's take a look back atget_digitThe basic function of the filter.As the name implies, it is designed to extract a 'specific digit' from numbers.We will specify a position when in use (counting from right to left, starting from 1), and the filter will return the number at that position.If the specified position does not exist, it usually returns the entire original number.1234567890This number:
{{ 1234567890|get_digit:2 }}it will return9(The second from the right is 9){{ 1234567890|get_digit:10 }}it will return1(The 10th from the right is 1)- If the specified digit exceeds the length of the number, for example
{{ 1234567890|get_digit:15 }}, it will return the complete1234567890.
From these examples, it can be seen that,get_digitThe filter is for numbers, it expects the input to be a value that can be split bit-wise.
The performance under Chinese string input
Then, when we pass a Chinese string toget_digitHow will it handle the filter?From the actual test results, its behavior is not simply 'extracting' a 'number' from a Chinese string, but rather attempting to perform some kind of numerical conversion on characters at specific positions in the string.
For example, if we take安企内容管理系统this Chinese string as input and try to get the second last character's “number”:{{ "安企内容管理系统"|get_digit:2 }}We will get139such a result.
Such output results seem somewhat unexpected to us, as it is neither a certain Arabic numeral extracted from a string nor a direct numeric representation of the Chinese character itself.This is because during internal processing, the filter may convert characters to their underlying numeric encoding (such as Unicode code points or UTF-8 byte values) for operation.Due to the multibyte encoding of Chinese characters, the complexity of their internal representation often leads to a final numerical result that is quite different from our usual understanding of 'numbers'.
Read other English examples, such as{{ "anqicms"|get_digit:2 }}It may return61while{{ "ANQICMS"|get_digit:2 }}Then it may return29These numbers are not the ASCII codes of letters minus the ASCII code of '0', but they have been transformed by some specific numerical conversion logic.
Core Points and Usage Suggestions
Through these examples, we can draw several important conclusions:
- Design Intention:
get_digitThe filter is mainly designed for processingPure NumbersorIt can be interpreted as a string representing a numberto extract its digit places. - Unexpected input: When the input is a non-numeric character (especially Chinese characters or common English strings),
get_digitThe filter attempts to convert its internal representation to a number for processing. The result of this conversion is usuallynumericized, but not what we usually mean by "number extraction". - the result is unpredictableFor Chinese character strings, due to their multi-byte encoding characteristics,
get_digitthe output of the filter will be more abstract and unpredictable, usually not producing 'numbers' with business significance.
Therefore, when usingget_digitthe filter, it is recommended that you always ensure that its input is a numeric type that is expected, or at least a string that clearly represents a number (for example"123"If your need is to extract numbers from a string containing Chinese characters or to perform other complex string operations, thenget_digitThe filter may not be the most suitable tool, you may need to consider other string handling methods or custom logic to implement.Correctly understanding and using the functional boundaries of each filter can help us build website features more efficiently and stably.
Common Questions (FAQ)
1.get_digitCan the filter recognize Arabic numerals in Chinese string values? For example, extract '123456' from 'Order number 123456'?Cannot.get_digitFilter is based on the character in the stringpositionThe one being processed, and it expects that a 'number' should be at this position.It cannot intelligently identify and extract a continuous sequence of Arabic numerals from a mixed string.To extract numbers from a mixed string, it is usually necessary to combine other string search or regular expression-related filters (if provided by AnQiCMS) or perform data preprocessing on the backend.
2. Whyget_digitWhy does the filter return values that seem irregular when processing Chinese string, instead of an error message?AnQiCMS's template engine is designed to return a result rather than directly reporting an error when encountering certain type mismatches that can be handled implicitly or through default logic, in order to maintain the smoothness of template rendering.get_digitWhen the input is not a numeric character, it may treat the internal encoding (such as Unicode value or UTF-8 byte data) of the character as a number and "extract" a numerical part from it.These numbers are calculated based on the underlying encoding of characters, so they may appear to have no regularity in the absence of context.
3. If I need to check if a variable is purely numeric so that it can be used safelyget_digitHow should I filter?In the AnQiCMS template, you can useifUse logical judgment in conjunction with other filters to initially check the variable type.For example, you can try to convert it to a numeric type (if supported), or check if it only contains numeric characters.get_digitAn expected number type.