使用HTML Simple Dom Parser保存后,在文件中保留空白


Keep white space in files after saving with HTML Simple Dom Parser

我使用"PHP Simple HTML DOM Parser"上传文件(在本例中为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>

谢谢!

如果将HTML解析为DOM树,则根本不会在该结构中维护原始格式。但是,您可以通过库运行输出,以放入适当的缩进并美化输出。

如果您想将输出保持原样,那么使用正则表达式查找和包装标记会取得更大的成功。