diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 26d9f38fcf6..8cf0462d606 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -9805,13 +9805,21 @@ function printCommonFooter($zone = 'private') * For example: "A=1;B=2;C=2" is exploded into array('A'=>1,'B'=>2,'C'=>3) * * @param string $string String to explode - * @param string $delimiter Delimiter between each couple of data + * @param string $delimiter Delimiter between each couple of data. Example: ';' or '[\n;]+' or '(\n\r|\r|\n|;)' * @param string $kv Delimiter between key and value * @return array Array of data exploded */ function dolExplodeIntoArray($string, $delimiter = ';', $kv = '=') { - if ($a = explode($delimiter, $string)) { + if (preg_match('/^\[.*\]$/sm', $delimiter) || preg_match('/^\(.*\)$/sm', $delimiter)) { + // This is a regex string + $newdelimiter = $delimiter; + } else { + // This is a simple string + $newdelimiter = preg_quote($delimiter, '/'); + } + + if ($a = preg_split('/'.$newdelimiter.'/', $string)) { $ka = array(); foreach ($a as $s) { // each part if ($s) { @@ -9824,6 +9832,7 @@ function dolExplodeIntoArray($string, $delimiter = ';', $kv = '=') } return $ka; } + return array(); } diff --git a/test/phpunit/FunctionsLibTest.php b/test/phpunit/FunctionsLibTest.php index b7e3b4c7555..e635e449379 100644 --- a/test/phpunit/FunctionsLibTest.php +++ b/test/phpunit/FunctionsLibTest.php @@ -1381,6 +1381,12 @@ class FunctionsLibTest extends PHPUnit\Framework\TestCase print __METHOD__." tmp=".json_encode($tmp)."\n"; $this->assertEquals('{"AA":"B\/B","CC":"","EE":"FF","HH":"GG;"}', json_encode($tmp)); + + $stringtoexplode="AA=B/B;CC=\n\rEE=FF\nHH=GG;;;\nII=JJ\n"; + $tmp=dolExplodeIntoArray($stringtoexplode, "(\r\n|\n|\r|;)", '='); + + print __METHOD__." tmp=".json_encode($tmp)."\n"; + $this->assertEquals('{"AA":"B\/B","CC":"","EE":"FF","HH":"GG","II":"JJ"}', json_encode($tmp)); } /**