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

In website content operation, we often need to handle text content that includes HTML tags.Sometimes it needs to be converted to plain text, and sometimes it is desired to retain some key elements while removing redundant formats, such as images and links.striptagsFilter is a commonly used one. Then, whatstriptagsCan filter retain images or links in the content while removing HTML tags?

In short,striptagsThe design purpose of the filter is to completely remove all HTML tags, so it will not retain images or links.If you want to remove the format tags while keeping 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>This text format label, is<div>/<p>This content block label, even<img />and<a href="...">This image and link label,striptagsIt will completely remove them, leaving only the plain text content inside the tags.

Let's take a look at an example to understand this intuitively:

Original HTML content:

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

UsestriptagsProcessed by the filter:

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

As you can see,striptagsNot only removedp/band other formatting tags, along with image tags<img>And link tags<a>Also cleared together, only pure text information remained.This filter is typically used to generate article summaries, SEO descriptions (meta description), or in certain scenarios where it is necessary to ensure that the content does not contain any HTML structure to prevent potential XSS attacks or to display a uniform format.

When you need to keep images and links:removetagsFilter

If you wish to more finely control the removal of tags, for example, only to remove bold, italic, paragraph, and other formatting tags, while keeping images and links intact,removetagsFilter is the tool you need.

Withstriptagsis different 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 parameters. As long as you do notimgandaThe label is included in the removal list, and these elements can be preserved intact.

Usage:

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

Example:Assuming we want to removepandbthe label, 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"Processed by the filter:

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

In this example, we specify that we have removedpandbTag, whileimgandaThe label and its content are retained. This is very useful when displaying rich text content on the front end while also wanting to unify its formatting style.

Choose the appropriate tool: when to usestriptagsWhen to useremovetags

After understanding the differences between these two filters, choosing the appropriate tool becomes clear and straightforward:

  • Usestriptags:When you need to convert HTML content to plain text completely, without including any HTML structure, for example, generating websites'<meta name="description">Content, or in some scenarios where strict security filtering is required,striptagsis a simple and effective choice.
  • Useremovetags:When your goal is to remove specific formatting tags while retaining 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 explicitly specify the format tags to be removed. Properly using these two filters will enable better management and presentation of your website content.


Common 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 (such as<script>)。But in practical applications, preventing XSS attacks requires a more comprehensive security strategy, including input validation, Content Security Policy (CSP), etc., and should not rely on it alone.striptagsAs the only protective measure. 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 nested tags?Yes,removetagsCan handle nested tags. For example, if you have a<div><p><span>Hello</span></p></div>structure and useremovetags:"span"so that<span>tags will be removed, contentHellowill be displayed directly in<p>the tag, and will not be invalidated due to nesting.

3. BesidesimgandaLabel, what other HTML tags are commonly used?removetagsWhat needs to be paid special attention to when preserving?In most content display scenarios, except for images (<img>) and link (",<a>),usually it is also necessary 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>English for embedding videos and audio.
  • Citation: <blockquote>English for referencing external content.
  • List: <ul>/<ol>/<li>English for unordered or ordered lists.
  • Semantic tags: <pre>(Pre-formatted text,)<code>Blocks (code blocks) etc., used to preserve the format or semantics of specific content.These tags usually contain important content structure or media information, and if they are mistakenly removed, they may seriously affect the integrity and readability of the content.