首頁 後端開發 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

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

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
<🎜>掩蓋:探險33-如何獲得完美的色度催化劑
2 週前 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教學
1677
14
CakePHP 教程
1430
52
Laravel 教程
1333
25
PHP教程
1278
29
C# 教程
1257
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 17, 2025 am 12:25 AM

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

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

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

在PHP和Python之間進行選擇:指南 在PHP和Python之間進行選擇:指南 Apr 18, 2025 am 12:24 AM

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

PHP和Python:深入了解他們的歷史 PHP和Python:深入了解他們的歷史 Apr 18, 2025 am 12:25 AM

PHP起源於1994年,由RasmusLerdorf開發,最初用於跟踪網站訪問者,逐漸演變為服務器端腳本語言,廣泛應用於網頁開發。 Python由GuidovanRossum於1980年代末開發,1991年首次發布,強調代碼可讀性和簡潔性,適用於科學計算、數據分析等領域。

PHP和框架:現代化語言 PHP和框架:現代化語言 Apr 18, 2025 am 12:14 AM

PHP在現代化進程中仍然重要,因為它支持大量網站和應用,並通過框架適應開發需求。 1.PHP7提升了性能並引入了新功能。 2.現代框架如Laravel、Symfony和CodeIgniter簡化開發,提高代碼質量。 3.性能優化和最佳實踐進一步提升應用效率。

為什麼要使用PHP?解釋的優點和好處 為什麼要使用PHP?解釋的優點和好處 Apr 16, 2025 am 12:16 AM

PHP的核心優勢包括易於學習、強大的web開發支持、豐富的庫和框架、高性能和可擴展性、跨平台兼容性以及成本效益高。 1)易於學習和使用,適合初學者;2)與web服務器集成好,支持多種數據庫;3)擁有如Laravel等強大框架;4)通過優化可實現高性能;5)支持多種操作系統;6)開源,降低開發成本。

PHP的影響:網絡開發及以後 PHP的影響:網絡開發及以後 Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

See all articles