Home >Backend Development >PHP Tutorial >Ideas and code sharing for exporting csv format data and converting numbers into text_PHP tutorial
PHP export of csv format data implementation:
First define a string to store the content, for example
$exportdata = 'Rule 111, Rule 222, Review 222, Rule 222, Service 2222, Rule 1, Rule 2, Rule 3, match characters, set time, validity period'."n";
Then perform a foreach loop on the array that needs to be saved as csv, such as
But when exporting numbers, csv will remove the leading 0. For example, if I want to display 00001, if it is output, it will display 1. The solution is to use a '"t' when outputting, this is Tab characters will be displayed as spaces. The value can be converted into text. However, ' " ' will appear when importing. Just use the trim function that comes with PHP. The complete code is as follows:
Copy code
foreach($lists as $key => $value){
$time = date("Y-m-d_H: i:s", $value['add_time']);
$exportdata .= ""t".$value['Rule_id']."","t".$value['Rule_name']." ","t".$value['Matching_level']."","t"."{$value['Rule_action']}"."","t".$value['Service_type']."" ,"t".$value['Keyword1']."","t".$value['Keyword2']."","t".$value['Keyword3']."","t". $value['Matching_word']."","t".$value['Set_time']."","t".$value['Validation_time'].""n";
}
}
$filename = "plcnetinfo_{$date}.csv"; attachment; filename=$filename");
header("Expires: 0");
header("Pragma: public"); );
header("Cache-Control: public");
echo(mb_convert_encoding($exportdata,"gb2312","UTF-8"));
http://www.bkjia.com/PHPjc/779565.html