mirror of
https://github.com/humhub/humhub.git
synced 2025-01-17 06:08:21 +01:00
Fix truncating of multi-byte strings (#5372)
* Fix truncating of multi-byte strings * Test for truncating multi-byte strings
This commit is contained in:
parent
e31dd9eff0
commit
61f82cb693
@ -22,38 +22,29 @@ class Helpers
|
||||
|
||||
/**
|
||||
* Shorten a text string
|
||||
*
|
||||
* @param string $text - Text string you will shorten
|
||||
* @param integer $length - Count of characters to show
|
||||
*
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function truncateText($text, $length)
|
||||
public static function truncateText($text, $length): string
|
||||
{
|
||||
$length = abs((int)$length);
|
||||
if (strlen($text) > $length) {
|
||||
$text = preg_replace("/^(.{1,$length})(\s.*|$)/s", '\\1...', $text);
|
||||
}
|
||||
$text = str_replace('<br />', '', $text);
|
||||
|
||||
return trim($text);
|
||||
return self::trimText($text, $length);
|
||||
}
|
||||
|
||||
public static function trimText($text, $length)
|
||||
public static function trimText($text, $length): string
|
||||
{
|
||||
$text = trim(preg_replace('#<br */?>#i', ' ', $text));
|
||||
|
||||
$length = abs((int)$length);
|
||||
$textlength = mb_strlen($text);
|
||||
if ($textlength > $length) {
|
||||
$text = self::substru($text, 0, $textlength - ($textlength - $length));
|
||||
$text .= '...';
|
||||
if (mb_strlen($text) > $length) {
|
||||
$text = trim(mb_substr($text, 0, $length)) . '...';
|
||||
}
|
||||
$text = str_replace('<br />', '', $text);
|
||||
|
||||
return trim($text);
|
||||
return $text;
|
||||
}
|
||||
|
||||
/* *
|
||||
/**
|
||||
* Compare two arrays values
|
||||
* @param array $a - First array to compare against..
|
||||
* @param array $b - Second array
|
||||
@ -75,13 +66,14 @@ class Helpers
|
||||
|
||||
/**
|
||||
* Temp Function to use UTF8 SubStr
|
||||
* @deprecated since 1.11 Use mb_substr() instead.
|
||||
*
|
||||
* @param type $str
|
||||
* @param type $from
|
||||
* @param type $len
|
||||
* @return type
|
||||
* @param string $str
|
||||
* @param integer $from
|
||||
* @param integer $len
|
||||
* @return string
|
||||
*/
|
||||
public static function substru($str, $from, $len)
|
||||
public static function substru($str, $from, $len): string
|
||||
{
|
||||
return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $from . '}' . '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $len . '}).*#s', '$1', $str);
|
||||
}
|
||||
|
@ -46,6 +46,13 @@ class RichTextShortTextConverterTest extends HumHubDbTestCase
|
||||
"Test...", ['maxLength' => 5]);
|
||||
}
|
||||
|
||||
public function testConvertMultiByteTextWithMaxLength()
|
||||
{
|
||||
$this->assertConversionResult(
|
||||
'相*ウ*ヨ報<br />夫チエ**景東署**シイ連苦ワ径特サニコワ政深ちぎ見敗ぜあじも内庫ゅしづぽ児意泉ねッを黒能わぱふ緩昇ろじ帯北悩びぞば。',
|
||||
"相ウヨ報 夫チエ景東...", [RichTextToShortTextConverter::OPTION_MAX_LENGTH => 10]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \yii\base\InvalidConfigException
|
||||
*/
|
||||
@ -781,7 +788,7 @@ class RichTextShortTextConverterTest extends HumHubDbTestCase
|
||||
"This **is a long** text we will truncate",
|
||||
"This is a...", [
|
||||
RichTextToShortTextConverter::OPTION_CACHE_KEY => 'test2',
|
||||
RichTextToShortTextConverter::OPTION_MAX_LENGTH => 11
|
||||
RichTextToShortTextConverter::OPTION_MAX_LENGTH => 9
|
||||
]);
|
||||
|
||||
$this->assertConversionResult(
|
||||
|
@ -116,7 +116,7 @@ class RichTextToPlainTextConverter extends RichTextToMarkdownConverter
|
||||
|
||||
foreach ($paragraph as $inline) {
|
||||
if(isset($inline[0], $inline[1]) && $inline[0] === 'text') {
|
||||
$this->textCount += strlen($inline[1]);
|
||||
$this->textCount += mb_strlen($inline[1]);
|
||||
if($this->textCount > $maxLength) {
|
||||
$this->skipBlocks = true;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user