4
4
5
5
class LineBreaksHelper
6
6
{
7
+ const REGEXP_DELIMITER = '~ ' ;
8
+
7
9
/**
8
10
* Detect line-breaks style in CSV file
9
11
* @param string $sample
@@ -60,13 +62,12 @@ public static function clearCsvValues($sample, $enclosure, $escapedBy)
60
62
* enclosure: |"|, escapedBy: none, regexp: ~"(?>(?>"")|[^"])*"~
61
63
* enclosure: |"|, escapedBy: |\|, regexp: ~"(?>(?>\\"|\\\\)|[^"])*"~
62
64
*/
63
- $ regexpDelimiter = '~ ' ;
64
65
// @formatter:off
65
66
$ regexp =
66
67
// regexp start
67
- $ regexpDelimiter .
68
+ self :: REGEXP_DELIMITER .
68
69
// enclosure start
69
- preg_quote ($ enclosure , $ regexpDelimiter ) .
70
+ preg_quote ($ enclosure , self :: REGEXP_DELIMITER ) .
70
71
/*
71
72
* Once-only group => if there is a match, do not try other alternatives
72
73
* See: https://www.php.net/manual/en/regexp.reference.onlyonce.php
@@ -78,19 +79,19 @@ public static function clearCsvValues($sample, $enclosure, $escapedBy)
78
79
// once-only group start
79
80
'(?> ' .
80
81
// escaped enclosure
81
- preg_quote ($ escapedEnclosure , $ regexpDelimiter ) .
82
+ preg_quote ($ escapedEnclosure , self :: REGEXP_DELIMITER ) .
82
83
// OR escaped escape char
83
- ($ escapedEscape ? '| ' . preg_quote ($ escapedEscape , $ regexpDelimiter ) : '' ) .
84
+ ($ escapedEscape ? '| ' . preg_quote ($ escapedEscape , self :: REGEXP_DELIMITER ) : '' ) .
84
85
// group end
85
86
') ' .
86
87
// OR not enclosure
87
- '|[^ ' . preg_quote ($ enclosure , $ regexpDelimiter ) . '] ' .
88
+ '|[^ ' . preg_quote ($ enclosure , self :: REGEXP_DELIMITER ) . '] ' .
88
89
// group end
89
90
')* ' .
90
91
// enclosure end
91
- preg_quote ($ enclosure , $ regexpDelimiter ) .
92
+ preg_quote ($ enclosure , self :: REGEXP_DELIMITER ) .
92
93
// regexp end
93
- $ regexpDelimiter ;
94
+ self :: REGEXP_DELIMITER ;
94
95
// @formatter:on
95
96
96
97
return preg_replace ($ regexp , $ doubleEnclosure , $ sample );
0 commit comments