Skip to content

Commit 84759c0

Browse files
committed
fix: Add support multibyte to View->excerpt()
1 parent 3216928 commit 84759c0

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

system/View/View.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ public function renderString(string $view, ?array $options = null, ?bool $saveDa
339339
*/
340340
public function excerpt(string $string, int $length = 20): string
341341
{
342-
return (strlen($string) > $length) ? substr($string, 0, $length - 3) . '...' : $string;
342+
return (mb_strlen($string) > $length) ? mb_substr($string, 0, $length - 3) . '...' : $string;
343343
}
344344

345345
/**

tests/system/View/ViewTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,4 +398,12 @@ public function testRenderSectionSavingData(): void
398398
$view->setVar('testString', 'Hello World');
399399
$this->assertStringContainsString($expected, $view->render('extend_reuse_section'));
400400
}
401+
402+
public function testViewExcerpt(): void
403+
{
404+
$view = new View($this->config, $this->viewsDir, $this->loader);
405+
406+
$this->assertSame('CodeIgniter is a PHP full-stack web framework...', $view->excerpt('CodeIgniter is a PHP full-stack web framework that is light, fast, flexible and secure.', 48));
407+
$this->assertSame('CodeIgniter - это полнофункциональный веб-фреймворк...', $view->excerpt('CodeIgniter - это полнофункциональный веб-фреймворк на PHP, который является легким, быстрым, гибким и безопасным.', 54));
408+
}
401409
}

0 commit comments

Comments
 (0)