Php batch generate html and txt file implementation code

Source: Internet
Author: User
This article provides a detailed analysis of the implementation code of html and txt files generated in batches using php. For more information, see

This article provides a detailed analysis of the implementation code of html and txt files generated in batches using php. For more information, see

First, create a conn. php file to connect to the database.

The Code is as follows:


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


Php batch generate html

The Code is as follows:


Require_once ("conn. php ");
$ Query = "SELECT id, title, introduce FROM my_table ";
$ Result = mysql_query ($ query) or die ("Query failed:". mysql_error ());
/* Generate HTML results */
While ($ row = mysql_fetch_array ($ result, MYSQL_ASSOC )){

$ Id = $ row ['id'];
$ Title = $ row ['title'];
$ Introduce = $ row ['inserted'];
$ Path = "html/$id.html ";
$ Fp = fopen ("template.html", "r"); // read-only open Template
$ Str = fread ($ fp, filesize ("template.html"); // read the template content
$ Str = str_replace ("{title}", $ title, $ str );
$ Str = str_replace ("{introduce}", $ introduce, $ str); // replace content
Fclose ($ fp );
$ Handle = fopen ($ path, "w"); // The path to open the news in writing mode.
Fwrite ($ handle, strip_tags ($ introduce); // write the replaced content into the generated HTML file.
Fclose ($ handle );
// Echo "generated successfully "."
";
}
/* Release resources */
Mysql_free_result ($ result );
Mysql_close ($ link );
?>


Template.html file content:

The Code is as follows:






{Title}


{Introduce}



Php batch generate txt

The Code is as follows:


Require_once ("conn. php ");
$ Query = "SELECT kid, title, introduce FROM pro_courses ";
$ Result = mysql_query ($ query) or die ("Query failed:". mysql_error ());
/* Generate txt results */
While ($ row = mysql_fetch_array ($ result, MYSQL_ASSOC )){

$ Id = $ row ['id'];
$ Title = $ row ['title'];
$ Introduce = $ row ['inserted'];
$ Path = "html/$id.txt ";
$ Handle = fopen ($ path, "w"); // The path to open the news in writing mode.
Fwrite ($ handle, strip_tags ($ introduce); // write the replaced content into the generated txt file.
Fclose ($ handle );
}
/* Release resources */
Mysql_free_result ($ result );
Mysql_close ($ link );
?>

, Website space, Hong Kong server, and Hong Kong Virtual Host

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.