From 20ae1cfcf07517baadbaac9f3bbf2a06416227a7 Mon Sep 17 00:00:00 2001 From: kolydart Date: Sun, 16 Apr 2017 10:19:26 +0300 Subject: [PATCH] New protected method VCard::chunk_split_unicode() to handle utf8 strings during VCard::fold(); with tests --- src/VCard.php | 22 +++++++++++++++++++++- tests/VCardTest.php | 19 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/VCard.php b/src/VCard.php index ffacd9f..9fdcf57 100644 --- a/src/VCard.php +++ b/src/VCard.php @@ -546,7 +546,27 @@ protected function fold($text) } // split, wrap and trim trailing separator - return substr(chunk_split($text, 73, "\r\n "), 0, -3); + return substr($this->chunk_split_unicode($text, 73, "\r\n "), 0, -3); + } + + /** + * multibyte word chunk split + * @link http://php.net/manual/en/function.chunk-split.php#107711 + * + * @param string $body The string to be chunked. + * @param integer $chunklen The chunk length. + * @param string $end The line ending sequence. + * @return string Chunked string + */ + protected function chunk_split_unicode($body, $chunklen = 76, $end = "\r\n") + { + $array = array_chunk( + preg_split("//u", $body, -1, PREG_SPLIT_NO_EMPTY), $chunklen); + $body = ""; + foreach ($array as $item) { + $body .= join("", $item) . $end; + } + return $body; } /** diff --git a/tests/VCardTest.php b/tests/VCardTest.php index a7415f8..da008c0 100644 --- a/tests/VCardTest.php +++ b/tests/VCardTest.php @@ -368,4 +368,23 @@ public function testSpecialFirstNameAndLastName2() $this->assertEquals('garcon-jeroen', $this->vcard->getFilename()); } + + public function testChunkSplitUnicode() + { + $class_handler = new \ReflectionClass('JeroenDesloovere\VCard\VCard'); + $method_handler = $class_handler->getMethod('chunk_split_unicode'); + $method_handler->setAccessible(true); + + $ascii_input="Lorem ipsum dolor sit amet,"; + $ascii_output = $method_handler->invokeArgs(new VCard(), [$ascii_input,10,'|']); + $unicode_input='Τη γλώσσα μου έδωσαν ελληνική το σπίτι φτωχικό στις αμμουδιές του Ομήρου.'; + $unicode_output = $method_handler->invokeArgs(new VCard(), [$unicode_input,10,'|']); + + $this->assertEquals( + "Lorem ipsu|m dolor si|t amet,|", + $ascii_output); + $this->assertEquals( + "Τη γλώσσα |μου έδωσαν| ελληνική |το σπίτι φ|τωχικό στι|ς αμμουδιέ|ς του Ομήρ|ου.|", + $unicode_output); + } }