Skip to content

Commit

Permalink
bugfix close #16 close #17
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Apr 28, 2015
1 parent 4ea0c45 commit 8754f8a
Show file tree
Hide file tree
Showing 3 changed files with 107,202 additions and 107,197 deletions.
35 changes: 10 additions & 25 deletions src/Pinyin/Pinyin.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ public static function parse($string, array $settings = array())
}

$source = $instance->string2pinyin($string);

// add accents
if ($settings['accent']) {
$pinyin = $instance->addAccents($source);
Expand All @@ -184,8 +185,8 @@ public static function parse($string, array $settings = array())

$return = array(
'src' => $string,
'pinyin' => $instance->escape($delimitedPinyin),
'letter' => $instance->getFirstLetters($source, $settings),
'pinyin' => stripslashes($delimitedPinyin),
'letter' => stripslashes($instance->getFirstLetters($source, $settings)),
);

return $return;
Expand Down Expand Up @@ -257,7 +258,7 @@ protected function getAdditionalWords()
static $additionalWords;

if (empty($additionalWords)) {
$additionalWords = include __DIR__ . '/data/additional.php';
$additionalWords = static::formatAdditionalWords(include __DIR__ . '/data/additional.php');
}

return array_merge($additionalWords, static::$appends);
Expand Down Expand Up @@ -290,7 +291,7 @@ protected static function formatDictPinyin($pinyin)
{
return preg_replace_callback('/[A-Z][a-z]{1,}:?\d{1}/', function($matches){
return strtolower($matches[0]);
}, "{$pinyin} ");
}, " {$pinyin} ");
}

/**
Expand All @@ -312,7 +313,7 @@ protected function containChinese($string)
*
* @return string
*/
protected function justChinese($string)
public function justChinese($string)
{
return preg_replace('/[^\p{Han}]/u', '', $string);
}
Expand All @@ -327,28 +328,12 @@ protected function justChinese($string)
protected function prepare($string)
{
$pattern = array(
'/([a-z])+(\d)/' => '\\1\\\2', // test4 => test\4
'/([A-z])(\d)/' => '$1\\\\\2', // test4 => test\\4
);

return preg_replace(array_keys($pattern), $pattern, $string);
}

/**
* Credits for this function go to velcrow, who shared this
* at http://stackoverflow.com/questions/1162491/alternative-to-mysql-real-escape-string-without-connecting-to-db
*
* @param string $value the string to be escaped
*
* @return string the escaped string
*/
protected function escape($value)
{
$search = array("\\", "\x00", "\n", "\r", "'", '"', "\x1a");
$replace = array("\\\\", "\\0", "\\n", "\\r", "\'", '\"', "\\Z");

return str_replace($search, $replace, $value);
}

/**
* Add delimiter.
*
Expand All @@ -369,8 +354,8 @@ protected function delimit($string, $delimiter = '')
protected function removeTone($string)
{
$replacement = array(
'/u:/' => 'u',
'/\d/' => '',
'/u:/' => 'u',
'/([a-z])[1-5]/' => '\\1',
);

return preg_replace(array_keys($replacement), $replacement, $string);
Expand All @@ -388,7 +373,7 @@ protected function addAccents($string)
{
// find words with a number behind them, and replace with callback fn.
return str_replace('u:', 'ü', preg_replace_callback(
'~([a-zA-ZüÜ]+\:?)(\d)~',
'~([a-zA-ZüÜ]+\:?)([1-5])~',
array($this, 'addAccentsCallback'),
$string));
}
Expand Down
Loading

0 comments on commit 8754f8a

Please sign in to comment.