PHP에서 DOMDocument를 사용하여 Google 문서 도구에서 스프레드 시트 셀의 내용에 액세스하려고합니다.
노드에 액세스 할 수 있지만 내용이 일반 텍스트이고 HTML 형식이 누락되었습니다.
다음은 굵게, 기울임 꼴 및 밑줄이있는 텍스트가있는 내가 사용하는 예제 링크입니다.
https://docs.google.com/spreadsheets/d/1Klpic32Gb_TDblDZDJQOkDedFGuNHAokxUXqrCPDFWE/pubhtml
다음은 내가 사용중인 PHP 코드입니다.
$url = "https://docs.google.com/spreadsheets/d/1Klpic32Gb_TDblDZDJQOkDedFGuNHAokxUXqrCPDFWE/pubhtml";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$htmlData = curl_exec($curl);
curl_close($curl);
$dom = new \DOMDocument();
$html = $dom->loadHTML($htmlData);
$dom->preserveWhiteSpace = false;
$tables = $dom->getElementsByTagName('table');
$rows = $tables->item(0)->getElementsByTagName('tr');
$cols = $rows->item(1)->getElementsByTagName('td');
$rowHeaders = array();
foreach ($cols as $i => $node) {
if($i >= 0 ) $rowHeaders[] = $node->textContent;
}
foreach ($rows as $i => $row){
if($i == 0 ) continue;
$cols = $row->getElementsByTagName('td');
$row = array();
foreach ($cols as $j => $node) {
$row[$rowHeaders[$j]] = $node->textContent;
}
$table[] = $row;
}
die(print_r($table));
내 출력에 내부 HTML 형식이 누락되었습니다.
[1] => Array
(
[Variable] => html_body
[Data] => Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
)
textContent를 사용하지 말고 시도해보십시오.
foreach ($cols as $j => $node) {
//$row[$rowHeaders[$j]] = $node->textContent;
$innerHTML= '';
$children = $node->childNodes;
foreach ($children as $child) {
$innerHTML .= $child->ownerDocument->saveXML( $child );
}
$row[$rowHeaders[$j]]= $innerHTML;
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다