使用PHP从textarea导出到CSV文件
我一直在网上搜索,这里是关于使用PHP将数据从textarea导出到CSV的 我想有我的页面上的按钮,将有该CSV文件生成,并提供下载时,按下它 我所能找到的就是从html表或mysql导出,我不需要那个 基本上文本区包含一些数据,一旦按下按钮,我想能够下载作为CSV文件的数据 下面是我试图自定义的类:使用PHP从textarea导出到CSV文件,php,html,csv,export,Php,Html,Csv,Export,我一直在网上搜索,这里是关于使用PHP将数据从textarea导出到CSV的 我想有我的页面上的按钮,将有该CSV文件生成,并提供下载时,按下它 我所能找到的就是从html表或mysql导出,我不需要那个 基本上文本区包含一些数据,一旦按下按钮,我想能够下载作为CSV文件的数据 下面是我试图自定义的类: <?php class textarea_2_csv{ var $arrLines; var $data; var $divider; functio
<?php
class textarea_2_csv{
var $arrLines;
var $data;
var $divider;
function __construct($postedData){
$this->arrLines = explode("\r\n",$postedData['xls']);
$this->data = NULL;
}
// process the posted data
function set_data(){
if(is_array($this->arrLines)){
if($this->divider == NULL) $this->set_divider(); // default divider setting if no other option is passed
foreach($this->arrLines as $key=>$line){
// if more than 1 field
if(strpos($line, chr(9)) !== FALSE){
$arrFields = explode(chr(9),$line);
foreach($arrFields as $c=>$val){
$this->data .= $val.$this->divider;
}
$this->data = trim($this->data,$this->divider);
$this->data .= "\r\n";
}
// if only 1 field
else{
$this->data .= $line."\r\n";
}
}
}
}
// saves the data as file or returns it back
function save_data($write = false){
$this->set_data();
if(!$write == false){
// write file (.txt if divider is tab)
($this->divider == chr(9)) ? $ext = '.txt' : $ext = '.csv';
if(file_put_contents($write.$ext, $this->data)) echo('<h3>Data saved in file: '.$write.$ext.'</h3>');
return(NULL);
}
else{
// return de data
return($this->data);
}
}
// set the divider as default to a comma
function set_divider($opt=','){
$this->divider = $opt;
}
}
您可以尝试:
if($bdd = mysqli_connect('localhost', 'root', '', 'checkport'))
{
if(isset($_POST['sub']))
{
$drp = "delete from listeadresse";
mysqli_query($bdd, $drp);
$filename=$_FILES["file"]["tmp_name"];
$file = fOpen($filename, "r");
$count=0;
while(($emapdata = fgetcsv($file, 100, ";")) !== FALSE )
{
如果textarea由从数据库获取的数据填充,则不需要发布texat区域内容。您只需从db发送数据id,然后从数据库中获取具有该id的数据并将其转换为csvNo。它不是由db中的数据填充的……如果您在textarea中有非结构化数据,您将如何构造csv数据?当用户搜索某个内容时,数据从web中获取,然后该数据将返回到该textarea字段中,例如:2010年3月12日,2010年3月13日、2014年3月14日等。。。
if($bdd = mysqli_connect('localhost', 'root', '', 'checkport'))
{
if(isset($_POST['sub']))
{
$drp = "delete from listeadresse";
mysqli_query($bdd, $drp);
$filename=$_FILES["file"]["tmp_name"];
$file = fOpen($filename, "r");
$count=0;
while(($emapdata = fgetcsv($file, 100, ";")) !== FALSE )
{