我正在使用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}}