From 86456e299c281df6e8b5864912e51df7e46a10a8 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 26 Apr 2023 18:25:56 +0200 Subject: [PATCH] NEW add option keepspace into dol_string_nospecialchar() --- .../prelevement/class/bonprelevement.class.php | 4 ++-- htdocs/core/lib/functions.lib.php | 8 ++++++-- test/phpunit/FunctionsLibTest.php | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/htdocs/compta/prelevement/class/bonprelevement.class.php b/htdocs/compta/prelevement/class/bonprelevement.class.php index 022bb3f6c03..131d55bee63 100644 --- a/htdocs/compta/prelevement/class/bonprelevement.class.php +++ b/htdocs/compta/prelevement/class/bonprelevement.class.php @@ -1997,7 +1997,7 @@ class BonPrelevement extends CommonObject $XML_DEBITOR .= ' '.$CrLf; $XML_DEBITOR .= ' '.$CrLf; // A string with some information on payment - 140 max - $XML_DEBITOR .= ' '.getDolGlobalString('PRELEVEMENT_USTRD', dolEscapeXML(dol_trunc(dol_string_nospecial(dol_string_unaccent($row_ref.($row_comment ? ' - '.$row_comment : '')), ''), 135, 'right', 'UTF-8', 1))).''.$CrLf; // Free unstuctured data - 140 max + $XML_DEBITOR .= ' '.getDolGlobalString('PRELEVEMENT_USTRD', dolEscapeXML(dol_trunc(dol_string_nospecial(dol_string_unaccent($row_ref.($row_comment ? ' - '.$row_comment : '')), '', '', '', 1), 135, 'right', 'UTF-8', 1))).''.$CrLf; // Free unstuctured data - 140 max $XML_DEBITOR .= ' '.$CrLf; $XML_DEBITOR .= ' '.$CrLf; return $XML_DEBITOR; @@ -2066,7 +2066,7 @@ class BonPrelevement extends CommonObject $XML_CREDITOR .= ' '.$CrLf; $XML_CREDITOR .= ' '.$CrLf; // A string with some information on payment - 140 max - $XML_CREDITOR .= ' '.getDolGlobalString('CREDITTRANSFER_USTRD', dolEscapeXML(dol_trunc(dol_string_nospecial(dol_string_unaccent($row_ref.($row_comment ? ' - '.$row_comment : '')), '')), 135, 'right', 'UTF-8', 1)).''.$CrLf; // Free unstructured data - 140 max + $XML_CREDITOR .= ' '.getDolGlobalString('CREDITTRANSFER_USTRD', dolEscapeXML(dol_trunc(dol_string_nospecial(dol_string_unaccent($row_ref.($row_comment ? ' - '.$row_comment : '')), '', '', '', 1)), 135, 'right', 'UTF-8', 1)).''.$CrLf; // Free unstructured data - 140 max $XML_CREDITOR .= ' '.$CrLf; $XML_CREDITOR .= ' '.$CrLf; return $XML_CREDITOR; diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 88fe167b73d..db8ebc57c70 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -1447,13 +1447,17 @@ function dol_string_unaccent($str) * @param string $newstr String to replace forbidden chars with * @param array|string $badcharstoreplace Array of forbidden characters to replace. Use '' to keep default list. * @param array|string $badcharstoremove Array of forbidden characters to remove. Use '' to keep default list. + * @param int $keepspaces 1=Do not treat space as a special char to replace or remove * @return string Cleaned string * * @see dol_sanitizeFilename(), dol_string_unaccent(), dol_string_nounprintableascii() */ -function dol_string_nospecial($str, $newstr = '_', $badcharstoreplace = '', $badcharstoremove = '') +function dol_string_nospecial($str, $newstr = '_', $badcharstoreplace = '', $badcharstoremove = '', $keepspaces = 0) { - $forbidden_chars_to_replace = array(" ", "'", "/", "\\", ":", "*", "?", "\"", "<", ">", "|", "[", "]", ",", ";", "=", '°', '$', ';'); // more complete than dol_sanitizeFileName + $forbidden_chars_to_replace = array("'", "/", "\\", ":", "*", "?", "\"", "<", ">", "|", "[", "]", ",", ";", "=", '°', '$', ';'); // more complete than dol_sanitizeFileName + if (empty($keepspaces)) { + $forbidden_chars_to_replace[] = " "; + } $forbidden_chars_to_remove = array(); //$forbidden_chars_to_remove=array("(",")"); diff --git a/test/phpunit/FunctionsLibTest.php b/test/phpunit/FunctionsLibTest.php index c361698a501..bf2ab232ec4 100644 --- a/test/phpunit/FunctionsLibTest.php +++ b/test/phpunit/FunctionsLibTest.php @@ -673,6 +673,24 @@ class FunctionsLibTest extends PHPUnit\Framework\TestCase } + /** + * testDolStringNoSpecial + * + * @return boolean + */ + public function testDolStringNoSpecial() + { + $text="A string with space and special char like ' or ° and more...\n"; + $after=dol_string_nospecial($text, '_', '', '', 0); + $this->assertEquals("A_string_with_space_and_special_char_like___or___and_more...\n", $after, "testDolStringNoSpecial 1"); + + $text="A string with space and special char like ' or ° and more...\n"; + $after=dol_string_nospecial($text, '_', '', '', 1); + $this->assertEquals("A string with space and special char like _ or _ and more...\n", $after, "testDolStringNoSpecial 2"); + + return true; + } + /** * testDolStringNohtmltag *