From 00db2ab402d39afeda33b8cf593a4083bf9089a6 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Thu, 13 Feb 2025 22:39:13 +0900 Subject: [PATCH] Fix broken emoji and other HTML entities when using cut_str() https://xetown.com/questions/1855257 --- common/legacy.php | 3 ++- tests/unit/functions/LegacyTest.php | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/common/legacy.php b/common/legacy.php index 0c8871059..e254d38eb 100644 --- a/common/legacy.php +++ b/common/legacy.php @@ -495,7 +495,8 @@ function cut_str($string, $cut_size = 0, $tail = '...'): string if(isset($GLOBALS['use_mb_strimwidth']) || function_exists('mb_strimwidth')) { $GLOBALS['use_mb_strimwidth'] = TRUE; - return mb_strimwidth($string, 0, $cut_size + 4, $tail, 'utf-8'); + $string = html_entity_decode($string); + return escape(mb_strimwidth($string, 0, $cut_size + 4, $tail, 'utf-8')); } $chars = array(12, 4, 3, 5, 7, 7, 11, 8, 4, 5, 5, 6, 6, 4, 6, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 4, 8, 6, 8, 6, 10, 8, 8, 9, 8, 8, 7, 9, 8, 3, 6, 7, 7, 11, 8, 9, 8, 9, 8, 8, 7, 8, 8, 10, 8, 8, 8, 6, 11, 6, 6, 6, 4, 7, 7, 7, 7, 7, 3, 7, 7, 3, 3, 6, 3, 9, 7, 7, 7, 7, 4, 7, 3, 7, 6, 10, 6, 6, 7, 6, 6, 6, 9); diff --git a/tests/unit/functions/LegacyTest.php b/tests/unit/functions/LegacyTest.php index f1d345873..be16242c7 100644 --- a/tests/unit/functions/LegacyTest.php +++ b/tests/unit/functions/LegacyTest.php @@ -81,6 +81,8 @@ class LegacyTest extends \Codeception\Test\Unit { $this->assertEquals('안녕하세요? 라이믹스...', cut_str('안녕하세요? 라이믹스입니다. 제목이 너무 길어서 잘립니다.', 20)); $this->assertEquals('Hello? This is Rhymix...', cut_str('Hello? This is Rhymix. This title is very long.', 20)); + $this->assertEquals('Hello <world> 😁', cut_str('Hello <world> 😁', 20)); + $this->assertEquals('Hello "Rhymix" test &...', cut_str('Hello "Rhymix" test &amp;', 20)); } public function testTimeFunctions()