From 0810302e4f66022fc521fab73ea1d2ef3c795bbf Mon Sep 17 00:00:00 2001 From: Joshua Sherman Date: Sun, 26 Jan 2014 15:09:16 -0500 Subject: [PATCH] Fixed issue with UTF-8 2-byte characters Characters were being split up and causing invalid sequences when using `substr()`. Went ahead and updated to use `mb_strcut()` and forcing the character encoding to UTF-8. I think the plan down the road will be to set the internal encoding to UTF-8 but I am not currently sure how that could effect the rest of the system (perhaps it won't). Closes #39 --- src/classes/String.php | 4 ++-- tests/classes/StringTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/classes/String.php b/src/classes/String.php index 08cf8f6..98fcae9 100644 --- a/src/classes/String.php +++ b/src/classes/String.php @@ -225,11 +225,11 @@ class String { if ($hover == true) { - $string = '' . substr($string, 0, $length) . '…'; + $string = '' . mb_strcut($string, 0, $length, 'UTF-8') . '…'; } else { - $string = substr($string, 0, $length) . '...'; + $string = mb_strcut($string, 0, $length, 'UTF-8') . '…'; } } diff --git a/tests/classes/StringTest.php b/tests/classes/StringTest.php index 6d60eac..0b93eb3 100644 --- a/tests/classes/StringTest.php +++ b/tests/classes/StringTest.php @@ -86,7 +86,7 @@ class StringTest extends PHPUnit_Framework_TestCase { return [ ['foo bar', 3, true, 'foo…'], - ['foo bar', 3, false, 'foo...'], + ['foo bar', 3, false, 'foo…'], ['foo bar', 7, true, 'foo bar'], ['foo bar', 8, true, 'foo bar'], ];