public function getWordDocument($content, $absolutePath = "", $isEraseLink = true) { $mht = new MhtFileMaker(); if ($isEraseLink) { $content = preg_replace('/<a\\s*.*?\\s*>(\\s*.*?\\s*)<\\/a>/i', '$1', $content); } //去掉链接 $images = array(); $files = array(); $matches = array(); //这个算法要求src后的属性值必须使用引号括起来 if (preg_match_all('/<img[.\\n]*?src\\s*?=\\s*?[\\"\'](.*?)[\\"\'](.*?)\\/>/i', $content, $matches)) { $arrPath = $matches[1]; for ($i = 0; $i < count($arrPath); $i++) { $path = $arrPath[$i]; $imgPath = trim($path); if ($imgPath != "") { $files[] = $imgPath; if (substr($imgPath, 0, 7) == 'http://') { //绝对链接,不加前缀 } else { $imgPath = $absolutePath . $imgPath; } $images[] = $imgPath; } } } $mht->AddContents("tmp.html", $mht->GetMimeType("tmp.html"), $content); for ($i = 0; $i < count($images); $i++) { $image = $images[$i]; if (@fopen($image, 'r')) { $imgcontent = @file_get_contents($image); if ($imgcontent) { $mht->AddContents($files[$i], $mht->GetMimeType($image), $imgcontent); } } else { echo "file:" . $image . " not exist!<br />"; } } return $mht->GetFile(); }
function getWordDocument($content, $absolutePath = "", $isEraseLink = false) { $mht = new MhtFileMaker(); if ($isEraseLink) { $content = preg_replace('/<a\\s*.*?\\s*>(\\s*.*?\\s*)<\\/a>/i', '$1', $content); } $images = array(); $files = array(); $matches = array(); if (preg_match_all('/<img[^>]*src\\s*=\\s*?[\\"\'](.*?)[\\"\'](.*?)\\/>/i', $content, $matches)) { $arrPath = $matches[1]; for ($i = 0; $i < count($arrPath); $i++) { $path = $arrPath[$i]; $imgPath = trim($path); if ($imgPath != "") { $files[] = $imgPath; if (substr($imgPath, 0, 7) == 'http://') { } else { $imgPath = "http://" . $_SERVER['HTTP_HOST'] . "/" . $imgPath; } $images[] = $imgPath; } } } $mht->AddContents("tmp.html", $mht->GetMimeType("tmp.html"), $content); for ($i = 0; $i < count($images); $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!<br />"; } } return $mht->GetFile(); }