Php batch generate html and txt files

Source: Internet
Author: User
Php batch generate html and txt files

  1. $ Link = mysql_connect ("mysql_host", "mysql_user", "mysql_password") or die ("cocould not connect:". mysql_error ());
  2. Mysql_query ("set names utf8 ");
  3. Mysql_select_db ("my_database") or die ("cocould not select database ");
  4. ?>

Php batch generate html

  1. Require_once ("conn. php ");

  2. $ Query = "SELECT id, title, introduce FROM my_table ";

  3. $ Result = mysql_query ($ query) or die ("Query failed:". mysql_error ());

  4. /* Generate HTML results */

  5. While ($ row = mysql_fetch_array ($ result, MYSQL_ASSOC )){
  6. $ Id = $ row ['id'];
  7. $ Title = $ row ['title'];
  8. $ Introduce = $ row ['inserted'];
  9. $ Path = "html/$id.html ";
  10. $ Fp = fopen ("template.html", "r"); // read-only open template
  11. $ Str = fread ($ fp, filesize ("template.html"); // read the template content
  12. $ Str = str_replace ("{title}", $ title, $ str );
  13. $ Str = str_replace ("{introduce}", $ introduce, $ str); // replace content
  14. Fclose ($ fp );
  15. $ Handle = fopen ($ path, "w"); // The path to open the news in writing mode.
  16. Fwrite ($ handle, strip_tags ($ introduce); // write the replaced content into the generated HTML file.
  17. Fclose ($ handle );
  18. // Echo "generated successfully "."
    ";
  19. }

  20. /* Release resources */

  21. Mysql_free_result ($ result );
  22. Mysql_close ($ link );
  23. ?>

Template.html file content:

  1. {Title}
  2. {Introduce}

Php batch generate txt

  1. Require_once ("conn. php ");

  2. $ Query = "SELECT kid, title, introduce FROM pro_courses ";

  3. $ Result = mysql_query ($ query) or die ("Query failed:". mysql_error ());

  4. /* Generate txt results */

  5. While ($ row = mysql_fetch_array ($ result, MYSQL_ASSOC )){
  6. $ Id = $ row ['id'];
  7. $ Title = $ row ['title'];
  8. $ Introduce = $ row ['inserted'];
  9. $ Path = "html/$id.txt ";
  10. $ Handle = fopen ($ path, "w"); // The path to open the news in writing mode.
  11. Fwrite ($ handle, strip_tags ($ introduce); // write the replaced content into the generated txt file.
  12. Fclose ($ handle );
  13. }

  14. /* Release resources */

  15. Mysql_free_result ($ result );
  16. Mysql_close ($ link );
  17. ?>

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.