In AnQiCMS template development, we often use various filters to process data, whereget_digitThe filter is a convenient tool used to extract specific position digits from numbers. However, when we turn our attention to a more common but potentially misunderstood scenario, namely whenget_digitThe filter encounters anon-numeric string. Its behavior becomes less obvious in this case.

First, let's take a look back atget_digitwhen the filter processes pure numbers. When the data is of the standard numeric type,get_digitThe way the filter works is very intuitive.It can extract the corresponding number from a number, based on the reverse position we specify (counting from the right, starting from position 1).

  • {{ 1234567890|get_digit:3 }}it will return8(Counting from right to left, the third digit is 8).
  • {{ 1234567890|get_digit:2 }}it will return9(Counting from right to left, the second digit is 9).
  • If the specified number position exceeds the length of the original number, it will usually return the original number itself, for example{{ 1234567890|get_digit:15 }}Still returns1234567890.

However,get_digitThe filter receives anon-numeric stringWhen used as input, its behavior is unexpected. Many users might guess that it might return an error, or simply return0, or try to convert a character in a string to a number (such as the ASCII value), but this usually does not match the actual result.According to the documentation and examples provided by AnQiCMS, the actual situation is that it returns a number, but this number is not the number we usually extract from strings.

  • For English lowercase strings:{{ "anqicms"|get_digit:2 }}The result is61
  • For English uppercase string:{{ "ANQICMS"|get_digit:2 }}The result is29
  • For Chinese string:{{ "安企内容管理系统"|get_digit:2 }}The result is139

These results clearly indicate that,get_digitThe filter does not directly throw an error or return an expected numeric value when it receives a non-numeric string.相反,它会生成一个看似与原始字符串的数字位不直接相关的数值。get_digitFiltering, the string will first be internally converted to some form (for example, its hash value or some internal numeric representation will be treated as a number), and then applied to the converted numerical valueget_digitThe logic. Therefore, here the returned61/29/139is the result after internal conversion and calculation, not the direct numerical representation of a character in the original string.

So, if you expect to accurately extract a character (such as the second character) from a string and treat it as a number,get_digitThe filter is not a suitable choice. Its original design intention is to handle pure numeric data. In practical applications, to avoid unnecessary confusion and errors, it is recommended to useget_digitBefore the filter, make sure your data is in standard numeric type. If you need to extract or process characters from strings, consider using more specialized string processing filters, such asslice(Used to extract a part of the string) or achieve the purpose through other template logic.

In short,get_digitFilter is a tool designed for numeric data.When it encounters a non-numeric string, although it will return a value, the source and meaning of this value are usually not consistent with our expectation of extracting numbers directly from the string.Understanding this will help us use the template features of AnQiCMS more efficiently and accurately.


Frequently Asked Questions (FAQ):

  1. get_digitFilter can be used to extract numbers from text containing numbers (such as "Order number: 12345")?Cannot.get_digitThe filter is designed to extract fromPure NumbersExtract specific position numbers.If the input is 'Order number: 12345', the system will treat it as a non-numeric string and return a value that is difficult to predict (as described above), rather than '1', '2', etc.To extract numbers from this kind of text, more complex string processing or regular expression methods are required, which usually need custom template functions or backend processing.

2