使用PHP的DomDocument appendChild时保持换行 [英] Keeping line breaks when using PHP's DomDocument appendChild

查看:100
本文介绍了使用PHP的DomDocument appendChild时保持换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP中的DOMDocument来添加/解析HTML文档中的内容.据我所知,将formOutput设置为true并将keepWhiteSpace设置为false应该可以使制表符和换行符保持顺序,但是对于新创建或附加的节点似乎不是如此.

I'm trying to use the DOMDocument in PHP to add/parse things in an HTML document. From what I could read, setting the formOutput to true and preserveWhiteSpace to false should keep the tabs and newlines in order, but it doesn't seem like it is for newly created or appended nodes.

代码如下:

$dom = new \DOMDocument;
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
$dom->loadHTMLFile($htmlsource);
$tables = $dom->getElementsByTagName('table');
foreach($tables as $table)
{
    $table->setAttribute('class', 'tborder');
    $div = $dom->createElement('div');
    $div->setAttribute('class', 'm2x');
    $table->parentNode->insertBefore($div, $table);
    $div->appendChild($table);
}
$dom->saveHTMLFile($html)

这是HTML的样子:

<table>
    <tr>
        <td></td>
    </tr>
</table>

这就是我想要的:

<div class="m2x">
    <table class="tborder">
        <tr>
            <td></td>
        </tr>
    </table>
</div>

这就是我得到的:

<div class="m2x"><table class="tborder"><tr>
<td></td>
        </tr></table></div>

我在做错什么吗?我尝试过用多种方式来搜索,但没有运气.

Is there something I'm doing wrong? I've tried googling this as many different ways as I could thing of with no luck.

推荐答案

不幸的是,您可能需要编写一个使输出缩进所需功能的函数.我做了一个小功能,您可能会觉得有用.

Unfortunately, you might need to write a function that indents the output how you want it. I made a little function you might find helpful.

function indentContent($content, $tab="\t")
{               

        // add marker linefeeds to aid the pretty-tokeniser (adds a linefeed between all tag-end boundaries)
        $content = preg_replace('/(>)(<)(\/*)/', "$1\n$2$3", $content);

        // now indent the tags
        $token = strtok($content, "\n");
        $result = ''; // holds formatted version as it is built
        $pad = 0; // initial indent
        $matches = array(); // returns from preg_matches()

        // scan each line and adjust indent based on opening/closing tags
        while ($token !== false) 
        {
                $token = trim($token);
                // test for the various tag states

                // 1. open and closing tags on same line - no change
                if (preg_match('/.+<\/\w[^>]*>$/', $token, $matches)) $indent=0;
                // 2. closing tag - outdent now
                elseif (preg_match('/^<\/\w/', $token, $matches))
                {
                        $pad--;
                        if($indent>0) $indent=0;
                }
                // 3. opening tag - don't pad this one, only subsequent tags
                elseif (preg_match('/^<\w[^>]*[^\/]>.*$/', $token, $matches)) $indent=1;
                // 4. no indentation needed
                else $indent = 0;

                // pad the line with the required number of leading spaces
                $line = str_pad($token, strlen($token)+$pad, $tab, STR_PAD_LEFT);
                $result .= $line."\n"; // add to the cumulative result, with linefeed
                $token = strtok("\n"); // get the next token
                $pad += $indent; // update the pad size for subsequent lines    
        }       

        return $result;
}

indentContent($ dom-> saveHTML())将返回:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
    <body>
        <div class="m2x">
            <table class="tborder">
                <tr>
                    <td>
                    </td>
                </tr>
            </table>
        </div>
    </body>
</html>

我从这个.

这篇关于使用PHP的DomDocument appendChild时保持换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆