首頁 後端開發 php教程 將網頁匯出為Word文件的php程式碼介紹

將網頁匯出為Word文件的php程式碼介紹

Jul 25, 2016 am 09:05 AM

  1. /**
  2. * 根據html程式碼取得word文檔內容
  3. * 建立一個本質為mht的文檔,該函數會分析文件內容並從遠端下載頁面中的圖片資源
  4. * 此函數依賴類別MhtFileMaker
  5. * 函數會分析img標籤,提取src的屬性值。但是,src的屬性值必須被引號包圍,否則不能提取
  6. *
  7. * @param string $content HTML內容
  8. * @param string $absolutePath 網頁的絕對路徑。如果HTML內容裡的圖片路徑是相對路徑,那麼就需要填入這個參數,讓函數自動填入絕對路徑。這個參數最後需要以/結束
  9. * @param bool $isEraseLink 是否去掉HTML內容中的連結
  10. */
  11. function getWordDocument( $content , $absolutePath = "" , $isEraseLink = true trueras)
  12. {
  13. $mht = new MhtFileMaker();
  14. if ($isEraseLink)
  15. $content = preg_replace('/(s*.*?s*) /i' , '$1' , $content); //去掉連結
  16. $images = array();
  17. $files = array();
  18. $matches = array();
  19. //這個演算法要求src後的屬性值必須用引號括起來
  20. if ( preg_match_all('/將網頁匯出為Word文件的php程式碼介紹/i',$content ,$matches ) )
  21. {
  22. $arrPath = $matches[1];
  23. for ( $i=0;$ i{
  24. $path = $arrPath[$i];
  25. $imgPath = trim( $path );
  26. if ( $imgPath != " " )
  27. {
  28. $files[] = $imgPath;
  29. if( substr($imgPath,0,7) == 'http://')
  30. {
  31. //絕對鏈接,不加前綴
  32. }
  33. else
  34. {
  35. $imgPath = $absolutePath.$imgPath;
  36. }
  37. $images[] = $imgPath;
  38. }
  39. }
  40. $images[] = $imgPath;
  41. }
  42. }
  43. $images[] = $imgPath;
  44. }
  45. }
  46. $images[] = $imgPath; >}
  47. }
  48. $mht->AddContents("tmp.html",$mht->GetMimeType("tmp.html"),$content);
  49. for ( $i=0;$i {
  50. $image = $images[$i];
  51. if ( @fopen($image , 'r') )
  52. {
  53. $imgcontent = @file_get_contents( $image );
  54. if ( $content )
  55. $mht->AddContents($files[$i],$mht->GetMimeType($image),$imgcontent);
  56. }
else
{ echo "file:".$image." not exist!
";
}
}

return $mht->GetFile( );

}
    複製程式碼
  1. 使用方法:
$fileContent = getWordDocument($content,"http://www.yoursite.com/Music/etc/");
$fp = fopen("test.doc", 'w'); fwrite($fp, $fileContent); fclose($fp);複製代碼

其中,$content變數應該是HTML原始碼,後面的連結應該是能填滿HTML程式碼中圖片相對路徑的URL位址 請注意,在使用這個函數之前,您需要先包含類別MhtFileMaker,這個類別可以幫助我們產生Mht文件。

  1. /**************************************************** **** ********************
  2. 分類:Mht File Maker
  3. 版本:1.2 beta
  4. 連結:http://bbs. it-home.org
  5. 作者:無迪
  6. 描述:該類別可以製作.mht檔。
  7. *********************************************** ***** ***********************/
  8. class MhtFileMaker{
  9. var $config = array();
  10. var $headers = array();
  11. var $headers_exists = array();
  12. var $files = array();
  13. var $boundary;
  14. var $dir_base;
  15. var $page_first;
  16. function MhtFile($config = array()){
  17. }
  18. function SetHeader($header){
  19. $this->headers[] = $header;
  20. $key = strtolower(substr($header, 0, strpos($header, ':')));
  21. $this->headers_exists[$key] = TRUE;
  22. }
  23. function SetFrom($from){
  24. $this->SetHeader("From: $from");
  25. }
  26. function SetSubject($subject){
  27. $this->SetHeader("主題:$subject");
  28. }
  29. function SetDate($date = NULL, $istimestamp = FALSE){
  30. if ($date == NULL) {
  31. $date = time();
  32. }
  33. if ($istimestamp == TRUE) {
  34. $date = date('D, d M Y H:i:s O', $date);
  35. }
  36. $this->SetHeader("日期:$date");
  37. }
  38. function SetBoundary($boundary = NULL){
  39. if ($boundary == NULL) {
  40. $this->boundary = '--' . strtoupper(md5(mt_rand())) 。
  41. } else {
  42. $this->boundary = $boundary;
  43. }
  44. }
  45. function SetBaseDir($dir){
  46. $this->dir_base = str_replace("\", "/", realpath($dir));
  47. }
  48. function SetFirstPage($filename){
  49. $this->page_first = str_replace("\", "/", realpath("{$this->dir_base}/$filename") );
  50. }
  51. function AutoAddFiles(){
  52. if (!isset($this->page_first)) {
  53. exit ('未設定第一頁。');
  54. }
  55. }
  56. exit ('未設定第一頁。');
  57. }
  58. }
  59. ) $filepath = str_replace($this->dir_base, '', $this->page_first);
  60. $filepath = 'http://mhtfile' 。 ->page_first, $filepath, NULL);
  61. $this->AddDir($this->dir_base);
  62. }
  63. function AddDir($dir){
  64. $handle_dir = opendir($dir ) );
  65. while ($filename = readdir($handle_dir)) {
  66. if (($filename!='.') && ($filename!='..') && ("$dir/$ filename "!=$this->page_first)) {
  67. if (is_dir("$dir/$filename")) {
  68. $this->AddDir("$dir/$filename");
  69. } elseif (is_file("$dir/$filename")) {
  70. $filepath = str_replace($this->dir_base, '', "$dir/$filename")
  71. $filepath = 'http:/ $檔案路徑;
  72. $this->AddFile("$dir/$filename", $filepath, NULL);
  73. }
  74. }
  75. }
  76. closedir($handle_dir) );
  77. }
  78. 函數AddFile($filename, $filepath = NULL, $encoding = NULL){
  79. if ($filepath == NULL) {
  80. $filepath = $filename;
  81. }
  82. $mimetype = $this->GetMimeType($filename);
  83. $filecont = file_get_contents($filename);
  84. $this->AddContents($filepath, $mimetype, $filecont, $encoding);
  85. }
  86. function AddContents($filepath, $mimetype, $filecont, $encoding = NULL){
  87. if ($encoding == NULL) {
  88. $filecont = chunk_split(base64_encode($filecont) ,76);
  89. $encoding = 'base64'
  90. }
  91. $this->files[] = array('filepath' => $filepath,
  92. 'mimetype' => $mimetype,
  93. 'filecont' => $filecont ,
  94. '編碼' => $編碼);
  95. }
  96. function CheckHeaders(){
  97. if (!array_key_exists('date', $this-this-this > headers_exists)) {
  98. $this->SetDate(NULL, TRUE)
  99. if ($this->boundary == NULL) {
  100. $this->SetBoundary(); 🎜>}
  101. }
  102. function CheckFiles(){
  103. if (count($this->files) == 0) {
  104. 回傳FALSE;
  105. } else {
  106. 回傳TRUE ;
  107. }
  108. }
  109. 函數GetFile(){
  110. $this->CheckHeaders();
  111. if (!$this->CheckFiles()) {
  112. exit ('未新增檔案。');
  113. }
  114. $contents = implode("rn", $this->headers);
  115. $contents .= "rn"
  116. $contents .= "MIME-版本: 1.0rn"; $contents .= "內容類型:多部分/相關;rn";
  117. $contents .= "tboundary="{$this->boundary}";rn";
  118. $contents .= "ttype= "" . $this->files[0]['mimetype'] . ""rn";
  119. $contents .= "X-MimeOLE:由Mht File Maker v1.0 beta製作」;
  120. $contents .= "rn";
  121. $contents .= "這是一條 MIME 格式的多部分訊息.rn";
  122. $contents .= "rn";
  123. foreach ($this->files as $file) {
  124. $contents .= "--{$this->boundary}rn";
  125. $contents .= "內容型別:$file[mimetype]rn";
  126. $contents .= "內容傳輸編碼:$file[encoding]rn";
  127. $contents .= "內容位置:$file[filepath]rn";
  128. $contents .= "rn";
  129. $contents .= $file['filecont']; ";
  130. }
  131. $contents .= "--{$this->boundary}--rn";
  132. 返回$內容;
  133. }
  134. function MakeFile($filename){
  135. $contents = $this->GetFile()
  136. $fp = fopen($filename, 'w');
  137. fwrite($fp, $contents); );
  138. }
  139. function GetMimeType($filename){
  140. $pathinfo = pathinfo($filename);
  141. switch ($pathinfo['extension']) {
  142. case 'htm': $mimetype = 'text/html';
  143. case 'html': $mimetype = 'text/html';
  144. case 'txt': $mimetype = 'text/plain'
  145. case 'cgi': $mimetype = 'text/plain';
  146. case 'php': $mimetype = 'text/plain';
  147. case 'css': $mimetype = 'text /css';
  148. case 'jpg': $mimetype = 'image/jpeg';
  149. case 'jpeg': $mimetype = 'image/jpeg'; ': $mimetype = '圖片/jpeg';休息;
  150. case 'gif': $mimetype = 'image/gif'; 休息;
  151. case 'png': $mimetype = 'image/png'; 休息;
  152. 預設值:$mimetype = 'application/octet-stream';休息;
  153. }
  154. 回傳 $mimetype;
  155. }
  156. }
  157. ?>
  158. 複製程式碼
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1667
14
CakePHP 教程
1426
52
Laravel 教程
1328
25
PHP教程
1273
29
C# 教程
1255
24
說明PHP中的安全密碼散列(例如,password_hash,password_verify)。為什麼不使用MD5或SHA1? 說明PHP中的安全密碼散列(例如,password_hash,password_verify)。為什麼不使用MD5或SHA1? Apr 17, 2025 am 12:06 AM

在PHP中,應使用password_hash和password_verify函數實現安全的密碼哈希處理,不應使用MD5或SHA1。1)password_hash生成包含鹽值的哈希,增強安全性。 2)password_verify驗證密碼,通過比較哈希值確保安全。 3)MD5和SHA1易受攻擊且缺乏鹽值,不適合現代密碼安全。

PHP:網絡開發的關鍵語言 PHP:網絡開發的關鍵語言 Apr 13, 2025 am 12:08 AM

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP和Python:比較兩種流行的編程語言 PHP和Python:比較兩種流行的編程語言 Apr 14, 2025 am 12:13 AM

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP行動:現實世界中的示例和應用程序 PHP行動:現實世界中的示例和應用程序 Apr 14, 2025 am 12:19 AM

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型? PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型? Apr 17, 2025 am 12:25 AM

PHP類型提示提升代碼質量和可讀性。 1)標量類型提示:自PHP7.0起,允許在函數參數中指定基本數據類型,如int、float等。 2)返回類型提示:確保函數返回值類型的一致性。 3)聯合類型提示:自PHP8.0起,允許在函數參數或返回值中指定多個類型。 4)可空類型提示:允許包含null值,處理可能返回空值的函數。

PHP的持久相關性:它還活著嗎? PHP的持久相關性:它還活著嗎? Apr 14, 2025 am 12:12 AM

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。

PHP和Python:解釋了不同的範例 PHP和Python:解釋了不同的範例 Apr 18, 2025 am 12:26 AM

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

PHP與其他語言:比較 PHP與其他語言:比較 Apr 13, 2025 am 12:19 AM

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

See all articles