In the process of website content operation, we often encounter the need for string processing, such as cleaning user input, uniform display format, or optimizing search engine inclusion (SEO), and so on. The powerful template engine of AnQiCMS (AnQiCMS) provides a variety of practical filters, wheretrim/trimLeftandtrimRightThese filters are the powerful assistants that allow us to flexibly remove extra spaces or specified characters from the beginning and end of strings or in specific directions.
trimFilter: Bidirectional trimming, accurately removes characters from both ends.
trimThe filter, as the name suggests, is mainly used to trim the ends of strings. Its core function is to remove unnecessary characters at the beginning and end of the string.
Basic usage: remove leading and trailing spacesWhentrimThe filter removes all whitespace from both ends of the string by default when no parameters are provided (including spaces, tabs, newlines, etc.).This is particularly useful when dealing with data from different sources, ensuring the display content is tidy.
For example, if a string variable ismessageThe value of" 欢迎使用安企CMS "we can use it like this:
{{ " 欢迎使用安企CMS "|trim }}
{# 显示结果:欢迎使用安企CMS #}
Advanced usage: remove specified character set
trimThe powerful aspect of the filter is that it allows you to specify a 'character set' to delete.The term "character set" does not refer to deleting an entire word, but rather to each character contained in the string you provide to the filter.trimIt checks whether the beginning and end of the original string contain any characters from this character set, and performs circular deletion until it encounters a character that does not belong to the character set.
For example, if your string is"--- 安企CMS ---"and you want to remove"-"and" "characters:
{{ "--- 安企CMS ---"|trim:" -" }}
{# 显示结果:安企CMS #}
In this example," -"as a character set provided. The filter will start from the beginning and end of the string, removing all occurrences of"-"or" "until it encounters a non"-"nor" "The character of.
You can also store the processing result in a variable for later use, for example:
{% set rawString = "### 安企CMS文档 ###" %}
{% set cleanedString = rawString|trim:"# " %}
{{ cleanedString }}
{# 显示结果:安企CMS文档 #}
trimLeftFilter: Focus on cleaning up the beginning of the string.
If you only want to process the beginning part of a string,trimLeftthe filter is your ideal choice. It works in a similar way to,trimbut only acts on the left side of the string.
Basic usage: Remove leading spacesWhen used without parameters,trimLeftit will remove all leading spaces from the string:
{{ " 系统公告:安企CMS更新了!"|trimLeft }}
{# 显示结果:系统公告:安企CMS更新了! #}
Advanced usage: Remove specified leading character setWithtrimThe same,trimLeftIt also accepts a character set as a parameter. It will remove all characters belonging to the character set from the beginning of the string until it encounters the first character that does not belong to the character set:
{{ "【置顶】安企CMS使用教程"|trimLeft:"【置顶】" }}
{# 显示结果:安企CMS使用教程 #}
In this example,"【置顶】"Provided as a character set. Filters will remove all characters【/置/顶/】that appear at the beginning of the string.
trimRightFilter: refine the end part of the string
WithtrimLeftCorrespondingly,trimRightFiltering focuses on processing the end of a string. It starts from the right side of the string and performs deletion operations.
Basic Usage: Remove trailing spacesWhen no parameters are provided,trimRightIt will remove all trailing spaces from the end of the string:
{{ "安企CMS:高效内容管理工具 "|trimRight }}
{# 显示结果:安企CMS:高效内容管理工具 #}
Advanced usage: Remove specified trailing character set
trimRightIt can also accept a character set parameter. It will remove all characters belonging to the character set from the end of the string, until it encounters the first character that does not belong to the character set:
{{ "联系我们(客服)"|trimRight:"(客服)" }}
{# 显示结果:联系我们 #}
Here,"(客服)"Is considered as a character set.trimRightDeletes all trailing occurrences of(/客/服/)characters, if they appear at the end of the string.
Application scenarios and tips
ThesetrimFilters have a wide range of applications in content operations:
- Data cleaning:When importing content from a database or other system, it is often encountered that field values contain unnecessary spaces or special characters.These filters can quickly clean the data, ensuring that the front-end display is neat and standardized.
- User input validation and formatting:After the user submits the form (such as messages, comments, search keywords), you can use
trimClean leading and trailing spaces to avoid matching failure or display exceptions due to extra spaces. - [en] SEO Optimization:Ensure the title, keywords, description, etc. of the page
metaThe information will not be affected by extra spaces or characters in the search engine parsing.Although the Anqi CMS has made many optimizations in the background, manual trimming may still be required in some custom output scenarios. - Uniform content display style:Some articles or product names may contain special prefixes or suffixes, by
trimLeftortrimRightYou can unify the display style of the front-end without modifying the original content.
When using these filters, please remember that when provided关键词the filter is based oncharacter setthis means it will delete any string that matches关键词Any character that matches the start and end character of a word, rather than deleting a complete 'word' or 'phrase'.
Common Questions (FAQ)
Q:
trimIn the filter.关键词Parameter, is it to delete a complete word, or delete each character in the keyword?A: Here is the关键词Parameter refers to acharacter set.trim(including)trimLeftandtrimRightIt will check if the beginning (or either end) of the string contains any character from the given set and remove it. It will not remove the characters you provide.关键词As a complete phrase to match and delete. For example,"AnQiCMS"|trim:"CMS"It will delete all occurrences ofC/M/Scharacters, rather than deletingCMSthis word.Q: I want to delete a complete word at the beginning of a string, for example
【新】,trimLeftCan it be done?A:trimLeftIt is based on character set deletion, if your string is【新】安企CMS,trimLeft:"【新】"It can indeed be processed as安企CMS。But this is because【/新/】These three characters exactly form the phrase you need to delete. If there are other characters within the phrase, or if the phrase itself is not composed of the characters from the set you provided,trimLeftIt cannot be used as a tool to "delete specific phrases". For the need to delete specific complete phrases, you may need to combinereplacefilters or more complex logic to achieve this.Q: If the string itself does not have leading and trailing spaces or specified characters, what effect will the filter have?
trim过滤器会有什么影响?A: If the string itself does not have leading and trailing spaces or specified characters to be removed, usetrim/trimLeftortrimRightThe filter will not affect the string