使用PHP将mysql导出到csv时使用strip html

时间:2016-06-22 01:48:13

标签: php mysql export-to-csv

我有一个php文件,我用它将两个mysql表中的特定列导出到csv文件。我的问题是其中一个列包含html,我需要在那里显示网页中的列内容,因为它的内容来自wysiwyg文本框。但是,当我将列导出到csv时,我不希望html在那里。但是我不知道如何strip_tags()以便写入csv的列不包含html?

这是我的代码

<?PHP
   //get database connection
   require("./inc/dbconn.inc.php");

   $surveyid = mysql_real_escape_string($_GET['surveyid']);

   //get survey name and date to use as the excel file name
     $sname = mysql_query("SELECT survey_name, survey_createdon FROM site_surveys WHERE surveyid='$surveyid' LIMIT 1");
     $snamerow = mysql_fetch_array($sname); 
     $survey_name = stripslashes($snamerow['survey_name']);
     $survey_name = str_replace("'", "", $survey_name);
     $survey_name = str_replace(' ', '_', $survey_name);

     function cleanData(&$str)
     {
         if($str == 't') $str = 'TRUE';
         if($str == 'f') $str = 'FALSE';
         if(preg_match("/^0/", $str) || preg_match("/^\+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str)) 
           {
         $str = "'$str";
           }
         if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
    }


   header("Content-Disposition: attachment; filename=\"$survey_name.csv\"");
   header("Content-Type: text/csv");

   $out = fopen("php://output", 'w');

   $flag = false;
   $result = mysql_query("SELECT survey_comments.the_comment, DATE_FORMAT(survey_comments.commentdate, '%m/%d/%Y %r') AS comment_date, users.fname, users.lname, users.email FROM survey_comments, users 
                     WHERE survey_comments.surveyid = $surveyid AND survey_comments.uid = users.id
                     ORDER BY survey_comments.commentdate desc") or die('Query failed!');
   while(false !== ($row = mysql_fetch_assoc($result))) {
   if(!$flag) 
{
  // display field/column names as first row
  fputcsv($out, array_keys($row), ',', '"');
  $flag = true;
} 

array_walk($row, 'cleanData');
fputcsv($out, array_values($row), ',', '"');
}

  fclose($out);
  exit;
 ?>

0 个答案:

没有答案