-
-
/**
- * 根據html程式碼取得word文檔內容
- * 建立一個本質為mht的文檔,該函數會分析文件內容並從遠端下載頁面中的圖片資源
- * 此函數依賴類別MhtFileMaker
- * 函數會分析img標籤,提取src的屬性值。但是,src的屬性值必須被引號包圍,否則不能提取
- *
- * @param string $content HTML內容
- * @param string $absolutePath 網頁的絕對路徑。如果HTML內容裡的圖片路徑是相對路徑,那麼就需要填入這個參數,讓函數自動填入絕對路徑。這個參數最後需要以/結束
- * @param bool $isEraseLink 是否去掉HTML內容中的連結
- */
- function getWordDocument( $content , $absolutePath = "" , $isEraseLink = true trueras)
- {
- $mht = new MhtFileMaker();
- if ($isEraseLink)
- $content = preg_replace('/(s*.*?s*) /i' , '$1' , $content); //去掉連結
- $images = array();
- $files = array();
- $matches = array();
- //這個演算法要求src後的屬性值必須用引號括起來
- if ( preg_match_all('//i',$content ,$matches ) )
- {
- $arrPath = $matches[1];
- for ( $i=0;$ i{
- $path = $arrPath[$i];
- $imgPath = trim( $path );
- if ( $imgPath != " " )
- {
- $files[] = $imgPath;
- if( substr($imgPath,0,7) == 'http://')
- {
- //絕對鏈接,不加前綴
- }
- else
- {
- $imgPath = $absolutePath.$imgPath;
- }
- $images[] = $imgPath;
- }
- }
- $images[] = $imgPath;
- }
- }
- $images[] = $imgPath;
- }
- }
- $images[] = $imgPath; >}
- }
- $mht->AddContents("tmp.html",$mht->GetMimeType("tmp.html"),$content);
- for ( $i=0;$i {
- $image = $images[$i];
- if ( @fopen($image , 'r') )
- {
- $imgcontent = @file_get_contents( $image );
- if ( $content )
- $mht->AddContents($files[$i],$mht->GetMimeType($image),$imgcontent);
- }
else { echo "file:".$image." not exist! "; } } return $mht->GetFile( ); }
$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文件。
-
-
/**************************************************** **** ********************
- 分類:Mht File Maker
- 版本:1.2 beta
- 連結:http://bbs. it-home.org
- 作者:無迪
- 描述:該類別可以製作.mht檔。
- *********************************************** ***** ***********************/
- class MhtFileMaker{
- var $config = array();
- var $headers = array();
- var $headers_exists = array();
- var $files = array();
- var $boundary;
- var $dir_base;
- var $page_first;
- function MhtFile($config = array()){
- }
- function SetHeader($header){
- $this->headers[] = $header;
- $key = strtolower(substr($header, 0, strpos($header, ':')));
- $this->headers_exists[$key] = TRUE;
- }
- function SetFrom($from){
- $this->SetHeader("From: $from");
- }
- function SetSubject($subject){
- $this->SetHeader("主題:$subject");
- }
- function SetDate($date = NULL, $istimestamp = FALSE){
- if ($date == NULL) {
- $date = time();
- }
- if ($istimestamp == TRUE) {
- $date = date('D, d M Y H:i:s O', $date);
- }
- $this->SetHeader("日期:$date");
- }
- function SetBoundary($boundary = NULL){
- if ($boundary == NULL) {
- $this->boundary = '--' . strtoupper(md5(mt_rand())) 。
- } else {
- $this->boundary = $boundary;
- }
- }
- function SetBaseDir($dir){
- $this->dir_base = str_replace("\", "/", realpath($dir));
- }
- function SetFirstPage($filename){
- $this->page_first = str_replace("\", "/", realpath("{$this->dir_base}/$filename") );
- }
- function AutoAddFiles(){
- if (!isset($this->page_first)) {
- exit ('未設定第一頁。');
- }
- }
- exit ('未設定第一頁。');
- }
- }
- ) $filepath = str_replace($this->dir_base, '', $this->page_first);
- $filepath = 'http://mhtfile' 。 ->page_first, $filepath, NULL);
- $this->AddDir($this->dir_base);
- }
- function AddDir($dir){
- $handle_dir = opendir($dir ) );
- while ($filename = readdir($handle_dir)) {
- if (($filename!='.') && ($filename!='..') && ("$dir/$ filename "!=$this->page_first)) {
- if (is_dir("$dir/$filename")) {
- $this->AddDir("$dir/$filename");
- } elseif (is_file("$dir/$filename")) {
- $filepath = str_replace($this->dir_base, '', "$dir/$filename")
- $filepath = 'http:/ $檔案路徑;
- $this->AddFile("$dir/$filename", $filepath, NULL);
- }
- }
- }
- closedir($handle_dir) );
- }
- 函數AddFile($filename, $filepath = NULL, $encoding = NULL){
- if ($filepath == NULL) {
- $filepath = $filename;
- }
- $mimetype = $this->GetMimeType($filename);
- $filecont = file_get_contents($filename);
- $this->AddContents($filepath, $mimetype, $filecont, $encoding);
- }
- function AddContents($filepath, $mimetype, $filecont, $encoding = NULL){
- if ($encoding == NULL) {
- $filecont = chunk_split(base64_encode($filecont) ,76);
- $encoding = 'base64'
- }
- $this->files[] = array('filepath' => $filepath,
- 'mimetype' => $mimetype,
- 'filecont' => $filecont ,
- '編碼' => $編碼);
- }
- function CheckHeaders(){
- if (!array_key_exists('date', $this-this-this > headers_exists)) {
- $this->SetDate(NULL, TRUE)
- if ($this->boundary == NULL) {
- $this->SetBoundary(); 🎜>}
- }
- function CheckFiles(){
- if (count($this->files) == 0) {
- 回傳FALSE;
- } else {
- 回傳TRUE ;
- }
- }
- 函數GetFile(){
- $this->CheckHeaders();
- if (!$this->CheckFiles()) {
- exit ('未新增檔案。');
- }
- $contents = implode("rn", $this->headers);
- $contents .= "rn"
- $contents .= "MIME-版本: 1.0rn"; $contents .= "內容類型:多部分/相關;rn";
- $contents .= "tboundary="{$this->boundary}";rn";
- $contents .= "ttype= "" . $this->files[0]['mimetype'] . ""rn";
- $contents .= "X-MimeOLE:由Mht File Maker v1.0 beta製作」;
- $contents .= "rn";
- $contents .= "這是一條 MIME 格式的多部分訊息.rn";
- $contents .= "rn";
- foreach ($this->files as $file) {
- $contents .= "--{$this->boundary}rn";
- $contents .= "內容型別:$file[mimetype]rn";
- $contents .= "內容傳輸編碼:$file[encoding]rn";
- $contents .= "內容位置:$file[filepath]rn";
- $contents .= "rn";
- $contents .= $file['filecont']; ";
- }
- $contents .= "--{$this->boundary}--rn";
- 返回$內容;
- }
- function MakeFile($filename){
- $contents = $this->GetFile()
- $fp = fopen($filename, 'w');
- fwrite($fp, $contents); );
- }
- function GetMimeType($filename){
- $pathinfo = pathinfo($filename);
- switch ($pathinfo['extension']) {
- case 'htm': $mimetype = 'text/html';
- case 'html': $mimetype = 'text/html';
- case 'txt': $mimetype = 'text/plain'
- case 'cgi': $mimetype = 'text/plain';
- case 'php': $mimetype = 'text/plain';
- case 'css': $mimetype = 'text /css';
- case 'jpg': $mimetype = 'image/jpeg';
- case 'jpeg': $mimetype = 'image/jpeg'; ': $mimetype = '圖片/jpeg';休息;
- case 'gif': $mimetype = 'image/gif'; 休息;
- case 'png': $mimetype = 'image/png'; 休息;
- 預設值:$mimetype = 'application/octet-stream';休息;
- }
- 回傳 $mimetype;
- }
- }
- ?>
-
-
- 複製程式碼
|