PHP Export Instance code for Word format documents

Source: Internet
Author: User
Tags learn php
  1. /**

  2. * Export Word format documents
  3. * Add keyword links only once
  4. * Edit bbs.it-home.org
  5. */
  6. Var_dump ($_server["HTTPS"]);d ie;
  7. Class Word
  8. {
  9. function Start ()
  10. {
  11. Ob_start ();
  12. Echo ' xmlns:w= "Urn:schemas-microsoft-com:office:word"
  13. Xmlns= "http://www.w3.org/tr/rec-html40″>";
  14. }

  15. function Save ($path)

  16. {
  17. Print "";
  18. $data = Ob_get_contents ();
  19. Ob_end_clean ();
  20. $this->wirtefile ($path, $data);
  21. }

  22. function Wirtefile ($FN, $data)

  23. {
  24. $FP =fopen ($FN, "WB");
  25. Fwrite ($fp, $data);
  26. Fclose ($FP);
  27. }
  28. }
  29. $db = mysql_connect (' localhost ', ' root ', ' 123123′ ' or Die ("Could not connect to database."); /Connection Database

  30. mysql_select_db (' Test '); Select Database

  31. $sql = "Select Info.itemid,info.title,info.tag,con.content from csign_info_22 as info left join csign_info_data_22 as con On con.itemid = Info.itemid ";

  32. mysql_query (' Set names ' Utf8″ ');

  33. $res =mysql_query ($sql, $db);

  34. while ($info =mysql_fetch_array ($res))

  35. {
  36. if ($info [' tag ']) {
  37. $tagexp = Explode (', $info [' tag ']);
  38. foreach ($tagexp as $k = = $v) {
  39. $tagstr. = '. $v. ';

  40. if (Strpos ($info [' content '], $v)) {

  41. $info [' content '] =str_replace_once ($v, "". $v. "", $info [' content ']);
  42. }
  43. }
  44. }
  45. echo $info [' content '];d ie;
  46. $html = '

    '. $info [' title ']. '

    '. $info [' content ']. '

    ’;

  47. if ($tagstr) {$html. = '

    Keywords: '. $tagstr. '

    ’;}
  48. $html. = '

    This article is compiled by the programmer's home, please visit tiandone for more information.

    ’;
  49. $word = new Word ();
  50. $word->start ();
  51. $html = "AAA". $i;
  52. $title = Iconv ("Utf-8″," Gb2312″, $info [' title ']);
  53. echo $title;d ie;
  54. $wordname = ' e:/web/paypal/product/tiandone/'. $title. ". Doc ";//Store Directory
  55. echo $wordname;d ie;
  56. Echo $html;
  57. $word->save ($wordname);
  58. Ob_flush ();//flush cache before each execution
  59. Flush ();
  60. Print_r ($info);d ie;
  61. Die
  62. }
  63. Match only one keyword repeat mismatch
  64. function Str_replace_once ($needle, $replace, $haystack) {
  65. Looks for the first occurence of $needle in $haystack
  66. and replaces IT with $replace.
  67. $pos = Strpos ($haystack, $needle);
  68. if ($pos = = = False) {
  69. Nothing found
  70. return $haystack;
  71. }
  72. Return Substr_replace ($haystack, $replace, $pos, strlen ($needle));
  73. }
  74. ?>
Copy Code

The above is the code for today's PHP tutorial, for everyone to learn the use of research. Learn PHP and come to the programmer's house.

  • 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.