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

WBOY
發布: 2016-07-25 09:05:23
原創
978 人瀏覽過
  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. 複製程式碼
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!