php实现word转html文档的例子
发布:smiling 来源: PHP粉丝网 添加日期:2015-04-13 10:54:00 浏览: 评论:0
word文档不适合放到网页上了,如果我们要放到网页中去是需要一个个复制了,如果你还在复制就out了,下文小编来为各位整理一篇php实现word转html文档的例子,希望文章对各位有帮助.
要想完美解决,office转pdf或者html,最好还是用windows office软件,libreoffice不能完美转换,wps没有api.
先确认com模块是不是开启,phpinfo里面如果有com_dotnet模块,说明已开启,如果没有,修改php.ini,com.allow_dcom = true
前面的注释去掉,重启就OK了,php官方网站说,php5.4.5之前,com模块是内置的,其实也不一定全是,官网下的php 5.3.39,com模块就没有内置.
如果不是内置模块的话,php.ini加上,前提你的ext文件夹下,有该扩展.
extension=php_com_dotnet.dll
然后重启就OK了,代码如下:
- function word2html($wordname,$htmlname)
- {
- $word = new COM("word.application") or die("Unable to instanciate Word"); //开源软件:phpfensi.com
- $word->Visible = 1;
- $word->Documents->Open($wordname);
- $word->Documents[1]->SaveAs($htmlname,8);
- $word->Quit();
- $word = null;
- unset($word);
- }
- word2html('D:/www/test/6.docx','D:/www/test/6.html');
注意:
1,转换出来的html,查看源码,比较乱的
2,转换过程中会调用winword.exe
3,如果页面一直在加载,把文档重命名,然后在重新转.
补充一个例子:
- function lego_clean($text) {
- $text = implode("\r",$text);
- // normalize white space
- $text = eregi_replace("[[:space:]]+", " ", $text);
- $text = str_replace("> <",">\r\r<",$text);
- $text = str_replace("<br>","<br>\r",$text);
- // remove everything before <body>
- $text = strstr($text,"<body");
- // keep tags, strip attributes
- $text = ereg_replace("<p [^>]*BodyTextIndent[^>]*>([^\n|\n\015|\015\n]*)</p>","<p>\\1</p>",$text);
- $text = eregi_replace("<p [^>]*margin-left[^>]*>([^\n|\n\015|\015\n]*)</p>","<blockquote>\\1</blockquote>",$text);
- $text = str_replace(" ","",$text);
- //clean up whatever is left inside <p> and <li>
- $text = eregi_replace("<p [^>]*>","<p>",$text);
- $text = eregi_replace("<li [^>]*>","<li>",$text);
- // kill unwanted tags
- $text = eregi_replace("</?span[^>]*>","",$text);
- $text = eregi_replace("</?body[^>]*>","",$text);
- $text = eregi_replace("</?div[^>]*>","",$text);
- $text = eregi_replace("<\![^>]*>","",$text);
- $text = eregi_replace("</?[a-z]\:[^>]*>","",$text);
- // kill style and on mouse* tags
- $text = eregi_replace("([ \f\r\t\n\'\"])style=[^>]+", "\\1", $text);
- $text = eregi_replace("([ \f\r\t\n\'\"])on[a-z]+=[^>]+", "\\1", $text);
- //remove empty paragraphs
- $text = str_replace("<p></p>","",$text);
- //remove closing </html>
- $text = str_replace("</html>","",$text);
- //clean up white space again
- $text = eregi_replace("[[:space:]]+", " ", $text);
- $text = str_replace("> <",">\r\r<",$text);
- $text = str_replace("<br>","<br>\r",$text);
- }
Tags: word转html php转html
- 上一篇:php中去掉头尾空格3种方法
- 下一篇:PHP实现301跳转,及延时跳转代码
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)