php處理word
Ⅰ php讀取word文檔怎麼處理亂碼
通過com調用word組件,試試。
<?
// this script is come from zend. :)
$word = new COM( "word.application ") or die( "Unable to instanciate Word ");
print "Loaded Word, version {$word-> Version}\n ";
$word-> Visible = 1;
$word-> Documents-> Add();
$word-> Selection-> TypeText( "This is a test... ");
$word-> Documents[1]-> SaveAs( "Useless test.doc ");
$word-> Quit();
?>
Ⅱ php word轉pdf 有什麼方法嗎
PHP也可以實現導出Word文檔為PDF的功能,不過要藉助於第三方的類庫,今天我們將為大家介紹PHP依靠com.sun.star.ServiceManager來轉換Word為PDF文檔的相關技巧。
PHP處理Word轉PDF的示例代碼:
02set_time_limit(0);
03functionMakePropertyValue($name,$value,$osm){
04$oStruct=$osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue");
05$oStruct->Name=$name;
06$oStruct->Value=$value;
07return$oStruct;
08}
09functionword2pdf($doc_url,$output_url){
10$osm=newCOM("com.sun.star.ServiceManager")ordie("請確認OpenOffice.org庫是否已經安裝. ");
11$args=array(MakePropertyValue("Hidden",true,$osm));
12$oDesktop=$osm->createInstance("com.sun.star.frame.Desktop");
13$oWriterDoc=$oDesktop->loadComponentFromURL($doc_url,"_blank",0,$args);
14$export_args=array(MakePropertyValue("FilterName","writer_pdf_Export",$osm));
15$oWriterDoc->storeToURL($output_url,$export_args);
16$oWriterDoc->close(true);
17}
18$output_dir="D:/temp/";
19$doc_file="D:/temps/test.doc";
20$pdf_file="test.pdf";
21$output_file=$output_dir.$pdf_file;
22$doc_file="file:///".$doc_file;
23$output_file="file:///".$output_file;
24word2pdf($doc_file,$output_file);
25?>
Ⅲ PHP怎麼樣去掉從word直接粘貼過來的沒有用的
一般處理的方式有二種:
通過編輯器的JS直接去除。
2.提交到後台後,直接用程序去掉無效標簽。下面我就分享一個通過PHP的處理方式,成功率可能不是100%。這程序也是在PHP官網上看到的,就順便粘貼過來了。
復制代碼 代碼如下:
function ClearHtml($content,$allowtags='') {
mb_regex_encoding('UTF-8');
//replace MS special characters first
$search = array('/『/u', '/』/u', '/「/u', '/」/u', '/—/u');
$replace = array(''', ''', '"', '"', '-');
$content = preg_replace($search, $replace, $content);
//make sure _all_ html entities are converted to the plain ascii equivalents - it appears
//in some MS headers, some html entities are encoded and some aren't
$content = html_entity_decode($content, ENT_QUOTES, 'UTF-8');
//try to strip out any C style comments first, since these, embedded in html comments, seem to
//prevent strip_tags from removing html comments (MS Word introced combination)
if(mb_stripos($content, '/*') !== FALSE){
$content = mb_eregi_replace('#/*.*?*/#s', '', $content, 'm');
}
//introce a space into any arithmetic expressions that could be caught by strip_tags so that they won't be
//'<1' becomes '< 1'(note: somewhat application specific)
$content = preg_replace(array('/<([0-9]+)/'), array('< $1'), $content);
$content = strip_tags($content, $allowtags);
//eliminate extraneous whitespace from start and end of line, or anywhere there are two or more spaces, convert it to one
$content = preg_replace(array('/^ss+/', '/ss+$/', '/ss+/u'), array('', '', ' '), $content);
//strip out inline css and simplify style tags
$search = array('#<(strong|b)[^>]*>(.*?)</(strong|b)>#isu', '#<(em|i)[^>]*>(.*?)</(em|i)>#isu', '#<u[^>]*>(.*?)</u>#isu');
$replace = array('<b>$2</b>', '<i>$2</i>', '<u>$1</u>');
$content = preg_replace($search, $replace, $content);
//on some of the ?newer MS Word exports, where you get conditionals of the form 'if gte mso 9', etc., it appears
//that whatever is in one of the html comments prevents strip_tags from eradicating the html comment that contains
//some MS Style Definitions - this last bit gets rid of any leftover comments */
$num_matches = preg_match_all("/<!--/u", $content, $matches);
if($num_matches){
$content = preg_replace('/<!--(.)*-->/isu', '', $content);
}
return $content;
}
測試使用結果:
復制代碼 代碼如下:
<?php
$content = ' <!--[if gte mso 9]><xml><w:WordDocument><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><w:>0</w:><w:>2</w:><w:DocumentKind>DocumentNotSpecified</w:DocumentKind><w:DrawingGridVerticalSpacing>7.8</w:DrawingGridVerticalSpacing><w:View>Normal</w:View><w:Compatibility></w:Compatibility><w:Zoom>0</w:Zoom></w:WordDocument></xml><![endif]-->
<p style="text-indent: 24.0000pt; margin-bottom: 0pt; margin-top: 0pt;"><span style="mso-spacerun: "yes"; font-size: 12.0000pt; font-family: "宋體";">《優伴戶外旅行》——讓旅行成為習慣!</span></p>越發忙碌的你,是否想給自己放個假?專注工作的你,是否還記得上一次鍛煉是什麼時候?優伴戶外旅行,給你不一樣的旅行體驗:給心自由,便處處都是風景!</span></p>';
echo ClearHtml($content,'<p>');
/*
得到的結果:
<p >《優伴戶外旅行》--讓旅行成為習慣!</p>越發忙碌的你,是否想給自己放個假?專注工作的你,是否還記得上一次鍛煉是什麼時候?優伴戶外旅行,給你不一樣的旅行體驗:給心自由,便處處都是風景!</p>
*/
?>
Ⅳ 怎樣用PHP讀取一個word文檔內容,並且包含樣式入庫
可以試下phpoffice/phpword庫。
phpoffice包含了office套件中的word,excel, powerpoint,visio等比較常用的軟體文檔的處理庫。可以用於讀取及生成相應的文檔。
具體操作方法可以查看官方文檔,以及示例代碼。
PHPOffice/PHPWord
Ⅳ php操作word文件,求方法謝謝。
如果你本地沒有安裝 office ,那麼系統里是沒有 com 可供調用的(也就是說,使用new com的方法不一定行得通)。
如果你想要生成 word 文件給別人用,可以先生成 mht 文件(就是將 html 及相關資源打包),然後發送給客戶端。相關編碼可參考如下資料:
http://www.cnblogs.com/phphuaibei/archive/2011/11/30/2269427.html
Ⅵ 怎樣用PHP讀取一個word文檔內容並在瀏覽器中顯示出來
目前程序編譯語言有很多種,其中php是最為常見的一種編程語言。php讀取word文檔是很多朋友都想了解的,下面就由達內的老師為大家介紹一下。
?php
/*
*
必須將
php.ini
中的
com.allow_dcom
設為
TRUE
*/
function
php_Word($wordname,$htmlname,$content)
{
//獲取鏈接地址
$url
=
$_SERVER['HTTP_HOST'];
$url
=
";
$url
=
$url.$_SERVER['PHP_SELF'];
$url
=
dirname($url)."/";
//建立一個指向新COM組件的索引
$word
=
new
COM("word.application")
or
die("Unable
to
instanciate
Word");
//顯示目前正在使用的Word的版本號
echo
"Loading
Word,
v.
{$word-
Version}";
//把它的可見性設置為0(假),如果要使它在最前端打開,使用1(真)
$word->Visible
=
1;
//---------------------------------讀取Word內容操作
START-----------------------------------------
//打開一個word文檔
$word->Documents->Open($url.$wordname);
//將filename.doc轉換為html格式,並保存為html文件
$word->Documents[1]->SaveAs(dirname(__FILE__)."/".$htmlname,8);
//獲取htm文件內容並輸出到頁面
(文本的樣式不會丟失)
$content
=
file_get_contents($url.$htmlname);
echo
$content;
//獲取word文檔內容並輸出到頁面(文本的原樣式已丟失)
$content=
$word->ActiveDocument->content->Text;
echo
$content;
//關閉與COM組件之間的連接
$word->Documents->close(true);
$word->Quit();
$word
=
null;
unset($word);
//---------------------------------新建立Word文檔操作
START--------------------------------------
//建立一個空的word文檔
$word->Documents->Add();
//寫入內容到新建word
$word->Selection->TypeText("$content");
//保存新建的word文檔
$word->Documents[1]->SaveAs(dirname(__FILE__)."/".$wordname);
//關閉與COM組件之間的連接
$word->Quit();
}
php_Word("tesw.doc","filename.html","寫入word的內容");
?>
Ⅶ 用php 讀取word 文檔內容 比如:word文檔為試題等等
這個是通過調用com組件的方式操作word的
<?
// 建立一個指向新COM組件的索引
$word = new COM("word.application") or die("Can't start Word!");
// 顯示目前正在使用的Word的版本號
//echo 「Loading Word, v. {$word->Version}<br>」;
// 把它的可見性設置為0(假),如果要使它在最前端打開,使用1(真)
// to open the application in the forefront, use 1 (true)
//$word->Visible = 0;
//打?一個文檔
$word->Documents->OPen("d:\myweb\muban.doc");
//讀取文檔內容
$test= $word->ActiveDocument->content->Text;
echo $test;
echo "<br>";
//將文檔中需要換的變數更換一下
$test=str_replace("<{變數}>","這是變數",$test);
echo $test;
$word->Documents->Add();
// 在新文檔中添加文字
$word->Selection->TypeText("$test");
//把文檔保存在目錄中
$word->Documents[1]->SaveAs("d:/myweb/comtest.doc");
// 關閉與COM組件之間的連接
$word->Quit();
?>
Ⅷ php5.3怎麼將word解碼
echo base64_encode(file_get_contents('word文檔'));
//編碼
echo base64_ decode(file_get_contents('word文檔'));
//解碼
有一個擴展好像可以處理word文檔,好像是COM的可以去看看
Ⅸ 如何使用PHP顯示在線Word文檔
php 在線預覽 word 文檔,有多種方法,本質上來說,大多是調用客戶端電腦系統安裝的 Word 軟體。可以用 php 語言的 com 支持功能函數來完成,比如:
$word = new COM("word.application") or die("Unable to instanciate Word");$word->Documents->Open($url.$wordname);
或者使用 word 文檔的插件來完成,比如:swftools 等。
