Php 合并两个文档节点 年 分数 2014 3078
如果将上面的表成功存储为变量,如何将其附加到具有overflow-x样式属性的div中 我尝试了以下代码片段,但没有雪茄:Php 合并两个文档节点 年 分数 2014 3078,php,html,simple-html-dom,Php,Html,Simple Html Dom,如果将上面的表成功存储为变量,如何将其附加到具有overflow-x样式属性的div中 我尝试了以下代码片段,但没有雪茄: <table> <tr> <th>Year</th> <th>Score</th> </tr> <tr> <td>2014</td> <td>3078</td> </tr> </ta
<table>
<tr>
<th>Year</th>
<th>Score</th>
</tr>
<tr>
<td>2014</td>
<td>3078</td>
</tr>
</table>
$div=str\u get\u html(“”);
$div=$div->find('div');
$div=$div->appendChild($table);
返回$div;
因此,预期产出应为:
$div = str_get_html('<div style="overflow-x:auto;"></div>');
$div = $div->find('div');
$div = $div->appendChild($table);
return $div;
年
分数
2014
3078
希望这篇文章能为您提供实现的基本思路。这里我们使用的是DOMDocument
你之前没有发布过同样的问题吗?@D.Wells你能分享你的预期结果吗?@Sahil我现在把它添加到了问题中“我尝试了以下片段,但没有雪茄:”-那么你得到了什么?什么是返回$table代码>假设在第一个块中返回?有没有代码被删除?你让我们很难帮助你。如果我们不知道您当前的结果/问题是什么,我们就无法真正调试您的代码。您还使用了“神奇”变量(我们不知道它们来自何处、如何填充或包含哪些变量),我们需要知道这些变量才能帮助您。
<div style="overflow-x:auto;">
<table>
<tr>
<th>Year</th>
<th>Score</th>
</tr>
<tr>
<td>2014</td>
<td>3078</td>
</tr>
</table>
</div>
<?php
ini_set('display_errors', 1);
//creating table node
$tableNode='<table><tr><th>Year</th><th>Score</th></tr><tr><td>2014</td><td>3078</td></tr></table>';
$domDocument = new DOMDocument();
$domDocument->encoding="UTF-8";
$domDocument->loadHTML($tableNode);
$domXPath = new DOMXPath($domDocument);
$table = $domXPath->query("//table")->item(0);
//creating empty div node.
$domDocument = new DOMDocument();
$element=$domDocument->createElement("div");
$element->setAttribute("style", "overflow-x:auto;");
$result=$domDocument->importNode($table,true);//importing node from of other DOMDocument
$element->appendChild($result);
echo $domDocument->saveHTML($element);