Php generates the txt file title and content

Source: Internet
Author: User
& Lt ;? Php *** a few days ago, when a buddy was working, they asked him to write a file generation class: generate a file. the file type can be: txt, html, csv, pdf, doc (or docx ). ** 2. the generated content is a table (like a table in html). the parameter is: file type and file generation.
 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 * /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 "failed to generate" ;}?>

 

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.