Skip to content

@timw4mail /index.php
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
PHP Kana transliterator
<?php
include("kana.php");
if(isset($_GET['in']) && isset($_GET['action']))
{
$in = $_GET['in'];
if($_GET['action'] == "to_hira")
{
$out = Kana::to_hiragana($in);
}
else if($_GET['action'] == "to_kata")
{
$out = Kana::to_katakana($in);
}
else if($_GET['action'] == "from_kana")
{
$out = Kana::from_kana($in);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Romaji/Kana transliteration</title>
</head>
<body>
<form action="" method="get">
<dl>
<dt><label for="in">Romaji</label></dt>
<dd><textarea rows="10" cols="80" name="in" id="in"></textarea></dd>
</dl>
<dl>
<dt><label for="action">Action</label></dt>
<dd>
<select name="action" id="action">
<option value="to_hira">To Hiragana</option>
<option value="to_kata">To Katakana</option>
<option value="from_kana">From Kana</option>
</select>
</dd>
</dl>
<button type="submit">Transliterate</button>
</form>
<p id="out">
<?= (isset($out)) ? $out : "" ?>
</p>
</body>
</html>
<?php class Kana {
function to_hiragana($text)
{
//Define kana arrays
$hiragana = array(
//vowels
'a' => '',
'i' => '',
'u' => '',
'e' => '',
'o' => '',
//k
'ka' => '',
'ki' => '',
'ku' => '',
'ke' => '',
'ko' => '',
'ca' => '',
'ci' => '',
'cu' => '',
'ce' => '',
'co' => '',
//g
'ga' => '',
'gi' => '',
'gu' => '',
'ge' => '',
'go' => '',
//s
'sa' => '',
'shi' => '',
'si' => '',
'su' => '',
'se' => '',
'so' => '',
//j
'ja' => 'じゃ',
'ji' => '',
'ju' => 'じゅ',
'je' => 'じぇ',
'jo' => 'じょ',
//z
'za' => '',
'zi' => '',
'zu' => '',
'ze' => '',
'zo' => '',
//t
'ta' => '',
'chi' => '',
'ti' => '',
'tsu' => '',
'tu' => '',
'te' => '',
'to' => '',
//d
'da' => '',
'di' => '',
'du' => '',
'de' => '',
'do' => '',
//n
'na' => '',
'ni' => '',
'nu' => '',
'ne' => '',
'no' => '',
//h
'ha' => '',
'hi' => '',
'hu' => '',
'fu' => '',
'he' => '',
'ho' => '',
//b
'ba' => '',
'bi' => '',
'bu' => '',
'be' => '',
'bo' => '',
//p
'pa' => '',
'pi' => '',
'pu' => '',
'pe' => '',
'po' => '',
//m
'ma' => '',
'mi' => '',
'mu' => '',
'me' => '',
'mo' => '',
//y
'ya' => '',
'yu' => '',
'yo' => '',
//r
'ra' => '',
'ri' => '',
'ru' => '',
're' => '',
'ro' => '',
//w
'wa' => '',
'wo' => '',
'n' => '',
//compound
'kya' => 'きゃ',
'kyu' => 'きゅ',
'kyo' => 'きょ',
'gya' => 'ぎゃ',
'gyu' => 'ぎゅ',
'gyo' => 'ぎょ',
'sha' => 'しゃ',
'shu' => 'しゅ',
'sho' => 'しょ',
'cha' => 'ちゃ',
'chu' => 'ちゅ',
'cho' => 'ちょ',
'nya' => 'にゃ',
'nyu' => 'にゅ',
'nyo' => 'にょ',
'hya' => 'ひゃ',
'hyu' => 'ひゅ',
'hyo' => 'ひょ',
'bya' => 'びゃ',
'byu' => 'びゅ',
'byo' => 'びょ',
'pya' => 'ぴゃ',
'pyu' => 'ぴゅ',
'pyo' => 'ぴょ',
'mya' => 'みゃ',
'myu' => 'みゅ',
'myo' => 'みょ',
'rya' => 'りゃ',
'ryu' => 'りゅ',
'ryo' => 'りょ',
);
$hira_repeated = array(
'kk' => 'っk',
'ss' => 'っs',
'tt' => 'っt',
'nn' => 'っn',
'hh' => 'っh',
'mm' => 'っm',
'yy' => 'っy',
'rr' => 'っr',
'ww' => 'っw',
'gg' => 'っg',
'zz' => 'っz',
'jj' => 'っj',
'dd' => 'っd',
'bb' => 'っb',
'pp' => 'っp',
'oo' => 'oう',
'oh' => 'oう',
);
return self::_to_kana($text, $hira_repeated, $hiragana);
}
// --------------------------------------------------------------------------
function to_katakana($text)
{
//Define kana arrays
$katakana = array(
//vowels
'a' => '',
'i' => '',
'u' => '',
'e' => '',
'o' => '',
//k
'ca' => '',
'ci' => '',
'cu' => '',
'ce' => '',
'co' => '',
'ka' => '',
'ki' => '',
'ku' => '',
'ke' => '',
'ko' => '',
//g
'ga' => '',
'gi' => '',
'gu' => '',
'ge' => '',
'go' => '',
//s
'sa' => '',
'shi' => '',
'si' => '',
'su' => '',
'se' => '',
'so' => '',
//j
'ja' => 'ジャ',
'ji' => '',
'ju' => 'ジュ',
'je' => 'ジェ',
'jo' => 'ジョ',
//z
'za' => '',
'zi' => '',
'zu' => '',
'ze' => '',
'zo' => '',
//t
'ta' => '',
'chi' => '',
'ti' => '',
'tu' => '',
'tsu' => '',
'te' => '',
'to' => '',
//d
'da' => '',
'di' => '',
'du' => '',
'de' => '',
'do' => '',
//n
'na' => '',
'ni' => '',
'nu' => '',
'ne' => '',
'no' => '',
//h
'ha' => '',
'hi' => '',
'hu' => '',
'fu' => '',
'he' => '',
'ho' => '',
//b
'ba' => '',
'bi' => '',
'bu' => '',
'be' => '',
'bo' => '',
//p
'pa' => '',
'pi' => '',
'pu' => '',
'pe' => '',
'po' => '',
//m
'ma' => '',
'mi' => '',
'mu' => '',
'me' => '',
'mo' => '',
//y
'ya' => '',
'yu' => '',
'yo' => '',
//r
'ra' => '',
'ri' => '',
'ru' => '',
're' => '',
'ro' => '',
//w
'wa' => '',
'wo' => '',
'n' => '',
//compound
'kya' => 'キャ',
'kyu' => 'キュ',
'kyo' => 'キョ',
'gya' => 'ギャ',
'gyu' => 'ギュ',
'gyo' => 'ギョ',
'sha' => 'シャ',
'shu' => 'シュ',
'sho' => 'ショ',
'cha' => 'チャ',
'chu' => 'チュ',
'cho' => 'チョ',
'nya' => 'ニャ',
'nyu' => 'ニュ',
'nyo' => 'ニョ',
'hya' => 'ヒャ',
'hyu' => 'ヒュ',
'hyo' => 'ヒョ',
'bya' => 'ビャ',
'byu' => 'ビュ',
'byo' => 'ビョ',
'pya' => 'ピャ',
'pyu' => 'ピュ',
'pyo' => 'ピョ',
'mya' => 'ミャ',
'myu' => 'ミュ',
'myo' => 'ミョ',
'rya' => 'リャ',
'ryu' => 'リュ',
'ryo' => 'リョ',
);
$kata_repeated = array(
'kk' => 'ッk',
'ss' => 'ッs',
'tt' => 'ッt',
'nn' => 'ッn',
'hh' => 'ッh',
'mm' => 'ッm',
'yy' => 'ッy',
'rr' => 'ッr',
'ww' => 'ッw',
'gg' => 'ッg',
'zz' => 'ッz',
'jj' => 'ッj',
'dd' => 'ッd',
'bb' => 'ッb',
'pp' => 'ッp',
'aa' => 'aー',
'ii' => 'iー',
'uu' => 'uー',
'ee' => 'eー',
'oo' => 'oー',
'oh' => 'oー',
);
return self::_to_kana($text, $kata_repeated, $katakana);
}
// --------------------------------------------------------------------------
function from_kana($text)
{
$hiragana = array(
//vowels
'' => 'a',
'' => 'i',
'' => 'u',
'' => 'e',
'' => 'o',
//k
'' => 'ka',
'' => 'ki',
'' => 'ku',
'' => 'ke',
'' => 'ko',
//g
'' => 'ga',
'' => 'gi',
'' => 'gu',
'' => 'ge',
'' => 'go',
//s
'' => 'sa',
'' => 'shi',
'' => 'su',
'' => 'se',
'' => 'so',
//j
'じゃ' => 'ja',
'' => 'ji',
'' => 'ji',
'じゅ' => 'ju',
'じぇ' => 'je',
'じょ' => 'jo',
//z
'' => 'za',
'' => 'zu',
'' => 'ze',
'' => 'zo',
//t
'' => 'ta',
'' => 'chi',
'' => 'tsu',
'' => 'te',
'' => 'to',
//d
'' => 'da',
'' => 'du',
'' => 'de',
'' => 'do',
//n
'' => 'na',
'' => 'ni',
'' => 'nu',
'' => 'ne',
'' => 'no',
//h
'' => 'ha',
'' => 'hi',
'' => 'fu',
'' => 'he',
'' => 'ho',
//b
'' => 'ba',
'' => 'bi',
'' => 'bu',
'' => 'be',
'' => 'bo',
//p
'' => 'pa',
'' => 'pi',
'' => 'pu',
'' => 'pe',
'' => 'po',
//m
'' => 'ma',
'' => 'mi',
'' => 'mu',
'' => 'me',
'' => 'mo',
//y
'' => 'ya',
'' => 'yu',
'' => 'yo',
//r
'' => 'ra',
'' => 'ri',
'' => 'ru',
'' => 're',
'' => 'ro',
//w
'' => 'wa',
'' => 'wo',
'' => 'n',
//compound
'きゃ' => 'kya',
'きゅ' => 'kyu',
'きょ' => 'kyo',
'ぎゃ' => 'gya',
'ぎゅ' => 'gyu',
'ぎょ' => 'gyo',
'しゃ' => 'sha',
'しゅ' => 'shu',
'しょ' => 'sho',
'ちゃ' => 'cha',
'ちゅ' => 'chu',
'ちょ' => 'cho',
'にゃ' => 'nya',
'にゅ' => 'nyu',
'にょ' => 'nyo',
'ひゃ' => 'hya',
'ひゅ' => 'hyu',
'ひょ' => 'hyo',
'びゃ' => 'bya',
'びゅ' => 'byu',
'びょ' => 'byo',
'ぴゃ' => 'pya',
'ぴゅ' => 'pyu',
'ぴょ' => 'pyo',
'みゃ' => 'mya',
'みゅ' => 'myu',
'みょ' => 'myo',
'りゃ' => 'rya',
'りゅ' => 'ryu',
'りょ' => 'ryo',
);
$hira_repeated = array(
'っk' => 'kk',
'っs' => 'ss',
'っt' => 'tt',
'っn' => 'nn',
'っh' => 'hh',
'っm' => 'mm',
'っy' => 'yy',
'っr' => 'rr',
'っw' => 'ww',
'っg' => 'gg',
'っz' => 'zz',
'っj' => 'jj',
'っd' => 'dd',
'っb' => 'bb',
'っp' => 'pp',
);
$katakana = array(
//vowels
'' => 'a',
'' => 'i',
'' => 'u',
'' => 'e',
'' => 'o',
//k
'' => 'ka',
'' => 'ki',
'' => 'ku',
'' => 'ke',
'' => 'ko',
//g
'' => 'ga',
'' => 'gi',
'' => 'gu',
'' => 'ge',
'' => 'go',
//s
'' => 'sa',
'' => 'shi',
'' => 'su',
'' => 'se',
'' => 'so',
//j
'ジャ' => 'ja',
'' => 'ji',
'' => 'ji',
'ジュ' => 'ju',
'ジェ' => 'je',
'ジョ' => 'jo',
//z
'' => 'za',
'' => 'zu',
'' => 'ze',
'' => 'zo',
//t
'' => 'ta',
'' => 'chi',
'' => 'tsu',
'' => 'te',
'' => 'to',
//d
'' => 'da',
'' => 'du',
'' => 'de',
'' => 'do',
//n
'' => 'na',
'' => 'ni',
'' => 'nu',
'' => 'ne',
'' => 'no',
//h
'' => 'ha',
'' => 'hi',
'' => 'fu',
'' => 'he',
'' => 'ho',
//b
'' => 'ba',
'' => 'bi',
'' => 'bu',
'' => 'be',
'' => 'bo',
//p
'' => 'pa',
'' => 'pi',
'' => 'pu',
'' => 'pe',
'' => 'po',
//m
'' => 'ma',
'' => 'mi',
'' => 'mu',
'' => 'me',
'' => 'mo',
//y
'' => 'ya',
'' => 'yu',
'' => 'yo',
//r
'' => 'ra',
'' => 'ri',
'' => 'ru',
'' => 're',
'' => 'ro',
//w
'' => 'wa',
'' => 'wo',
'' => 'n',
//compound
'キャ' => 'kya',
'キュ' => 'kyu',
'キョ' => 'kyo',
'ギャ' => 'gya',
'ギュ' => 'gyu',
'ギョ' => 'gyo',
'シャ' => 'sha',
'シュ' => 'shu',
'ショ' => 'sho',
'チャ' => 'cha',
'チュ' => 'chu',
'チョ' => 'cho',
'ニャ' => 'nya',
'ニュ' => 'nyu',
'ニョ' => 'nyo',
'ヒャ' => 'hya',
'ヒュ' => 'hyu',
'ヒョ' => 'hyo',
'ビャ' => 'bya',
'ビュ' => 'byu',
'ビョ' => 'byo',
'ピャ' => 'pya',
'ピュ' => 'pyu',
'ピョ' => 'pyo',
'ミャ' => 'mya',
'ミュ' => 'myu',
'ミョ' => 'myo',
'リャ' => 'rya',
'リュ' => 'ryu',
'リョ' => 'ryo',
);
$kata_repeated = array(
'ッk' => 'kk',
'ッs' => 'ss',
'ッt' => 'tt',
'ッn' => 'nn',
'ッh' => 'hh',
'ッm' => 'mm',
'ッy' => 'yy',
'ッr' => 'rr',
'ッw' => 'ww',
'ッg' => 'gg',
'ッz' => 'zz',
'ッj' => 'jj',
'ッd' => 'dd',
'ッb' => 'bb',
'ッp' => 'pp',
'aー' => 'aa',
'iー' => 'ii',
'uー' => 'uu',
'eー' => 'ee',
'oー' => 'oo',
'oー' => 'oh',
);
$kata_only = array(
'ヴァ' => 'va',
'ファ' => 'fa',
'ウィ' => 'wi',
'ヴィ' => 'vi',
'フィ' => 'fi',
'' => 'vu',
'イェ' => 'ye',
'ウェ' => 'we',
'ヴェ' => 've',
'フェ' => 'fe',
'ヴォ' => 'vo',
'フォ' => 'fo',
'クァ' => 'kwa',
'グァ' => 'gwa',
'ツァ' => 'tsa',
'クィ' => 'kwi',
'グィ' => 'gwi',
'ツィ' => 'tsi',
'ティ' => 'thi',
'クェ' => 'kwe',
'グェ' => 'gwe',
'シェ' => 'she',
'チェ' => 'che',
'ツェ' => 'tse',
'クォ' => 'kwo',
'グォ' => 'gwo',
'ツォ' => 'tso',
'ヴャ' => 'vya',
'テャ' => 'tha',
'ヂャ' => 'dya',
'フャ' => 'fya',
'ヴュ' => 'vyu',
'テュ' => 'thu',
'ヂュ' => 'dyu',
'フュ' => 'fyu',
'ヴョ' => 'vyo',
'テョ' => 'tho',
'ヂョ' => 'dyo',
'フョ' => 'fyo',
);
//Merge arrays
$pairs = array_merge($hiragana, $katakana, $kata_only);
//Replace normal characters
$text = strtr($text, $pairs);
//Replace doubled letters
$text = strtr($text, array_merge($hira_repeated, $kata_repeated));
return $text;
}
// --------------------------------------------------------------------------
private function _to_kana($text, $repeated, $pairs)
{
$kata_only = array(
'va' => 'ヴァ',
'fa' => 'ファ',
'wi' => 'ウィ',
'vi' => 'ヴィ',
'fi' => 'フィ',
'vu' => '',
'ye' => 'イェ',
'we' => 'ウェ',
've' => 'ヴェ',
'fe' => 'フェ',
'vo' => 'ヴォ',
'fo' => 'フォ',
'kwa' => 'クァ',
'gwa' => 'グァ',
'tsa' => 'ツァ',
'kwi' => 'クィ',
'gwi' => 'グィ',
'tsi' => 'ツィ',
'thi' => 'ティ',
'kwe' => 'クェ',
'gwe' => 'グェ',
'she' => 'シェ',
'che' => 'チェ',
'tse' => 'ツェ',
'kwo' => 'クォ',
'gwo' => 'グォ',
'tso' => 'ツォ',
'vya' => 'ヴャ',
'tha' => 'テャ',
'dya' => 'ヂャ',
'fya' => 'フャ',
'vyu' => 'ヴュ',
'thu' => 'テュ',
'dyu' => 'ヂュ',
'fyu' => 'フュ',
'vyo' => 'ヴョ',
'tho' => 'テョ',
'dyo' => 'ヂョ',
'fyo' => 'フョ',
);
//Replace doubled letters
$out = str_replace(array_keys($repeated), array_values($repeated), $text);
//Add in katakana only combinations
$pairs = array_merge($pairs, $kata_only);
//Replace everything else
$out = strtr($out, $pairs);
return $out;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.