Can the `striptags` filter retain images or links in HTML content, while removing only the formatting tags?

Calendar 👁️ 77

In website content operation, we often need to handle text content containing HTML tags.Sometimes it needs to be converted to plain text, and sometimes it is desirable to retain some key elements while removing redundant formats, such as images and links.AnQiCMS's template engine provides a rich set of filters to help us handle such requirements, among whichstriptagsThe filter is a commonly used one. So, thisstriptagsCan the filter retain images or links in the content when removing HTML tags?

In short,striptagsThe design of the filter is to completely remove all HTML tags, therefore it will not retain images or links.If you want to remove format tags while retaining these key elements, AnQiCMS provides another more flexible tool.

Deep understandingstriptagsFilter

As the name suggests,striptagsThe core function of the filter is to strip all HTML, XML, and even PHP tags. This means that regardless of what is contained in your content,<b>/<i>Such text format tags, or<div>/<p>Such content block tags, even<img />and<a href="...">Such image and link tags,striptagsThey will be completely removed, leaving only the pure text content inside the tags.

Let's look at an example to understand this visually:

Original HTML content:

<p>这是一段<b>重要的</b>文本,包含一张图片:<img src="image.jpg" alt="示例图片">,以及一个<a href="https://anqicms.com">链接</a>。</p>

UsestriptagsAfter the filter:

这是一段重要的文本,包含一张图片:,以及一个链接。

As you can see,striptagsNot only removed:p/bFormatting tags, including image tags<img>and link tags<a>Also remove it, only leaving pure text information. This filter is usually used to generate article summaries, SEO descriptions (meta description), or in some scenarios where it is necessary to ensure that the content does not contain any HTML structure to prevent potential XSS attacks or to unify the display format.

When you need to keep images and links:removetagsFilter

If you want to remove tags more precisely, for example, only to remove bold, italic, paragraph and other format tags, while keeping images and links intact, thenremovetagsThe filter is the tool you need.

withstriptagsDifferent from the 'one-size-fits-all' approach,removetagsThe filter allows you to explicitly specify the HTML tags to be removed. It accepts a list of tag names separated by commas as an argument. As long as you do notimgandaLabels included in the removal list, these elements can be preserved intact.

Usage:

{{ obj|removetags:"标签1,标签2,标签3" }}

Example:Assuming we want to removepandblabels, but preserve images and links.

Original HTML content:

<p>这是一段<b>重要的</b>文本,包含一张图片:<img src="image.jpg" alt="示例图片">,以及一个<a href="https://anqicms.com">链接</a>。</p>

Useremovetags:"p,b"After the filter:

这是一段重要的文本,包含一张图片:<img src="image.jpg" alt="示例图片">,以及一个<a href="https://anqicms.com">链接</a>。

In this example, we specify the removal ofpandbtags, whileimgandaLabels and content are retained. This is very useful when you need to display rich text content on the front end but also want to unify its formatting style.

Choose the appropriate tool: when to usestriptags, when should it be usedremovetags

Understood the difference between these two filters, choosing the appropriate tool becomes clear:

  • Usestriptags:When you need to convert HTML content into plain text completely, without including any HTML structure, for example, generating websites'<meta name="description">Content, or in some cases where strict security filtering is required,striptagsis a simple and effective choice.
  • Useremovetags:When your goal is to remove specific formatting tags, but retain other important HTML elements such as images, links, and tables, removetagsProvided greater flexibility and control. You can precisely decide which tags need to be cleaned and which need to be retained.

Summary

In summary, AnQiCMS'sstriptagsThe filter will remove all HTML tags, including images and links. If you want to keep images and links while cleaning the format, please useremovetagsFilter, and clearly specify the format tags to be removed. Properly using these two filters will help you better manage and display your website content.


Frequently Asked Questions (FAQ)

1.striptagsCan the filter be used to prevent XSS attacks? striptagsIt can indeed remove all HTML tags, thereby preventing the injection of malicious script tags to some extent (such as<script>)。But in actual application, preventing XSS attacks requires a comprehensive security strategy, including input validation, Content Security Policy (CSP), etc., and should not rely onstriptagsAs the only protection means. It is recommended to perform strict input filtering and output escaping on the backend for user-generated content.

2.removetagsCan the specified tag be removed from the nested tags in other tags?Yes,removetagsCan handle nested tags. For example, if you have a<div><p><span>Hello</span></p></div>structure and useremovetags:"span"then<span>tags will be removed, the contentHellowill be displayed directly in<p>the tag, and will not fail due to nesting.

3. BesidesimgandaTags, what other HTML tags are commonly usedremovetagsshould be paid special attention to when preserving?In most content display scenarios, in addition to images (<img>) and link (<a>) Usually, it also needs to consider retaining the following tags and their contents:

  • Structured content: <table>(Table),<thead>/<tbody>/<tr>/<td>/<th>Etc., for displaying data.
  • Media elements: <video>/<audio>used for embedding video and audio.
  • Citation: <blockquote>used for referencing external content.
  • List: <ul>/<ol>/<li>used for unordered or ordered lists.
  • Semantic tags: <pre>(formatted text),<code>(code block) etc., used to preserve the format or semantics of specific content.These tags usually carry important content structure or media information, and if they are mistakenly removed, it may seriously affect the completeness and readability of the content.

Related articles

How to convert Markdown to HTML in a template and then remove the tags from the generated HTML?

In the AnQiCMS template, when handling Markdown content, it is often necessary to first convert it to HTML, and then further filter or remove specific HTML tags on this basis.This is particularly useful when creating content summaries, generating plain text previews, or implementing specific layout controls.The template tags and filters provided by AnQi CMS can help us complete these operations efficiently and flexibly.

2025-11-08

Does AnQiCMS provide a feature to batch remove all HTML tags from all articles under a specific content model?

In website content operation, we often encounter situations where we need to uniformly process a large number of articles, such as for content standardization, SEO optimization needs, or preparing to export content to other platforms, we need to remove the HTML tags from the articles.AnQiCMS (AnQiCMS) is a powerful content management system that provides an efficient way to solve such problems.The Anqi CMS indeed has the ability to remove all HTML tags from all articles under the specified content model in bulk.

2025-11-08

How to remove HTML tags in AnQiCMS template based on conditions?

In the development and maintenance of website templates, we often encounter such needs: to determine whether a certain HTML element is displayed on the page based on specific data conditions.For example, an article only displays the image tag when there is a thumbnail or when a custom field has content, and renders the corresponding block.This "on-demand display" mechanism not only makes the template code cleaner, avoids the appearance of empty tags or unnecessary visual elements on the page, but also significantly improves user experience and page loading efficiency.

2025-11-08

What are the built-in cleanup tools of AnQiCMS when processing HTML content from external sources?

When processing HTML content collected from external sources, AnQiCMS (AnQiCMS) provides a series of built-in cleaning tools and strategies to help users efficiently manage and optimize content, ensuring the quality, safety, and display effect of website content.These features provide comprehensive support from the automated processing before the content is stored to the refined control during content display.

2025-11-08

How to remove whitespace tags from HTML without affecting content display in AnQiCMS?

Manage website content in Anqi CMS, we often pay attention to details, such as the blank tags in HTML code.These seemingly harmless empty tags can sometimes affect the rendering of the page, and even cause slight interference to search engine optimization (SEO).Although Anqi CMS does not have a direct function to "one-click clear blank HTML tags", we can cleverly utilize its powerful content management tools to achieve this goal without affecting the actual display of content.Understanding the trouble with empty tags Empty HTML tags usually refer to `<p></p>`

2025-11-08

How to use a filter to convert HTML content to plain text for word count or SEO analysis?

During the operation of a website, we often need to perform various analyses on the content we publish, among which word count and SEO analysis are crucial.However, rich text editors in content management systems (CMS) often add a large number of HTML tags to text. Although these tags provide rich visual effects when rendered on the front-end page, they often cause interference when performing word counts or when pure text is needed for SEO analysis.AnQiCMS (AnQiCMS) is an efficient and flexible content management system that fully considers the needs of users

2025-11-08

What Markdown syntax features does the `render` parameter of AnQiCMS support when converting Markdown to HTML?

In AnQiCMS, the flexibility of content creation has always been our focus.With the introduction of the Markdown editor, content publishing has become more efficient and convenient.When we enter Markdown format content through the background editor, the system defaults to converting it into beautiful HTML for display on the website front end.The core of this conversion process is the important parameter `render` that we may use in template tags.

2025-11-08

How to prevent external JavaScript code from injecting through HTML when AnQiCMS template is rendered?

In website content management, security is always a core consideration.Especially in the template rendering phase, if not prevented, maliciously designed external JavaScript code may be injected through HTML, leading to cross-site scripting attacks (XSS), which can further steal user data, alter page content, or even hijack sessions, posing huge risks to websites and users.Understanding how AnQiCMS handles security issues in template rendering and mastering the corresponding preventive strategies is crucial for maintaining the healthy operation of the website.### AnQiCMS template engine is the cornerstone of security

2025-11-08