- /**
- * Get Word document content based on HTML code
- * Create a document that is essentially MHT, which parses the contents of a file and downloads the picture resource from the page remotely
- * This function depends on the class Mhtfilemaker
- * This function parses the IMG tag and extracts the SRC attribute values. However, the attribute value of SRC must be surrounded by quotation marks, otherwise it cannot be extracted
- *
- * @param string $content HTML content
- * @param string $absolutePath The absolute path of the Web page. If the image path in the HTML content is a relative path, you need to fill in this parameter to let the function automatically fill in the absolute path. This parameter finally needs to be/end
- * @param bool $isEraseLink whether to remove links from HTML content
- */
- function Getworddocument ($content, $absolutePath = "", $isEraseLink = True)
- {
- $MHT = new Mhtfilemaker ();
- if ($isEraseLink)
- $content = Preg_replace ('/(\s*.*?\s*) <\/a>/i ', ' $ ', $content); Remove link
- $images = Array ();
- $files = Array ();
- $matches = Array ();
- This algorithm requires that attribute values after src must be enclosed in quotation marks.
- 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//')
- {
- Absolute links, without prefixes
- }
- Else
- {
- $imgPath = $absolutePath. $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 ();
- }
Copy CodeHow to use:
- $fileContent = Getworddocument ($content, "http://www.yoursite.com/Music/etc/");
- $fp = fopen ("Test.doc", ' W ');
- Fwrite ($fp, $fileContent);
- Fclose ($FP);
Copy CodeWhere the $content variable should be the HTML source code, the following link should be able to fill the HTML code in the image relative path of the URL address note, before using this function, you need to include the class Mhtfilemaker, this class can help us generate MHT document.
- /***********************************************************************
- CLASS:MHT File Maker
- version:1.2 Beta
- link:http://bbs.it-home.org
- Author:wudi
- Description:the class can make. mht file.
- ***********************************************************************/
- 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: $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: $date");
- }
- function setboundary ($boundary = NULL) {
- if ($boundary = = NULL) {
- $this->boundary = '--'. Strtoupper (MD5 (Mt_rand ())). ' _multipart_mixed ';
- } 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 (' not set the first page. ');
- }
- $filepath = Str_replace ($this->dir_base, ", $this->page_first);
- $filepath = ' Http://mhtfile '. $filepath;
- $this->addfile ($this->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://mhtfile '. $filepath;
- $this->addfile ("$dir/$filename", $filepath, NULL);
- }
- }
- }
- Closedir ($handle _dir);
- }
- function 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,
- ' Encoding ' = $encoding);
- }
- function Checkheaders () {
- if (!array_key_exists (' Date ', $this->headers_exists)) {
- $this->setdate (NULL, TRUE);
- }
- if ($this->boundary = = NULL) {
- $this->setboundary ();
- }
- }
- function Checkfiles () {
- if (count ($this->files) = = 0) {
- return FALSE;
- } else {
- return TRUE;
- }
- }
- function GetFile () {
- $this->checkheaders ();
- if (! $this->checkfiles ()) {
- Exit (' No file was added. ');
- }
- $contents = Implode ("\ r \ n", $this->headers);
- $contents. = "\ r \ n";
- $contents. = "mime-version:1.0\r\n";
- $contents. = "content-type:multipart/related;\r\n";
- $contents. = "\tboundary=\" {$this->boundary}\ "; \ r \ n";
- $contents. = "\ttype=\" ". $this->files[0][' mimetype '). "\" \ r \ n ";
- $contents. = "x-mimeole:produced by Mht File Maker v1.0 beta\r\n";
- $contents. = "\ r \ n";
- $contents. = "This was a multi-part message in MIME format.\r\n";
- $contents. = "\ r \ n";
- foreach ($this->files as $file) {
- $contents. = "--{$this->boundary}\r\n";
- $contents. = "Content-type: $file [mimetype]\r\n";
- $contents. = "Content-transfer-encoding: $file [encoding]\r\n";
- $contents. = "Content-location: $file [filepath]\r\n";
- $contents. = "\ r \ n";
- $contents. = $file [' Filecont '];
- $contents. = "\ r \ n";
- }
- $contents. = "--{$this->boundary}--\r\n";
- return $contents;
- }
- function MakeFile ($filename) {
- $contents = $this->getfile ();
- $fp = fopen ($filename, ' w ');
- Fwrite ($fp, $contents);
- Fclose ($FP);
- }
- function GetMimeType ($filename) {
- $pathinfo = PathInfo ($filename);
- Switch ($pathinfo [' extension ']) {
- Case ' htm ': $mimetype = ' text/html '; Break
- Case ' html ': $mimetype = ' text/html '; Break
- Case ' txt ': $mimetype = ' text/plain '; Break
- Case ' cgi ': $mimetype = ' text/plain '; Break
- Case ' php ': $mimetype = ' text/plain '; Break
- Case ' CSS ': $mimetype = ' text/css '; Break
- Case ' jpg ': $mimetype = ' image/jpeg '; Break
- Case ' jpeg ': $mimetype = ' image/jpeg '; Break
- Case ' JPE ': $mimetype = ' image/jpeg '; Break
- Case ' gif ': $mimetype = ' image/gif '; Break
- Case ' png ': $mimetype = ' image/png '; Break
- Default: $mimetype = ' application/octet-stream '; Break
- }
- return $mimetype;
- }
- }
- ?>
Copy Code |