Skip to content

Commit

Permalink
Merge pull request #90 from kolydart/multibyte_split_chunk
Browse files Browse the repository at this point in the history
multibyte split chunk
  • Loading branch information
jeroendesloovere authored Dec 18, 2017
2 parents 8be1cba + 20ae1cf commit ad2bb7d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/VCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,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;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/VCardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,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);
}
}

0 comments on commit ad2bb7d

Please sign in to comment.