【问题标题】:Inserting a div into a HTML file using PHP使用 PHP 将 div 插入 HTML 文件
【发布时间】:2016-07-13 14:43:23
【问题描述】:

使用 PHP,我尝试在解析的 HTML 中添加一个 div,插入位置取决于现有标签的 ID。

我得到了一个基本上是只读的生成的 HTML 文件(我无法控制)。 我的工作是获取那个 HTML 文件,添加一些 div(按钮和其他东西),然后重新发送回去。

这是有点棘手的地方:我需要在哪里添加 div(我用按钮和东西添加的那些)取决于可以在 HTML 文件中多次出现的 div ID。

为了让我添加的 div 正常工作,它必须添加到已标记的可识别 div 的父 div 之前。

简单地说:

... some html

<--- where i want to put our lovely div

<div> // no  tags no info just a simple div

    <div id="myId">
        ... some html
    </div>

</div>

... some html

【问题讨论】:

  • 举个例子就好了...
  • 嗯,我试图找到一种在 html 文件中搜索标签的方法,获取父标签并在它之前插入一串代码。获取父标签是困扰我的一个
  • 为什么不构造一个 if/else 语句? if ($divID === 'abc') { insert ... } else { 做点什么 }
  • Probelm 是 html 文件本身可能有多个变体和大量 div 与 myTag 发生我对 html 的控制为 0 我所能做的就是解析它,因为它是添加元素(我的问题现在)
  • @Z3R0 - 我明白,我在下面提供的比临时解决方案要好一些,如果你花时间学习 DOMDocument,它会是一个很好的工具来做各种其他的乐趣html和php的东西:-)

标签: javascript php html


【解决方案1】:

您绝对最好的选择,imo 将使用 PHP 的原生 DOMDocument:http://php.net/manual/en/class.domdocument.php

这方面的学习曲线相当长,所以我想出了一些可以让你朝着正确方向前进的东西——如果不能完全提供解决方案的话。我在这里逐行包含 cmets 来解释每个步骤:

// the filename you want to parse
$filename = './test.html';

// an array of replacement html snippets and the id of the child element.
// html will be inserted before parent of each child with a matching ID as you described
$replacements = [
    [
        'id'     => 'myId',
        'insert' => '<button>Insert before parent of #myId</button>'
    ],
    [
        'id'     => 'myId2',
        'insert' => '<button>Insert before parent of#myId2</button>'
    ]
];

// instantiate DOMDocument and read the html file
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTMLFile('./test.html');

// get an array of all dom elements
$elements = $dom->getElementsByTagName('*');

// iterate through dom elements
foreach($elements as $element) {

    // check if this element has an 'id' attribute
    if ($element->hasAttribute('id')) {

        // iterate through replacement array
        foreach ($replacements as $i => $replacement) {

            // if element's id is a match then add this node to our array
            if ($element->getAttribute('id') == $replacement['id']) {
                $replacements[$i]['nodes'][] = $element;
            }
        }
    }
}

// iterate through replacements again
foreach ($replacements as $replacement) {

    // iterate through nodes we found which matched
    foreach ($replacement['nodes'] as $node) {

        // create a DOMDocument node from an html string
        $html = $dom->createDocumentFragment();
        $html->appendXML($replacement['insert']);

        // insert this node before parent
        $node->parentNode->parentNode->insertBefore($html,$node->parentNode);
    }
}

// output the revised html
echo $dom->saveHTML();

// note - if your html doesn't have <html> and <body> tags they will be automatically added by DOMDOcument
// you can work around this and get only body innerhtml with something like this
echo str_replace(['<body>','</body>'],'',$dom->saveHTML($dom->getElementsByTagName('body')->item(0)));

我使用以下名为 test.html 的 html 创建了一个测试文档。我故意使用myId 两次来证明这实际上将匹配每个元素,而不管其有效性:

<div> // no  tags no info just a simple div
    <div id="myId">
        ... some html
    </div>
</div>

<div> // no  tags no info just a simple div
    <div id="myId2">
        ... some html
    </div>
</div>

<div> // no  tags no info just a simple div
    <div id="myId">
        ... some html
    </div>
</div>

上面的php代码输出如下:

<button>Insert before parent of #myId</button><div> // no  tags no info just a simple div
    <div id="myId">
        ... some html
    </div>
</div>

<button>Insert before parent of #myId2</button><div> // no  tags no info just a simple div
    <div id="myId2">
        ... some html
    </div>
</div>

<button>Insert before parent of #myId</button><div> // no  tags no info just a simple div
    <div id="myId">
        ... some html
    </div>
</div>

【讨论】:

    猜你喜欢
    • 2017-01-30
    • 1970-01-01
    • 2020-07-27
    • 2010-10-09
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多