$html = '123abc中文abc123'; // html 內容
$html = strip_tags($html, '
,
<h1>,
<h2>,
<h3>,
<h4>,
<h5>,
<h6>,<strong>,<em>,<sup>,<sub>,
<table>,
<tr>,
<td>
,
<ul>,
<ol>,
<li>'); // 建議過濾 只留下這些html
$fileName = 'test'; // 檔名
start();
echo $html;
save($fileName.'.docx');
ob_flush();
flush();
redirect($fileName.'.docx');
function start(){
ob_start();
echo '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
';
}
function save($path) {
echo "</html>";
$data = ob_get_contents();
ob_end_clean();
wirtefile ($path,$data);
}
function wirtefile ($fn,$data) {
$fp=fopen($fn,"wb");
fwrite($fp,$data);
fclose($fp);
}
參考資料:
https://www.itread01.com/articles/1478056826.html
相關