0

我有一个关于 domdocument 的问题。

我的$html包含类似

texts …paragraph..

<table class='test'>
   tr and td...
</table>

texts and more texts

我想检测我的 html 变量是否有表格元素。如果是这样,请将其他文本包装在<p>标签中。

所以它会是

<p>texts …paragraph..</p>

<table class='test'>
   tr and td...
</table>

<p>texts and more texts</p>

我的代码就像

$doc = new DOMDocument();
$doc->loadHTML($htmlString);

$tables = $doc->getElementsByTagName('table');

     foreach ($tables as $table) {
        //I am not sure what to do next...
     }

有人可以帮我解决这个问题吗?非常感谢!

4

1 回答 1

-1

我没有测试这个但是。

$html  = preg_replace('/(<table.*</table>)/i','<p>$1</p>', $html);

希望能帮助到你...

于 2013-09-06T18:11:28.107 回答