From fad5ebcdc82a2a08e3c9c2bbe3a47583a8a8a75f Mon Sep 17 00:00:00 2001 From: Jan Piskvor Martinec Date: Fri, 24 Nov 2017 17:52:39 +0100 Subject: [PATCH] Added function to detect delimiter using sep= file header --- parsecsv.lib.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/parsecsv.lib.php b/parsecsv.lib.php index d6fd2d2..e324bff 100644 --- a/parsecsv.lib.php +++ b/parsecsv.lib.php @@ -1165,4 +1165,27 @@ class parseCSV { return false; } + + /** + * Detect separator using a nonstandard hack: such file starts with the + * first line containing only "sep=;", where the last character is the + * separator. + * + * @access protected + * + * @param string $data file data + * + * @return string|false detected delimiter, or false if none found + */ + protected function _get_delimiter_from_sep($data) { + $sep = false; + // 32 bytes should be quite enough data for our sniffing, chosen arbitrarily + $sepPrefix = substr($data, 0, 32); + + if (preg_match('/^sep=(.)/i', $sepPrefix, $sepMatch)) { + // we get separator. + $sep = $sepMatch[1]; + } + return $sep; + } }