Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Commit

Permalink
Fix encoding error
Browse files Browse the repository at this point in the history
  • Loading branch information
bepsvpt committed Jan 9, 2016
1 parent d00fedc commit 9eddd10
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions src/Faker/Provider/zh_TW/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,57 @@ protected static function appendEnd($text)
if ($mbAvailable) {
$last = mb_substr($text, mb_strlen($text, static::$encoding) - 1, null, static::$encoding);
} else {
$chars = static::explode($text);
$last = end($chars);
$chars = static::utf8Encoding($text);
$last = $chars[count($chars) - 1];
}

// if the last char is a not-valid-end punctuation, remove it
if (in_array($last, static::$notEndPunct)) {
if ($mbAvailable) {
$text = mb_substr($text, 0, mb_strlen($text, static::$encoding) - 1, static::$encoding);
} else {
$text = substr($text, 0, strlen($text) - 1);
array_pop($chars);
$text = implode('', $chars);
}
}

// if the last char is not a valid punctuation, append a default one.
return in_array($last, static::$endPunct) ? $text : $text . '';
}

/**
* Convert original string to utf-8 encoding.
*
* @param string $text
* @return array
*/
protected static function utf8Encoding($text)
{
$encoding = array();

$chars = str_split($text);

for ($i = 0; $i < count($chars); ++$i) {
$temp = $chars[$i];

$ord = ord($chars[$i]);

switch (true) {
case $ord > 251:
$temp .= $chars[++$i];
case $ord > 247:
$temp .= $chars[++$i];
case $ord > 239:
$temp .= $chars[++$i];
case $ord > 223:
$temp .= $chars[++$i];
case $ord > 191:
$temp .= $chars[++$i];
}

$encoding[] = $temp;
}

return $encoding;
}
}

0 comments on commit 9eddd10

Please sign in to comment.