Browse Source

use mb_string function

antograssiot 10 years ago
parent
commit
a7d16c0060
2 changed files with 19 additions and 2 deletions
  1. 2 2
      src/Utility/Text.php
  2. 17 0
      tests/TestCase/Utility/TextTest.php

+ 2 - 2
src/Utility/Text.php

@@ -342,7 +342,7 @@ class Text
         $wrapped = self::wrap($text, $options);
 
         if (!empty($options['indent'])) {
-            $indentationLength = strlen($options['indent']);
+            $indentationLength = mb_strlen($options['indent']);
             $chunks = explode("\n", $wrapped);
             $count = count($chunks);
             if ($count < 2) {
@@ -350,7 +350,7 @@ class Text
             }
             $toRewrap = '';
             for ($i = $options['indentAt']; $i < $count; $i++) {
-                $toRewrap .= substr($chunks[$i], $indentationLength) . ' ';
+                $toRewrap .= mb_substr($chunks[$i], $indentationLength) . ' ';
                 unset($chunks[$i]);
             }
             $options['width'] -= $indentationLength;

+ 17 - 0
tests/TestCase/Utility/TextTest.php

@@ -504,6 +504,23 @@ TEXT;
     }
 
     /**
+     * test wrapBlock() indenting with multibyte caracters
+     *
+     * @return void
+     */
+    public function testWrapBlockIndentWithMultibyte()
+    {
+        $text = 'This is the song that never ends. 这是永远不会结束的歌曲。 This is the song that never ends.';
+        $result = Text::wrapBlock($text, ['width' => 33, 'indent' => " → ", 'indentAt' => 1]);
+        $expected = <<<TEXT
+This is the song that never ends.
+ → 这是永远不会结束的歌曲。 This is the song
+ → that never ends.
+TEXT;
+        $this->assertTextEquals($expected, $result);
+    }
+
+    /**
      * testTruncate method
      *
      * @return void