How to generate the title and content of a txt file using php
Source: Internet
Author: User
This article describes in detail how php generates the title and content of a txt file. The following is a good example.
The code is as follows:
/**
* 1. when a Buddy worked a few days ago, they asked him to write a file generation class: generate a file. the file types include txt, html, csv, pdf, doc (or docx ).
*
* 2. the generated content is a table (like a table in html). the parameter is the file type generated, the title of the generated content (array), and the generated content (array, corresponding to the title ).
*/
/*************************************** **********
* Class name: createFile
* Description: create different type files
* Author: fenghuo
* Date: 2013-11-12
**************************************** ********/
/**
* 3. I used the evening time to help him sort out a txt file class.
***/
Class createFile {
Public $ file_type;
Public $ file_name;
Public $ file_dir;
/**
* Constructor: initialize the Directory of the generated file
*/
Public function _ construct ($ file_dir ){
$ This-> file_dir = $ file_dir;
}
/**
* File generation Portal function
* @ String $ file_name file name
* @ String $ file_type file type
* @ Array $ title: the title line of the content generated
* @ Array $ data generated content
*/
Public function create_file ($ file_name, $ file_type, $ title, $ data ){
If (empty ($ data )){
Return false;
}
If (! Empty ($ title )){
If (count ($ title )! = Count ($ data [0]) {
Return false;
}
}
If ($ file_name = ""){
$ File_name = $ this-> file_name;
}
If ($ file_type = ""){
$ File_type = $ this-> file_type;
}
$ Fun = 'MK _ '. $ file_type;
# Test point
Echo $ fun ,'--------------
';
If (method_exists ($ this, $ fun ))
{
$ File = $ file_name. ".". $ file_type;
$ This-> $ fun ($ file, $ title, $ data );
Return true;
} Else {
Return "NO! ";
}
}
/**
* Generate a txt file
* @ String $ file name
* @ Array $ title
* @ Array $ data Content
*/
Public function mk_txt ($ file, $ title, $ data ){
$ String = "";
If (! Empty ($ title )){
For ($ I = 0; $ I <count ($ title); $ I ++ ){
$ String. = ''. mb_convert_encoding ($ title [$ I], 'gbk'," UTF-8 ");
}
$ String. = "\ r \ n ";
}
Foreach ($ data as $ key => $ var)
{
For ($ I = 0; $ I <count ($ data [$ key]); $ I ++ ){
$ String. = ''. mb_convert_encoding ($ data [$ key] [$ I], 'gbk'," UTF-8 ");
}
$ String. = "\ r \ n ";
}
# Test point
Echo $ this-> file_dir. $ file, '----- 123 ---------
';
$ Fp = fopen ($ this-> file_dir. $ file, "a + ");
Fwrite ($ fp, $ string );
Fclose ($ fp );
Return true;
}
}
//**************************************
// Test
$ Dir = 'E: \ dev \';
$ File_name = "test ";
$ File_type = "txt ";
$ Title = array ("name", "sex", "age ");
$ Data [] = array ("tom", "boy", 20 );
$ Data [] = array ("perry", "girl", 20 );
$ File = new createFile ($ dir );
$ Flag = $ file-> create_file ($ file_name, $ file_type, $ title, $ data );
If ($ flag = true ){
Echo "generated successfully ";
} Else {
Echo "generation failed ";
}
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.