我要文件。在档案中,让我们说go.php有这个源代码:
<?php include('folder1/folder2/main.php'); ?>
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1 data-loop="true">This is a Heading</h1>
<p>This is a paragraph.</p>
<span data-loop="true"> tag span</span>
<p>This is a paragraph</p>
<p data-loop="true">This is loop</p>
</body>
</html>
第二个文件main.php里面有这个:
<?php
if(file_get_contents(basename($_SERVER['PHP_SELF'])) === false) {
} else {
$document = file_get_contents(basename($_SERVER['PHP_SELF']));
ltrim( $document, '<?php' );
rtrim( $document, '?>' );
$doc = new DOMDocument();
$doc->loadHTML($document);
$root = $doc->getElementsByTagName('body')->item(0);
$tag = $doc->getElementsByTagName('*');
for($b=0; $tag->length > $b; $b++) {
if( ($tag->item($b) == true) and ($tag->item($b)->hasAttribute('data-loop')) ) {
$tag->item($b)->removeAttribute('data-loop');
$cloned = $tag->item($b)->cloneNode(true);
$root->insertBefore($cloned, $tag->item($b));
}
if($tag->length - 1 == $b) {
}
}
echo $doc->saveHTML();
}
?>
第二个文件遍历文件go.php,添加一些标签并给出结果。但事情是,因此我有go.php的原始源代码和新的源代码。我想只有新的源代码,但我不知道如何从go.php中删除旧的源代码。所以file_put_contents()
不是一个选择。通过所有go.php回显新的源代码会很棒。