In website content management, we often encounter scenarios where we need to uniformly adjust or batch modify specific attribute values in a large amount of HTML content. For example, you may need to update the height attributes of all images, or add specific attributes to some links.relAttributes, even adjusting the styles of certain tags generated by the rich text editor.AnQiCMS provides flexible tools to meet these needs, among which the batch replacement feature in the background is a direct tool for modifying stored content, while the template filter can dynamically convert content at the time of output. Combined, both can efficiently manage and optimize website content.

First part: Use the batch replacement feature of AnQiCMS backend to directly modify the stored content

When your goal is to permanently modify the specific attribute value of the stored HTML content in the database, the 'Document Keyword Replacement' feature of AnQiCMS backend is your preferred tool.This feature allows you to define replacement rules and apply them to all site content, supporting both regular text replacement and advanced regular expression replacement. The power of regular expressions is particularly prominent when it comes to precise modification of HTML attribute values.

  1. Function entryIn AnQiCMS backend, navigate to "Content Management" -> "Document Management".Above the document list page, you will see the option 'Document Keyword Replacement'.Click to enter the feature interface.

  2. Configure replacement rulesHere, you need to enter the "search word" and "replacement word".

    • Normal text replacementIf you just want to replace a fixed attribute value (such aswidth="100") with another fixed value (such aswidth="150"It can be entered directly.
    • Regular expression replacementThis is the core of batch modifying HTML specific attribute values. Regular expressions allow you to match complex patterns, thereby accurately locating and modifying attribute values.
      • Syntax hint【en】Regular expression rules need to be used{at the beginning, followed by}and end. For example,{[0-9]+}Can match consecutive numbers.By combining capturing groups and backreferences, you can achieve very fine-grained modifications.
  3. Practice example: Modify the image width attributeSuppose that all images in the article content of your website have usedwidth="xxx"Such inline attributes are used to define width, now you want to统一 modify the width attributes of all imagesmax-width: 100%; height: auto;to achieve a better responsive layout, and remove the oldwidthproperties.

    • search term (regular expression):(<img[^>]*?)\swidth="\d+"([^>]*?>)
      • The meaning of this regular expression is:
        • (<img[^>]*?): Capture<img>tag start, as well as any non->character following it, until it encounterswidththe front of the attribute. This is the first capture group.
        • \swidth="\d+"Match a space,width=" followed by any number(\d+) and".
        • ([^>]*?>): Capturewidthto the end of>the tag. This is the second capture group.
    • The word to be replaced:$1 style="max-width: 100%; height: auto;"$2
      • $1and$2This refers to the first and second capture groups in the regular expression.
      • This means we will keep the other parts of the image tag, insert the newstyleattribute, and automatically remove the old one.widthproperties.
    • Application ReplacementSelect the range of content you want to apply the replacement to (such as all articles, products, etc.), then click Execute. The system will traverse all content and perform batch replacement according to the rules you define.

Important NoticeUse regular expressions for batch replacement with caution, and it is recommended to validate in a test environment first, and back up the database in advance to prevent irreversible content damage due to accidental operations.

Second part: Combine AnQiCMS filter to dynamically transform and optimize HTML content display

The AnQiCMS template filter provides the ability to dynamically transform content when outputting to the front-end page.This means that these modifications will not directly change the original HTML content stored in the database, but will take effect immediately when the user browses the page, which is very suitable for optimizations and adjustments at the display level.

Filters are usually applied to the output of content variables, with a basic format of{{ 变量名|过滤器名称:参数 }}.

  1. replaceFilter: Text-level property value replacementAlthough batch replacement in the background is more powerful, in some cases, you may only want to replace in the front end.