Commit 9ec28777 authored by Sebastian Kummer's avatar Sebastian Kummer

ZP-1240 Add parameter $htmlsafe (default false) to

Utils::Utf8_truncate().

Released under the Affero GNU General Public License (AGPL) version 3.
parent 774744c6
......@@ -380,11 +380,13 @@ class Utils {
*
* If it's not possible to truncate properly, an empty string is returned
*
* @param string $string - the string
* @param string $length - position where string should be cut
* @param string $string the string
* @param string $length position where string should be cut
* @param boolean $htmlsafe doesn't cut html tags in half, doesn't ensure correct html - default: false
*
* @return string truncated string
*/
static public function Utf8_truncate($string, $length) {
static public function Utf8_truncate($string, $length, $htmlsafe = false) {
// make sure length is always an interger
$length = (int)$length;
......@@ -393,6 +395,16 @@ class Utils {
$length = strlen($string) - 1;
}
// The intent is not to cut HTML tags in half which causes displaying issues (see ZP-1240).
// The used method just tries to cut outside of tags, without checking tag validity and closing tags.
if ($htmlsafe) {
$offset = 0 - strlen($string) + $length;
$validPos = strrpos($string, "<", $offset);
if ($validPos > strrpos($string, ">", $offset)) {
$length = $validPos;
}
}
while($length >= 0) {
if ((ord($string[$length]) < 0x80) || (ord($string[$length]) >= 0xC0)) {
return substr($string, 0, $length);
......
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