Index: admin/export_data_functions.php
===================================================================
--- admin/export_data_functions.php	(revision 9992)
+++ admin/export_data_functions.php	(working copy)
@@ -29,6 +29,21 @@
 }
 
 /**
+ * Strips html tags
+ *
+ * @param $string
+ * @return $string
+ */
+function strip_tags_full_save_newline($string) {
+    $string=html_entity_decode($string, ENT_QUOTES, "UTF-8");
+    //combining these into one mb_ereg_replace call ought to speed things up
+    //$string = str_replace(array("\r\n","\r","\n",'-oth-'), '', $string);
+    //The backslashes must be escaped twice, once for php, and again for the regexp
+    //$string = str_replace("'|\\\\'", "&apos;", $string);
+    return FlattenTextWithNewline($string);
+}
+
+/**
  * Returns true if passed $value is numeric
  *
  * @param $value
@@ -46,9 +61,8 @@
     return ($eng_or_world);
 }
 
-function spss_export_data ($na = null) {
+function spss_export_data ($na = null, $is_csv = false) {
     global $length_data;
-
     // Build array that has to be returned
     $fields = spss_fieldmap();
 
@@ -138,9 +152,19 @@
                     echo("'0'");
                 }
             } elseif (!$field['hide']) {
-                $strTmp=mb_substr(strip_tags_full($row[$fieldno]), 0, $length_data);
+				if($is_csv == true) {
+					$strTmp=mb_substr(strip_tags_full_save_newline($row[$fieldno]), 0, $length_data);
+				}
+				else {
+					$strTmp=mb_substr(strip_tags_full($row[$fieldno]), 0, $length_data);
+				}
                 if (trim($strTmp) != ''){
-                    $strTemp=str_replace(array("'","\n","\r"),array("''",' ',' '),trim($strTmp));
+					if($is_csv == true) {
+						$strTemp=str_replace(array("'"),array("''"),trim($strTmp));
+					}
+					else {
+						$strTemp=str_replace(array("'","\n","\r"),array("''",' ',' '),trim($strTmp));
+					}
                     /*
                      * Temp quick fix for replacing decimal dots with comma's
                      if (my_is_numeric($strTemp)) {
Index: admin/export_data_r.php
===================================================================
--- admin/export_data_r.php	(revision 9992)
+++ admin/export_data_r.php	(working copy)
@@ -138,7 +138,7 @@
     header("Pragma: public");
 
     $na="";	//change to empty string instead of two double quotes to fix warnings on NA
-    spss_export_data($na);
+    spss_export_data($na, true); // this true indicates that the format is .csv
 
     exit;
 }
Index: common_functions.php
===================================================================
--- common_functions.php	(revision 9992)
+++ common_functions.php	(working copy)
@@ -5008,7 +5008,7 @@
     $sNicetext = strip_javascript($sTextToFlatten);
     $sNicetext = strip_tags($sNicetext);
     $sNicetext = str_replace(array("\n","\r"),array('',''), $sNicetext);
-    if ($bDecodeHTMLEntities==true)
+	if ($bDecodeHTMLEntities==true)
     {
         $sNicetext = str_replace('&nbsp;',' ', $sNicetext); // html_entity_decode does not properly convert &nbsp; to spaces
         $sNicetext = html_entity_decode($sNicetext, ENT_QUOTES, $sCharset);
@@ -5019,6 +5019,30 @@
 
 
 /**
+ *  This functions removes all HTML tags, Javascript, and other strange chars from a given text
+ *
+ * @param string $sTextToFlatten  Text you want to clean
+ * @param boolan $bDecodeHTMLEntities If set to true then all HTML entities will be decoded to the specified charset. Default: false
+ * @param string $sCharset Charset to decode to if $decodeHTMLEntities is set to true
+ * 
+ * @return string  Cleaned text
+ */
+function FlattenTextWithNewline($sTextToFlatten, $bDecodeHTMLEntities=false, $sCharset='UTF-8')
+{
+    $sNicetext = strip_javascript($sTextToFlatten);
+    $sNicetext = strip_tags($sNicetext);
+    $sNicetext = str_replace(array("\r\n","\r","\n"),array(PHP_EOL,PHP_EOL,PHP_EOL), $sNicetext);
+	if ($bDecodeHTMLEntities==true)
+    {
+        $sNicetext = str_replace('&nbsp;',' ', $sNicetext); // html_entity_decode does not properly convert &nbsp; to spaces
+        $sNicetext = html_entity_decode($sNicetext, ENT_QUOTES, $sCharset);
+    }
+    $sNicetext = trim($sNicetext);
+    return  $sNicetext;
+}
+
+
+/**
  * getArrayFiltersForGroup() queries the database and produces a list of array_filter questions and targets with in the same group
  * @global string $surveyid
  * @global string $gid
