我正在使用PHP简单的HTML DOM解析器'上传文件(在本例中为zip),打开索引文件,并允许更改链接。更新链接后,将覆盖索引文件并压缩下载。
这一切都运行正常,我遇到的唯一问题是保存更新的索引文件后,它会丢失文件中的所有空白区域。因此,如果您在文本编辑器中打开文件,则不再使用任何结构进行布局,因此很难正常读取/编辑。
有没有办法可以保留这个空白区域?
"上传"获取上传文件的文件,将其显示在iFrame中,其下方的字段显示现有链接,以及更新它们的字段:
<div class="output">
<form action="download.php" id="urlform" method="POST">
<?php
//Traverse DOM of iFrame content, output existing href tags
$html = file_get_html($_SESSION['index']);
$i = 1;
$inc = 1;
foreach($html -> find('a') as $element) : ?>
<!-- Ignore untagged links -->
<?php if (strpos($element -> href, "utm") !== false) {
$element -> name = $inc; ?>
<?php if ($i % 2 === 0) {
echo '<div class="alternate">';
} else {
echo '<div class="standard">';
} ?>
<!-- Add name attr to anchor tags for reference -->
<div class="assign">
<p><?php echo $element -> name; ?></p>
</div>
<div class="content">
<h3>Existing tag:</h3>
<p><?php echo $element -> href; ?></p>
<h3>New tag:</h3>
<input type="textarea" name="urltag_<?php echo $inc; ?>">
</div>
</div>
<?php $i++; $inc++; } ?>
<?php endforeach; ?>
<input type="submit" value="Update eShot">
</form>
</div>
下载文件,显示带有更新索引文件和下载的iFrame:
<?php
//Open index.html of zip and replace href with new tags
$newhtml = file_get_html($_SESSION['index']);
$inc = 1;
foreach($newhtml -> find('a') as $url) {
if (strpos($url -> href, "utm") !== false) {
$url -> href = $_POST["urltag_" . $inc];
$inc++;
};
}
//Store changes in new index file
file_put_contents($_SESSION['index'], $newhtml);
$folder = $_SESSION['new_directory'];
if (isset($_SESSION['portal'])){
$zipname = $_SESSION['name'] . " " . $_SESSION['portal'] . " " . date('dmy') . ".zip";
} else {
$zipname = $_SESSION['name'] . " " . date('dmy') . ".zip";
}
class FlxZipArchive extends ZipArchive {
public function addDir($folder, $zipname) {
$this->addEmptyDir($zipname);
$this->addDirDo($folder, $zipname);
}
private function addDirDo($folder, $zipname) {
$zipname .= '/';
$folder .= '/';
$dir = opendir ($folder);
while ($file = readdir($dir))
{
if ($file == '.' || $file == '..') continue;
$do = (filetype( $folder . $file) == 'dir') ? 'addDir' : 'addFile';
$this->$do($folder . $file, $zipname . $file);
}
}
}
$za = new FlxZipArchive;
$res = $za->open($folder . "/" . $zipname, ZipArchive::CREATE);
if($res === TRUE)
{
$za->addDir($folder, basename($folder));
$za->close();
}
else { echo 'Could not create a zip archive';}
?>
<div class="thankyou">
<p>Thank you. Please check your new links in the eShot below:</p>
</div>
<iframe class="eshotwrapper" src="<?php echo $_SESSION['index'] ?>"></iframe>
<div class="downloadlink">
<p>Once you have confirmed your tagged links are correct, please click the link below to download your new eShot: <br>
(Right-click and select "Save as..." to choose a save location)</p>
<a href="<?php echo $_SESSION['new_directory'] . "/" . $zipname ?>"><button type="button">Download</button></a>
</div>
</div>
谢谢!
答案 0 :(得分:0)
如果将HTML解析为DOM树,则根本不会在该结构中维护原始格式。但是,您可以通过库运行输出以进行适当的缩进并美化输出。
如果您想完全按原样维护输出,那么使用正则表达式来查找和包装标记会有更多成功。