What are the differences between the `urlencode` and `iriencode` filters in URL parameter encoding, and which scenarios are they applicable to?

Calendar 👁️ 85

In website operation, the construction and processing of URL (Uniform Resource Locator) is a fundamental and key task.Especially when dynamically generating links, handling user input as parameters, it is crucial to encode URLs correctly, which can ensure the validity of the links, prevent garbled text and potential security issues.AnQiCMS (AnQiCMS) providedurlencodeandiriencodeThese two filters help us better manage special characters in URLs. Although they are both used for encoding, their application scenarios and processing methods are different.

urlencodeFilter: Strict Percent-encoding

First, let's understandurlencodeFilter. Its main function is to perform standard URL percent-encoding (percent-encoding).This means, any character not in the URL safe character set (usually letters, numbers, and a few punctuation symbols like- . _ ~Characters within the ) range will be converted to%xx(wherexxThe hexadecimal ASCII value of the character).

This encoding method is very strict and comprehensive, its goal is to ensure that all characters in the URL can be safely transmitted and parsed by network protocols, avoiding ambiguity. For example, spaces, Chinese characters, and other characters cannot be included directly in the URL.&Symbol (because it is a parameter separator),=Symbols (because they are key-value separators) and so on. If these characters appear in a URL without encoding, it may cause links to break, parameter parsing errors, or even security vulnerabilities.

Application scenarios:

  • Encode the entire URL or query string:When you need to pass a complete URL string as a parameter to another URL (for example, during redirection or tracking) or to encode the entire query string to ensure its integrity, urlencodeit is an ideal choice.
  • Encode a single query parameter value:The most common scenario is that users enter Chinese characters, including spaces or special symbols, in the search box. To safely pass these keywords as URL parameters, you need tourlencode.
    • Example:Suppose the user searches for "Anqi CMS official website", if entered directly into the URL, it may cause problems.http://example.com/search?q=安企 CMS 官网UseurlencodeAfter:http://example.com/search?q=%E5%AE%89%E4%BC%81%20CMS%20%E5%AE%98%E7%BD%91(Here, the%20representing a space,%E5%AE%89etc. represent Chinese characters)
  • Ensure that all unsafe characters are processed:Use when you are unsure about the character set of the input content:urlencodeCan provide the highest level of security, avoiding any unexpected characters that may cause the URL to fail.

Using in Anqi CMS template,urlencodeThe way the filter works is as follows:

{{ "http://www.example.org/foo?a=b&c=d"|urlencode }}
{# 输出: http%3A%2F%2Fwww.example.org%2Ffoo%3Fa%3Db%26c%3Dd #}

{{ "我的搜索关键词"|urlencode }}
{# 输出: %E6%88%91%E7%9A%84%E6%90%9C%E7%B4%A2%E5%85%B3%E9%94%AE%E8%AF%8D #}

iriencodeFilter: structure-preserving internationalized encoding

iriencodeThe filter provides a relatively lenient encoding method, which is mainly used to process IRI (Internationalized Resource Identifier, an internationalized resource identifier).IRI is a superset of URL, allowing more Unicode characters in identifiers to support various languages globally.iriencodeWhen encoding, some structural special characters in the URL are retained, and only other characters that need to be encoded are processed.

According to the Anqi CMS document instructions,iriencodewill be retained/#%[]=:;$&()+,!?*@'~The original appearance of these characters, while escaping the other characters for URL parameters.This means it will be able to identify the structure of URLs more intelligently, avoiding encoding of characters that act as separators or have specific meanings, thereby maintaining the readability and structural integrity of URLs.

Application scenarios:

  • Encode the segments in the URL path:When the URL path contains Chinese or special characters, you may want to use path delimiters/to maintain the path structure.
    • Example: http://example.com/产品分类/电子产品Useiriencodemay include产品分类and电子产品Chinese characters are encoded but retained/:http://example.com/%E4%BA%A7%E5%93%81%E5%88%86%E7%B1%BB/%E7%94%B5%E5%AD%90%E4%BA%A7%E5%93%81
  • Internationalized domain names or paths:If your website uses Chinese or other non-ASCII character domain names or paths (such as.公司or/新闻标题)iriencodeIt is more suitable for handling these internationalized elements as it is designed to be compatible with a wider character set.
  • It is necessary to retain the specific URL structure characters:In the construction of some complex URLs, you may explicitly know that certain characters (such as:/=/&etc.) are part of the URL structure and should not be encoded.iriencodeIt can encode other unsafe characters without destroying these structural characters.
  • The scenarios of HTML entity encoding in a specific environment:Although the name isiriencode, but the examples given in the document"?foo=123&bar=yes"|iriencodeoutput?foo=123&bar=yesThis indicates that in some cases, it may also perform HTML entity encoding (such as&to&). If your final output is directly embedded in HTML and you need to process&Characters such as these are encoded as HTML entities rather than URL percent encoding, which may be a hidden feature or specific behavior. But in typical URL encoding scenarios,&Generally not converted&. It is recommended to verify the actual output effect when using it

In AnQi CMS

Related articles

How to safely truncate text with HTML tags using `truncatechars_html` and `truncatewords_html` filters to prevent structure damage?

In website content operation, we often need to display the summary or part of the content in different scenarios, such as on the list page, search results, or related recommendations.This is when you need to truncate the content.If the content is plain text, a simple character or word cutter can handle the task well.However, when the content is rich in HTML tags, the problem becomes complex: directly cutting may cause the HTML tags to be truncated, thereby destroying the page structure, causing display errors, and even affecting the user experience.

2025-11-08

How to handle truncation and add an ellipsis when truncating text with `truncatechars` and `truncatewords` filters, including HTML content?

In the increasingly fragmented information consumption era, the way websites present content directly affects user experience.Whether it is the abstract of an article list, the preview of product introduction, or various information stream cards, we need to convey the core information within a limited space while maintaining the page's neatness and uniformity of layout.This introduces the need for text truncation. AnQiCMS, as a fully functional content management system, understands this well and provides us with a powerful text truncation filter, especially its intelligent processing capabilities for HTML content, making content operations more relaxed.

2025-11-08

How do the `trim`, `trimLeft`, and `trimRight` filters remove spaces or specific characters from the beginning or end of a string?

In website operation and content management, we often encounter the need for string processing, especially in data entry, content display, or API interaction.For example, the user may have mistakenly entered multiple spaces before and after the text box, or some data may have been sourced with unnecessary specific characters.These seemingly minor issues may affect the layout and aesthetics of the content, even causing functional abnormalities.The AnQi CMS template engine provides a series of powerful filters to help us easily deal with these string cleaning tasks.Today, let's get to know the three very practical filters: `trim`

2025-11-08

How to get the thumbnail version of the image according to the image address using the `thumb` filter in the Anqi CMS template?

In website content management, the effective use of images is crucial for improving user experience and page loading speed.AnQiCMS deeply understands this, providing powerful image processing functions, among which the `thumb` filter is a tool that helps us easily obtain the thumbnail version of images.Why thumbnails are so important?Imagine if all the images on your website loaded in their original high-definition large size, how heavy the page would become!

2025-11-08

How `urlize` and `urlizetrunc` filters automatically convert URLs and email addresses in text to clickable hyperlinks?

How to efficiently and beautifully convert text containing URLs and email addresses into clickable hyperlinks when managing website content in AnQi CMS, is a concern for many content operators.Manually adding the `<a>` tag to each URL or email is not only time-consuming and labor-intensive but also prone to errors.Luckily, AnQi CMS provides two very practical template filters——`urlize` and `urlizetrunc`, which can automatically complete this task and greatly improve the efficiency and user experience of content publishing.### `urlize`

2025-11-08

How to calculate the number of words in a string with the `wordcount` filter in Anqi CMS template?

When processing text content in the AnQi CMS template, we often need to perform various operations on strings, such as counting words, cutting content, and so on.Among them, calculating the number of words in a string is a common requirement, which is very useful in content analysis, SEO optimization, and even estimating the reading time of an article.AnQi CMS provides a simple and powerful tool——`wordcount` filter.### `wordcount` filter: Core functionality and purpose The `wordcount` filter is specifically used to count the number of words in a given string

2025-11-08

How does the `wordwrap` filter automatically wrap long text to a specified length

In the daily operation of websites, we often encounter such situations: the content of the article is too long, or the text extracted from other places has not been formatted, resulting in the text content exceeding the container width when displayed on the web page, destroying the overall layout and seriously affecting the reading experience of users.Especially on mobile devices, long lines of text are more difficult to read.To solve this problem, Anqi CMS provides a very practical tool - the `wordwrap` filter, which can help us automatically wrap long text to the specified length, thereby optimizing the display effect of the content

2025-11-08

How does AnQiCMS ensure that website content displays perfectly on different devices?

In the digital age, website content can seamlessly adapt to various devices, from large desktop monitors to small smartphone screens, and is no longer an option, but the cornerstone of success.The user expects to have a smooth, beautiful, and fully functional experience regardless of which device is accessed.AnQiCMS as a product designed for efficient content management, provides a series of powerful and flexible features to ensure that your website displays content perfectly on any screen, making it shine on any screen.### AnQiCMS's multi-dimensional adaptive strategy AnQiCMS

2025-11-08