Skip to content

EspoCRM – Open Source CRM Application

Fix string\pad counting bytes instead of characters (#3798)

Leo Camusfca520824f69
diff --git a/application/Espo/Core/Formula/Functions/StringGroup/PadType.php b/application/Espo/Core/Formula/Functions/StringGroup/PadType.php
index 860c0dfe6f..d606e89ee7 100644
--- a/application/Espo/Core/Formula/Functions/StringGroup/PadType.php
+++ b/application/Espo/Core/Formula/Functions/StringGroup/PadType.php
@@ -65,6 +65,6 @@ class PadType extends BaseFunction
$padType = $map[$type] ?? \STR_PAD_RIGHT;
- return str_pad($input, $length, $string, $padType);
+ return mb_str_pad($input, $length, $string, $padType);
}
}
diff --git a/tests/unit/Espo/Core/Formula/EvaluatorTest.php b/tests/unit/Espo/Core/Formula/EvaluatorTest.php
index aad2640384..17930952df 100644
--- a/tests/unit/Espo/Core/Formula/EvaluatorTest.php
+++ b/tests/unit/Espo/Core/Formula/EvaluatorTest.php
@@ -266,6 +266,17 @@ class EvaluatorTest extends TestCase
$this->assertEquals('0110', $actual);
}
+ public function testStringPadMultibyte()
+ {
+ $expression = "string\\pad('Zoé', 5, '0', 'left')";
+ $actual = $this->evaluator->process($expression);
+ $this->assertEquals('00Zoé', $actual);
+
+ $expression = "string\\pad('1', 3, '€')";
+ $actual = $this->evaluator->process($expression);
+ $this->assertEquals('1€€', $actual);
+ }
+
public function testStringMatchAll()
{
$expression = "string\\matchAll('{token1} foo {token2} bar', '/{[^}]*}/')";