When building websites and handling user input, we often need to ensure the security and format correctness of the data.AnQiCMS (AnQiCMS) is an enterprise-level content management system developed based on the Go language, which provides a variety of filters in the template to help us complete these tasks. Among them,addslashesA filter is a utility specifically designed for string processing.But when we use it, a natural question may arise in our minds: will this filter change the length of the original string?If it would, how much would it increase?
addslashesThe principle of filter operation and string length change
The answer is affirmative,addslashesThe filter indeed changes the length of the original string and usually increases it. Specifically, it inserts a backslash before the predefined characters in the string.\Escape characters. Each time such a specific character is encountered, the length of the string increases by 1 character.
Then, which characters will beaddslashesThe filter processes? According to the AnQiCMS documentation, it automatically adds backslashes before the following characters:
- Single quote (
') - Double quote (
") - Backslash (
\) - NUL (NULL character)
This means, if your original string contains these characters,addslashesThey will add a backslash "protectively" in front of them. Therefore, if your string contains N characters that need to be escaped, then afteraddslashesAfter processing, the final length of the string will increase by N.
Let's see this change through a few simple examples:
- Processing single quotes:
- Original string:
安企'CMS - Original length: 7
- Using a filter: ``
- Original string: