Example of PHP implementing Word to HTML document

Source: Internet
Author: User
Tags vars

Example of PHP implementing Word to HTML document2015-10-16 17:07 2751 People read Comments (2) favorite reports Word document is not suitable to put on the page, if we want to put on the page to be copied, if you are still in the copy out, the following small series for you to compile a word to HTML document example, I hope the article will help you.

For a perfect solution, office to PDF or HTML, preferably with Windows Office software, LibreOffice cannot be transformed perfectly, WPS has no API.

First confirm that the COM module is not open, phpinfo inside if there is com_dotnet module, the description has been turned on, if not, modify PHP.ini,
Com.allow_dcom = True

Before the comments removed, restart on OK, PHP official website said, php5.4.5 before, COM module is built-in, in fact, is not necessarily all, the official website of the PHP 5.3.39,com module is not built-in.

If not the built-in module, PHP.ini Plus, the premise of your Ext folder, there is the extension

Extension=php_com_dotnet.dll

And then reboot is OK.

[PHP]View PlainCopyprint?
  1. function word2html ($wordname,$htmlname)
  2. {
  3. $word = new COM ("Word.Application") or Die ("Unable to instanciate word");
  4. $word->visible = 1;
  5. $word->documents->open ($wordname);
  6. $word->documents[1]->saveas ($htmlname, 8);
  7. $word->quit ();
  8. $word = null;
  9. unset ($word);
  10. }
  11. word2html (' d:/www/test/6.docx ',' d:/www/test/6.html ');

Attention:

1, converted out of HTML, view the source code, the more chaotic
2, the Winword.exe is called during the conversion process
3, if the page has been loaded, rename the document and then re-turn.

Add an example

[PHP]View PlainCopyprint?
  1. function Lego_clean ($text) {
  2. $text = implode ("\ r",$text);
  3. //Normalize white space
  4. $text = eregi_replace ("[[: space:]]+", "", $text);
  5. $text = str_replace ("> <",">\r\r<",$text);
  6. $text = str_replace ("<br>","<br>\r",$text);
  7. //Remove everything before <body>
  8. $text = strstr ($text,"<body");
  9. //Keep tags, strip attributes
  10. $text = ereg_replace ("<p [^>]*bodytextindent[^>]*> ([^\n|\n\015|\015\n]*) </p>","    <p>\\1</p> ",$text);
  11. $text = eregi_replace ("<p [^>]*margin-left[^>]*> ([^\n|\n\015|\015\n]*) </p>","    <blockquote>\\1</blockquote> ",$text);
  12. $text = str_replace ("","" ",$text);
  13. //clean up whatever are left inside <p> and <li>
  14. $text = eregi_replace ("<p [^>]*>","<p>",$text);
  15. $text = eregi_replace ("<li [^>]*>","<li>",$text);
  16. //Kill unwanted tags
  17. $text = eregi_replace ("</?span[^>]*>", "",$text);
  18. $text = eregi_replace ("</?body[^>]*>", "",$text);
  19. $text = eregi_replace ("</?div[^>]*>", "",$text);
  20. $text = eregi_replace ("<\![    ^>]*> "," ",$text);
  21. $text = eregi_replace ("</?[    A-z]\:[^>]*> "," ",$text);
  22. //Kill style and on mouse* tags
  23. $text = eregi_replace ("([\f\r\t\n\ ' \"]) style=[^>]+"," \\1 ", $text);
  24. $text = eregi_replace ("([\f\r\t\n\ ' \"]) on[a-z]+=[^>]+"," \\1 ", $text);
  25. //remove Empty paragraphs
  26. $text = str_replace ("<p></p>", "",$text);
  27. //remove closing
  28. $text = str_replace ("",$text);
  29. //clean up white space again
  30. $text = eregi_replace ("[[: space:]]+", "", $text);
  31. $text = str_replace ("> <",">\r\r<",$text);
  32. $text = str_replace ("<br>","<br>\r",$text);
  33. }

Example of PHP implementing Word to HTML document

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.