使用php包装div标签的表标签

时间:2018-02-16 07:14:53

标签: php wordpress dom domdocument createelement

我正在使用WordPress。 我想使用wordpress的php将<table> <thead> <tr><th>txt<br>txt</th><th>txt</th></tr> </thead> <tbody> <tr><td>txt</td><td>txt</td></tr> <tr><td>txt</td><td>txt</td></tr> </tbody> </table> 标记与<div> <table> <thead> <tr><th>txt<br>txt</th><th>txt</th></tr> </thead> <tbody> <tr><td>txt</td><td>txt</td></tr> <tr><td>txt</td><td>txt</td></tr> </tbody> </table> </div> 标记括起来。如下所示。

原样

add_filter( 'the_content', 'insert_table_outer');

function insert_table_outer($html) {
    $doc = new DOMDocument();
    $doc->loadHTML( mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
    $tables = $doc->getElementsByTagName('table');
    for ($i=0;$i<$tables->length;$i++) {
        $parent = $doc->createElement('div');
        $table = $tables->item($i);
        $parent->appendChild($table);
    }
    $out = mb_convert_encoding($doc->saveHTML(), 'utf-8', 'HTML-ENTITIES');
    return $out;
}

{{1}}

我尝试了以下代码,但效果不佳。

{{1}}

0 个答案:

没有答案