Feature Request #3187076
[phpmyadmin-regexreplace.git] / libraries / kanji-encoding.lib.php
blob67c8b1cddf514dd7ec4c562d527ccace995efdc4
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions for kanji-encoding convert (available only with japanese
5 * language)
7 * PHP4 configure requirements:
8 * --enable-mbstring --enable-mbstr-enc-trans --enable-mbregex
10 * 2002/2/22 - by Yukihiro Kawada <kawada@den.fujifilm.co.jp>
12 * @package phpMyAdmin
14 if (! defined('PHPMYADMIN')) {
15 exit;
18 /**
19 * Gets the php internal encoding codes and sets the available encoding
20 * codes list
21 * 2002/1/4 by Y.Kawada
23 * @global string the current encoding code
24 * @global string the available encoding codes list
26 * @return boolean always true
28 function PMA_internal_enc_check() {
29 global $internal_enc, $enc_list;
31 $internal_enc = mb_internal_encoding();
32 if ($internal_enc == 'EUC-JP') {
33 $enc_list = 'ASCII,EUC-JP,SJIS,JIS';
34 } else {
35 $enc_list = 'ASCII,SJIS,EUC-JP,JIS';
38 return TRUE;
39 } // end of the 'PMA_internal_enc_check' function
42 /**
43 * Reverses SJIS & EUC-JP position in the encoding codes list
44 * 2002/1/4 by Y.Kawada
46 * @global string the available encoding codes list
48 * @return boolean always true
50 function PMA_change_enc_order() {
51 global $enc_list;
53 $p = explode(',', $enc_list);
54 if ($p[1] == 'EUC-JP') {
55 $enc_list = 'ASCII,SJIS,EUC-JP,JIS';
56 } else {
57 $enc_list = 'ASCII,EUC-JP,SJIS,JIS';
60 return TRUE;
61 } // end of the 'PMA_change_enc_order' function
64 /**
65 * Kanji string encoding convert
66 * 2002/1/4 by Y.Kawada
68 * @param string the string to convert
69 * @param string the destination encoding code
70 * @param string set 'kana' convert to JIS-X208-kana
72 * @global string the available encoding codes list
74 * @return string the converted string
76 function PMA_kanji_str_conv($str, $enc, $kana) {
77 global $enc_list;
79 if ($enc == '' && $kana == '') {
80 return $str;
82 $nw = mb_detect_encoding($str, $enc_list);
84 if ($kana == 'kana') {
85 $dist = mb_convert_kana($str, 'KV', $nw);
86 $str = $dist;
88 if ($nw != $enc && $enc != '') {
89 $dist = mb_convert_encoding($str, $enc, $nw);
90 } else {
91 $dist = $str;
93 return $dist;
94 } // end of the 'PMA_kanji_str_conv' function
97 /**
98 * Kanji file encoding convert
99 * 2002/1/4 by Y.Kawada
101 * @param string the name of the file to convert
102 * @param string the destination encoding code
103 * @param string set 'kana' convert to JIS-X208-kana
105 * @return string the name of the converted file
107 function PMA_kanji_file_conv($file, $enc, $kana) {
108 if ($enc == '' && $kana == '') {
109 return $file;
112 $tmpfname = tempnam('', $enc);
113 $fpd = fopen($tmpfname, 'wb');
114 $fps = fopen($file, 'r');
115 PMA_change_enc_order();
116 while (!feof($fps)) {
117 $line = fgets($fps, 4096);
118 $dist = PMA_kanji_str_conv($line, $enc, $kana);
119 fputs($fpd, $dist);
120 } // end while
121 PMA_change_enc_order();
122 fclose($fps);
123 fclose($fpd);
124 unlink($file);
126 return $tmpfname;
127 } // end of the 'PMA_kanji_file_conv' function
131 * Defines radio form fields to switch between encoding modes
132 * 2002/1/4 by Y.Kawada
134 * @param string spaces character to prepend the output with
136 * @return string xhtml code for the radio controls
138 function PMA_set_enc_form($spaces) {
139 return "\n"
140 /* l10n: This is currently used only in Japanese locales */
141 . $spaces . '<ul>' . "\n" . '<li>'
142 . $spaces . '<input type="radio" name="knjenc" value="" checked="checked" id="kj-none" /><label for="kj-none">' . _pgettext('None encoding conversion', 'None') . "</label>\n"
143 . $spaces . '<input type="radio" name="knjenc" value="EUC-JP" id="kj-euc" /><label for="kj-euc">EUC</label>' . "\n"
144 . $spaces . '<input type="radio" name="knjenc" value="SJIS" id="kj-sjis" /><label for="kj-sjis">SJIS</label>' . "\n"
145 . $spaces . '</li>' . "\n" . '<li>'
146 . $spaces . '<input type="checkbox" name="xkana" value="kana" id="kj-kana" />' . "\n"
147 /* l10n: This is currently used only in Japanese locales */
148 . $spaces . '<label for="kj-kana">' . __('Convert to Kana') . '</label><br />' . "\n"
149 . $spaces . '</li>' . "\n" . '</ul>'
151 } // end of the 'PMA_set_enc_form' function
154 PMA_internal_enc_check();