note 59004 added to function.recode-string

From: Date: Wed, 23 Nov 2005 18:41:34 +0000
Subject: note 59004 added to function.recode-string
Groups: php.notes 
Request: Send a blank email to php-notes+get-98987@lists.php.net to get a copy of this message
Here's how to convert romaji to katakana/hiragana with PHP (transliterating Japanese text). The function Romaji2Kana($s) will return with keys 'hira' and 'kata' that respectively contain the hiragana and katakana versions of the given string in UTF-8 encoding. <?php // eucjp: 2421; unicode: 3041 define('HIRATABLE', 'a A i I u U e E o O KAGAKIGIKUGUKEGEKOGOSAZASIZISUZUSEZESOZO'. 'TADATIDItuTUDUTEDETODONANINUNENOHABAPAHIBIPIHUBUPUHEBEPEHOBOPO'. 'MAMIMUMEMOyaYAyuYUyoYORARIRUREROwaWAWIWEWOn '); // eucjp: 2521; unicode: 30A1 define('KATATABLE', 'a A i I u U e E o O KAGAKIGIKUGUKEGEKOGOSAZASIZISUZUSEZESOZO'. 'TADATIDItuTUDUTEDETODONANINUNENOHABAPAHIBIPIHUBUPUHEBEPEHOBOPO'. 'MAMIMUMEMOyaYAyuYUyoYORARIRUREROwaWAWIWEWOn VUkake'); function HiraTrans($s) { #print "trans('$s')\n"; $pos = strpos(HIRATABLE, $s); if($pos===false) return 0xA1BC; // ^ return 0xA4A1 + $pos/2; } function KataTrans($s) { $pos = strpos(KATATABLE, $s); if($pos===false) return 0xA1BC; // ^ return 0xA5A1 + $pos/2; } function Romaji2Kana($s) { $s = strtoupper(str_replace( Array('shi', 'sh', 'fu', 'chi', 'ch', 'tsu', 'dz', 'l', '-', 'â', 'î', 'û', 'ê', 'ô', 'ā', 'ī', 'ū', 'ē', 'ō'), Array('si', 'sy', 'hu', 'ti', 'ty', 'tu', 'j', 'r', '^', 'a^', 'i^', 'u^', 'e^', 'o^', 'a^', 'i^', 'u^', 'e^', 'o^'), $s)); // FO -> FUxo $s = preg_replace('@F([AIOE])@e', '"HU".strtolower("\1")', $s); // VO -> VUxo $s = preg_replace('@V([AIUEO])@e', '"VU".strtolower("\1")', $s); // KYA -> KYya $s = preg_replace('@([KSTNHMRGZBPD])Y([AUO])@e', '"\1Iy".strtolower("\2")', $s); // XTU -> tu (make them actually small) $s = preg_replace('@X(TU|Y[AUO]|[AIUEO]|KA|KE)@e', 'strtolower("\1")', $s); // KKO -> tuKO $s = preg_replace('@([KSTHMRYWGZBPDV]{2,})@e', 'str_pad("",2*strlen("\1")-2,"tu").substr("\1",0,1)', $s); // N -> n (but not NO -> nO) // At this point, N' will work correctly $s = preg_replace('@N(?![AIUEO])@', 'n', $s); // Unrecognized characters off $s = eregi_replace('[^^VAIUEOKSTNHMYRWGZBPD]', '', $s); $pat = '@([AIUEOnaiueo^]|..)@e'; $rec = 'EUCJP..UTF8'; return Array('hira' => recode_string($rec,preg_replace($pat, 'pack("n", HiraTrans("\1"))', $s)), 'kata' => recode_string($rec,preg_replace($pat, 'pack("n", KataTrans("\1"))', $s))); } print_r( Romaji2Kana('konnichiha') ); ?> Note: Due to technical limitations in the manual pages, there are two errors in this code: - Some characters in the first str_replace may appear wrong in some php.net mirrors. It supposed to contain aiueo with circumflex and aiueo with macron. - The strings in the defines should be constant, not appendage expressions. (Line length limitation) -Joel Yliluoma ---- Manual Page -- http://www.php.net/manual/en/function.recode-string.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+59004 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59004&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59004&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59004&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59004&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59004&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59004&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59004&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+59004&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#98987) next »