In the template development of AnQi CMS,replaceA filter is a very flexible string processing tool that can help users quickly replace specific strings in text content.However, when its application scenario involves handling URL (Uniform Resource Locator) parameters, we need to invest more thought and caution, as URL parameters have their own unique structure and encoding standards.

Get to knowreplaceBasic function of the filter

First, let's take a look backreplaceThe basic usage of the filter. Its syntax structure is usually{{ 字符串变量|replace:"旧字符串,新字符串" }}. It will find all the parts of the string that match the "old string" and replace them with the "new string".

For example, if you have a variable namedtitleThe variable, its value is"欢迎使用安企CMS内容管理系统"And you can replace "AnQi" with "AnQi", like this:

{{ title|replace:"安企,AnQi" }}
{# 显示结果: 欢迎使用AnQiCMS内容管理系统 #}

Pay special attention herereplaceThe two special behaviors of the filter:

  1. When the "old string" is empty:replaceThe filter exhibits an unusual behavior. It inserts the 'new string' between each UTF-8 character in the original string. For example,{{ "ABC"|replace:",-" }}It will output.A-B-C-This feature may be useful in general string processing, but is almost always catastrophic in URLs.
  2. When the 'new string' is empty: At this timereplaceThe filter will directly remove the matched "old string" from the original string. For example,{{ "欢迎使用安企CMS"|replace:"安企," }}It will output.欢迎CMS.

The specificity of URL and its parameters

The composition and parameter passing methods of URLs are strictly regulated. A complete URL usually includes the protocol, domain name, path, and query parameters. The query parameter part is preceded by a question mark (?Start with a parenthesis, parameters separated by an ampersand&Connected with a parenthesis, each parameter consists of a key and a value separated by an equal sign=Consists of connection with a parenthesis, each parameter is composed of a key and a value connected by an equal sign

What is more important is that some characters in the URL (such as spaces,&/?/=etc.) must be URL-encoded when transmitted as parameter values, converting them to%XXThe form, to avoid confusion with separators in URL structures. For example, spaces are encoded as%20or+,and the ampersand&Will be encoded as%26.

When processing URL parameters,replaceThe risks of filters may bring

due toreplaceThe filter is based on plain string matching and replacement, it does not know which part of the URL it is processing, therefore it is directly used to modify the URL string, especially where parameters are involved, which can easily lead to problems:

  1. Destroy the URL structureIf you try to replace the?/&/=and other key separators directly, the structure of the URL will be immediately destroyed, causing the page to fail to parse correctly. For example, replace thesearch?q=cmsof?If replaced with other characters, the entire query parameter will become invalid.
  2. Incorrect replacement of URL-encoded characters: If the value of the URL parameter has already been URL-encoded (for example, replacing spaces with%20),You try usingreplaceThe filter to replace one of the original characters (for example, to%20Replace-),It may not match the expected character or cause incorrect secondary encoding.
  3. The danger of an empty 'old string'As mentioned before, ifreplaceThe filter is in the URL string withreplace:",-"This method uses, it will insert hyphens between each character in the URL. Imagine{{ "/search?q=cms"|replace:",-" }}This operation will generate a completely unreadable link, resulting in a 404 error.

How to safely use in practicereplaceFilter processing URL parameters

To avoid the aforementioned risks, when we want to usereplaceA filter should be followed to modify URL parameters, adhering to the following principles and steps:

  1. Clearly replace the target: only modify the parameter "value".replaceThe safest usage of a filter is to apply it to URL parametersValueThis is not a URL structure part or the entire URL string.This means you should first obtain the original value of the parameter, perform the replacement operation, and then reassemble the replaced value into the URL.

  2. URL encode after replacement. After performing the parameter valuereplaceoperation, if the value is to be transmitted as part of a URL, thenMake sureEncode it. AnQi CMS providesurlencodeandiriencodefilters to complete this task.

    • urlencodeThe filter will encode all special characters in the string, including?/&/=It is usually used to encode the entire query string or individual parameter values.
    • iriencodeThe filter is relatively gentle, it will retain some structural characters in the URL (such as:/