最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to verify and prevent unintended changes when modifying specific parts of an HTML document using PHP DOMDocument? - Stack Ov

programmeradmin2浏览0评论

I got a task to update specific parts of HTML elements across all pages of a web project. Given the size and complexity of the project, I prefer using DOM manipulation over methods like regex to ensure more structured changes. However, as I'm a beginner in DOM manipulation and am handling real data, I have some concerns.

My main concern is ensuring that only the intended parts of the HTML are modified, while other parts of the document remain unchanged. I want to verify and prevent if any unintended changes are made implicitly by the DOM parser during manipulation. This is crucial for data integrity and preventing accidental modifications in the rest of the document.

So far, I've also noticed that PHP's DOMDocument class converts all HTML tag names to lowercase by default when parsing HTML, which is fine.

$html = '
<!DOCTYPE html>
<html>
<head>
    <title>My Web Page</title>
</head>
<body>
    "This Line Non Tag TextNode Before header"
    <h1 id="main-header" class="header">Main Header</h1>
    <div class="content">
        <h1 class="nested-header">Nested Header 1</h1>
        <p>Some content under nested header.</p>
        <a href=";>Example Link</a>
    </div>
    <h2 id="sub-header-1">Sub Header 1</h2>
    <h3>Sub Header 2</h3>
    <h1>Another Main Header</h1>
    <p>This is a paragraph outside the header tags.</p>
    <footer>Some footer text with <span>inline text</span>.</footer>
</body>
</html>';

// Load HTML content
$doc = new DOMDocument();
@$doc->loadHTML($html);

// Create a TextNode to be inserted before the first <h1>
$textNode = $doc->createTextNode('This Line Non Tag TextNode Before header');

// Insert the TextNode before the first <h1> element
$firstHeader = $doc->getElementsByTagName('h1')->item(0);
if ($firstHeader) {
    $firstHeader->parentNode->insertBefore($textNode, $firstHeader);
}

// Remove the first <h1> element
if ($firstHeader) {
    $firstHeader->parentNode->removeChild($firstHeader);
}

// Check if other headers or elements are modified unintentionally
$modifiedHtml = $doc->saveHTML();
echo $modifiedHtml;

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论