Current Location: Home> Latest Articles> Batch replacement of attributes in HTML preprocessor

Batch replacement of attributes in HTML preprocessor

M66 2025-06-02

In modern web development, we often need to modify or replace attributes of a large number of HTML elements, especially in template systems or preprocessors. Using manual modification of properties is not only inefficient, but also prone to errors. This article will introduce how to implement HTML attributes through PHP scripts to improve development efficiency.

Handle HTML with DOMDocument

PHP's own DOMDocument class allows us to easily parse and modify HTML structures. Here is a basic example where we will batch replace the src attributes of all img tags and replace the old domain name with the new m66.net .

 <?php

$html = <<<HTML
<!DOCTYPE html>
<html>
<head>
    <title>Test page</title>
</head>
<body>
    <img src="http://example.com/images/a.jpg" alt="pictureA">
    <img src="http://example.com/images/b.jpg" alt="pictureB">
</body>
</html>
HTML;

// create DOMDocument Example
$doc = new DOMDocument();
// Disabled HTML Error warning
libxml_use_internal_errors(true);
$doc->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
libxml_clear_errors();

// Get all img Label
$imgs = $doc->getElementsByTagName('img');

foreach ($imgs as $img) {
    $src = $img->getAttribute('src');
    // Replace domain name
    $newSrc = str_replace('example.com', 'm66.net', $src);
    $img->setAttribute('src', $newSrc);
}

// Output modified HTML
echo $doc->saveHTML();
?>

Output result:

 <!DOCTYPE html>
<html>
<head>
    <title>Test page</title>
</head>
<body>
    <img src="http://m66.net/images/a.jpg" alt="pictureA">
    <img src="http://m66.net/images/b.jpg" alt="pictureB">
</body>
</html>

Extension: Replace multiple attributes

Suppose we not only want to replace src , but also want to replace the href domain names in all links ( a tags). You can write this way:

 // replace a Label的 href
$links = $doc->getElementsByTagName('a');
foreach ($links as $link) {
    $href = $link->getAttribute('href');
    $newHref = str_replace('example.com', 'm66.net', $href);
    $link->setAttribute('href', $newHref);
}

Batch replacement suggestions

Batch replacement of attributes before using HTML preprocessors or template engines (such as Twig, Blade), you can:

  • Keep the template neat

  • Avoid hard-coded errors

  • Supports multi-environment deployment (such as switching different CDN domain names)

In addition, if you are dealing with static HTML files, you can also use the file to read + DOMDocument:

 $html = file_get_contents('template.html');
$doc->loadHTML($html);

Summarize

Using PHP's DOMDocument class, we can efficiently complete batch replacement of HTML attributes, especially suitable for the preprocessing stage. Batch updates of attributes through scripting not only reduce manual errors, but also make the development process more automated and controllable.

Whether you're working on static templates or dynamic content, this approach is worth a try.

If you need to further implement functions such as path replacement, label reconstruction or conditional replacement, you can combine regular or XPath to perform more advanced operations.