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

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.