diff --git a/libraries/ParserCSV.inc b/libraries/ParserCSV.inc index bdd8890..5380201 100644 --- a/libraries/ParserCSV.inc +++ b/libraries/ParserCSV.inc @@ -66,6 +66,8 @@ class ParserCSVIterator implements Iterator { */ class ParserCSV { private $delimiter; + private $fromEncoding; + private $toEncoding; private $skipFirstLine; private $columnNames; private $timeout; @@ -73,11 +75,12 @@ class ParserCSV { private $startByte; private $lineLimit; private $lastLinePos; + private $useMbString; public function __construct() { $this->delimiter = ','; - $this->from_encoding = 'UTF-8'; - $this->to_encoding = 'UTF-8'; + $this->fromEncoding = 'UTF-8'; + $this->toEncoding = 'UTF-8'; $this->skipFirstLine = FALSE; $this->columnNames = FALSE; $this->timeout = FALSE; @@ -86,6 +89,9 @@ class ParserCSV { $this->lineLimit = 0; $this->lastLinePos = 0; ini_set('auto_detect_line_endings', TRUE); + if (extension_loaded('mbstring') && variable_get('feeds_use_mbstring', TRUE)) { + $this->useMbString = TRUE; + } } /** @@ -105,7 +111,7 @@ class ParserCSV { * The encoding to set. */ public function setEncoding($encoding) { - $this->from_encoding = $encoding; + $this->fromEncoding = $encoding; } /** @@ -352,15 +358,15 @@ class ParserCSV { * @throws ParserCSVEncodingException * Thrown when a given encoding does not match. */ - private function fixEncoding($data) { - if (extension_loaded('mbstring') && variable_get('feeds_use_mbstring', TRUE)) { - if (mb_check_encoding($data, $this->from_encoding)) { + public function fixEncoding($data) { + if ($this->useMbString && $this->toEncoding != $this->fromEncoding) { + if (mb_check_encoding($data, $this->fromEncoding)) { // Convert encoding. The conversion is to UTF-8 by default to prevent // SQL errors. - $data = mb_convert_encoding($data, $this->to_encoding, $this->from_encoding); + $data = mb_convert_encoding($data, $this->toEncoding, $this->fromEncoding); } else { - throw new ParserCSVEncodingException(t('Source file is not in %encoding encoding.', array('%encoding' => $this->from_encoding))); + throw new ParserCSVEncodingException(t('Source file is not in %encoding encoding.', array('%encoding' => $this->fromEncoding))); } }