2017-09-05 3 views
2
<table> 
<tr> 
    <th>Year</th> 
    <th>Score</th> 
</tr> 
<tr> 
    <td>2014</td> 
    <td>3078</td> 
</tr> 
</table> 

위의 테이블을 변수로 성공적으로 저장 한 경우 오버플로 -x 스타일 속성이있는 div에 어떻게 추가 할 수 있습니까?DOMDocument 노드 두 개를 병합

$div = str_get_html('<div style="overflow-x:auto;"></div>'); 

$div = $div->find('div'); 

$div = $div->appendChild($table); 

return $div; 

그래서 예상 출력해야한다 :

<div style="overflow-x:auto;"> 
<table> 
    <tr> 
     <th>Year</th> 
     <th>Score</th> 
    </tr> 
    <tr> 
     <td>2014</td> 
     <td>3078</td> 
    </tr> 
</table> 
</div> 
+0

당신은 이전에 같은 질문을 게시하지 않았나요? – Barmar

+0

@ D.Wells 예상 출력을 공유 할 수 있습니까? –

+0

@Sahil 지금 질문에 추가했습니다. –

답변

1

희망이 하나가 당신에게 구현의 기본 아이디어를 줄 것이다

나는 다음 코드 있지만 시가를 시도했습니다. 여기서 우리는 DOMDocument을 사용하고 있습니다.

Try this code snippet here

<?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);