I have a php file that I am using to export specific columns from two mysql tables to a csv file. My issue is one of the columns contain html which I need in there when displaying the columns content in a webpage since its content from a wysiwyg textbox. However when I am exporting the column to csv I do not want the html in there. However I am not sure how to strip_tags() so that the column that is written to csv contains no html?
Here is my code
<?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;
?>