From 721c9a6f5b8da55ceaa67fce105607ddb26f0231 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 21 Dec 2011 16:10:11 +0100 Subject: [PATCH] Fix: [ bug #253 ] Non UTF-8 characters in CSV exports --- htdocs/core/lib/functions.lib.php | 5 +++-- htdocs/core/modules/export/export_csv.modules.php | 10 +++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 18fa9a2dbaa36..2d4ef228a37da 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -3524,12 +3524,13 @@ function picto_required() * * @param StringHtml String to clean * @param removelinefeed Replace also all lines feeds by a space + * @param pagecodeto Encoding of input string * @return string String cleaned */ -function dol_string_nohtmltag($StringHtml,$removelinefeed=1) +function dol_string_nohtmltag($StringHtml,$removelinefeed=1,$pagecodeto='UTF-8') { $pattern = "/<[^>]+>/"; - $temp = dol_entity_decode($StringHtml); + $temp = dol_entity_decode($StringHtml,$pagecodeto); $temp = preg_replace($pattern,"",$temp); // Supprime aussi les retours diff --git a/htdocs/core/modules/export/export_csv.modules.php b/htdocs/core/modules/export/export_csv.modules.php index 8d6939757b7f3..ff3217cfe990e 100644 --- a/htdocs/core/modules/export/export_csv.modules.php +++ b/htdocs/core/modules/export/export_csv.modules.php @@ -171,7 +171,7 @@ function write_title($array_export_fields_label,$array_selected_sorted,$outputla foreach($array_selected_sorted as $code => $value) { $newvalue=$outputlangs->transnoentities($array_export_fields_label[$code]); - $newvalue=$this->csv_clean($newvalue); + $newvalue=$this->csv_clean($newvalue,$outputlangs->charset_output); fwrite($this->handle,$newvalue.$this->separator); } @@ -206,7 +206,6 @@ function write_record($array_selected_sorted,$objp,$outputlangs) { $alias=str_replace(array('.','-'),'_',$code); if (empty($alias)) dol_print_error('','Bad value for field with key='.$code.'. Try to redefine export.'); - $newvalue=$outputlangs->convToOutputCharset($objp->$alias); // Translation newvalue @@ -215,7 +214,7 @@ function write_record($array_selected_sorted,$objp,$outputlangs) $newvalue=$outputlangs->transnoentities($reg[1]); } - $newvalue=$this->csv_clean($newvalue); + $newvalue=$this->csv_clean($newvalue,$outputlangs->charset_output); fwrite($this->handle,$newvalue.$this->separator); $this->col++; @@ -251,14 +250,15 @@ function close_file() * Clean a cell to respect rules of CSV file cells * * @param string $newvalue String to clean + * @param string $charset Output character set * @return string Value cleaned */ - function csv_clean($newvalue) + function csv_clean($newvalue, $charset) { $addquote=0; // Rule Dolibarr: No HTML - $newvalue=dol_string_nohtmltag($newvalue); + $newvalue=dol_string_nohtmltag($newvalue,1,$charset); // Rule 1 CSV: No CR, LF in cells $newvalue=str_replace("\r",'',$newvalue);