Auto CMS string cleaning utility guide: Efficiently remove specific characters filter application
During the operation of a website, we often encounter situations where we need to clean and format string data.Whether it is the extra spaces typed by the user or the redundant characters carried in when importing content from the outside, these subtle details may affect the neatness of the website content and the user experience.AnQiCMS (AnQiCMS) provides multi-functional and easy-to-use template filters that can help us easily meet these string cleaning needs.
This article will delve into how to utilizetrim/trimLeft/trimRightandcutThese filters accurately remove unwanted characters from strings, whether they are at the beginning, end, or any position.
Remove characters from the beginning and end of a string:trimSeries of filters
When you need to handle unnecessary characters at both ends of a string,trimthe series filter is your ideal choice. This is particularly useful when cleaning up form data submitted by users, or when unifying content formats.
trimFilter: Clean strings at both endstrimThe filter is mainly used to remove characters from the beginning and end of a string.- Basic usage (removing spaces and newline characters):If you use it without any parameters,
trim,It will automatically detect and remove all consecutive spaces and newline characters at both ends of the string. This is very convenient for standardizing text.- For example:
{{ " 欢迎使用安企CMS " | trim }}After processing, you will get欢迎使用安企CMS.
- For example:
- Advanced Usage (Remove Specific Character Sets):If the characters you need to remove are specific, for example, to remove exclamation marks and commas at both ends,
trimCan accept a string as a parameter. This parameter defines a “character set”,}]trimIt will remove all characters at both ends of the string that belong to this character set until it encounters a character that does not belong to the character set.- For example:
{{ "!!!安企CMS???" | trim:"!?" }}It will remove exclamation marks and question marks from both ends of the string and output the final result.安企CMSPlease note, it removes characters from the character set, not a complete substring.anyCharacters, not a complete substring.
- For example:
- Basic usage (removing spaces and newline characters):If you use it without any parameters,
trimLeftandtrimRightFilter: Precisely control one-sided cleaningSometimes, we may only need to handle the beginning or end of a string, rather than both ends. At this time,trimLeftandtrimRightProvided finer control.trimLeftFilter:Used specifically to remove characters from the left (beginning) of a string.- For example:
{{ " 欢迎使用安企CMS" | trimLeft }}Remove leading spaces, the result is欢迎使用安企CMS. - For example:
{{ "###标题内容" | trimLeft:"#" }}Remove leading hashtags, the result is标题内容.
- For example:
trimRightFilter:Then focus on removing characters from the right (end) of the string.- For example:
{{ "欢迎使用安企CMS " | trimRight }}Remove trailing spaces, the result is欢迎使用安企CMS. - For example:
{{ "内容结尾..." | trimRight:"." }}Remove the trailing ellipsis, resulting in内容结尾.
- For example:
Through thesetrimA series of filters, you can very flexibly clean up the messy characters at the beginning and end of strings, making the content more standardized.
Remove characters at any position in a string:cutFilter
When the character or substring you need to remove is not fixed at the beginning or end, but scattered at any position in the string,cutThe filter is your best choice. It will globally search and clear all matching items.
cutFilter:cut