Commit 2e8bd067 authored by Sebastian Kummer's avatar Sebastian Kummer

ZP-1199 Utils::Utf8_truncate() needs to check that a string is valid

UTF-8 even if it's shorter than the requested length.

Released under the Affero GNU General Public License (AGPL) version 3.
parent e232e4c6
...@@ -388,13 +388,15 @@ class Utils { ...@@ -388,13 +388,15 @@ class Utils {
// make sure length is always an interger // make sure length is always an interger
$length = (int)$length; $length = (int)$length;
if (strlen($string) <= $length) // if the input string is shorter then the trunction, make sure it's valid UTF-8!
return $string; if (strlen($string) <= $length) {
$length = strlen($string) - 1;
}
while($length >= 0) { while($length >= 0) {
if ((ord($string[$length]) < 0x80) || (ord($string[$length]) >= 0xC0)) if ((ord($string[$length]) < 0x80) || (ord($string[$length]) >= 0xC0)) {
return substr($string, 0, $length); return substr($string, 0, $length);
}
$length--; $length--;
} }
return ""; return "";
...@@ -1177,7 +1179,7 @@ class Utils { ...@@ -1177,7 +1179,7 @@ class Utils {
} }
/** /**
* Check if the UTF-8 string has ISO-2022-JP esc seq * Check if the UTF-8 string has ISO-2022-JP esc seq
* if so, it is ISO-2022-JP, not UTF-8 and convert it into UTF-8 * if so, it is ISO-2022-JP, not UTF-8 and convert it into UTF-8
* string * string
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment