从HTML中删除当前节点并使用DOMDocument php获取最终的HTML

时间:2017-10-24 10:43:27

标签: php html-parsing domdocument

我有一个类似下面的HTML:

<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>ABC</td>
            <td><a data-permission="allow"></a></td>
        </tr>
        <tr>
            <td>B</td>
            <td><a data-permission="allow"></a></td>
        </tr>
        <tr>
            <td>C</td>
            <td><a data-permission="allow"></a></td>
        </tr>
        <tr>
            <td>D</td>
            <td><a data-permission="allow"></a></td>
        </tr>
        <tr>
            <td>E</td>
            <td><button type="button" data-permission="allow"></button></td>
        </tr>
    </tbody>
</table>

现在我发现上面例子中包含“数据权限”属性的节点,如(a,按钮等) 要做到这一点,我使用以下代码。现在我要做的是删除整个<a>..</a> or <button>...</button>或任何其他元素,如果它们包含“data-permission”属性,删除后只返回剩余的HTML。那么如何实现呢?

$dom = new DOMDocument;
$dom->loadHTML($output);

$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//@data-permission-id');
foreach ($nodes as $node) {
    echo $node->nodeValue;
    //$node->parentNode->removeChild($node); throws the error "Not Found Error"
}

注意 - 我试过了$ node-&gt; parentNode-&gt; removeChild($ node);内部循环,但它会抛出错误。删除该标签后,我想获得剩余的HTML。我看过How to delete element with DOMDocument?,但没有帮助。

1 个答案:

答案 0 :(得分:1)

替换您的节点值以删除:$node->nodeValue = "";

$dom = new DOMDocument;
$dom->loadHTML($output);
echo "Previous : ".PHP_EOL.$dom->textContent.PHP_EOL;
$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//*[@data-permission='allow']");
foreach ($nodes as $node) {
    $node->nodeValue = "";
    $dom->saveHTML();
}

现场演示:https://eval.in/885719

使用您的表数据进行实时演示:https://eval.in/885780