3.3.0-rc2
[phpmyadmin/madhuracj.git] / libraries / kanji-encoding.lib.php
blob00a81c56fb1a9e8285ba24606fcf7519a0b882d8
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 * @version $Id$
13 * @package phpMyAdmin
15 if (! defined('PHPMYADMIN')) {
16 exit;
19 /**
20 * Gets the php internal encoding codes and sets the available encoding
21 * codes list
22 * 2002/1/4 by Y.Kawada
24 * @global string the current encoding code
25 * @global string the available encoding codes list
27 * @return boolean always true
29 function PMA_internal_enc_check() {
30 global $internal_enc, $enc_list;
32 $internal_enc = mb_internal_encoding();
33 if ($internal_enc == 'EUC-JP') {
34 $enc_list = 'ASCII,EUC-JP,SJIS,JIS';
35 } else {
36 $enc_list = 'ASCII,SJIS,EUC-JP,JIS';
39 return TRUE;
40 } // end of the 'PMA_internal_enc_check' function
43 /**
44 * Reverses SJIS & EUC-JP position in the encoding codes list
45 * 2002/1/4 by Y.Kawada
47 * @global string the available encoding codes list
49 * @return boolean always true
51 function PMA_change_enc_order() {
52 global $enc_list;
54 $p = explode(',', $enc_list);
55 if ($p[1] == 'EUC-JP') {
56 $enc_list = 'ASCII,SJIS,EUC-JP,JIS';
57 } else {
58 $enc_list = 'ASCII,EUC-JP,SJIS,JIS';
61 return TRUE;
62 } // end of the 'PMA_change_enc_order' function
65 /**
66 * Kanji string encoding convert
67 * 2002/1/4 by Y.Kawada
69 * @param string the string to convert
70 * @param string the destinasion encoding code
71 * @param string set 'kana' convert to JIS-X208-kana
73 * @global string the available encoding codes list
75 * @return string the converted string
77 function PMA_kanji_str_conv($str, $enc, $kana) {
78 global $enc_list;
80 if ($enc == '' && $kana == '') {
81 return $str;
83 $nw = mb_detect_encoding($str, $enc_list);
85 if ($kana == 'kana') {
86 $dist = mb_convert_kana($str, 'KV', $nw);
87 $str = $dist;
89 if ($nw != $enc && $enc != '') {
90 $dist = mb_convert_encoding($str, $enc, $nw);
91 } else {
92 $dist = $str;
94 return $dist;
95 } // end of the 'PMA_kanji_str_conv' function
98 /**
99 * Kanji file encoding convert
100 * 2002/1/4 by Y.Kawada
102 * @param string the name of the file to convert
103 * @param string the destinasion encoding code
104 * @param string set 'kana' convert to JIS-X208-kana
106 * @return string the name of the converted file
108 function PMA_kanji_file_conv($file, $enc, $kana) {
109 if ($enc == '' && $kana == '') {
110 return $file;
113 $tmpfname = tempnam('', $enc);
114 $fpd = fopen($tmpfname, 'wb');
115 $fps = fopen($file, 'r');
116 PMA_change_enc_order();
117 while (!feof($fps)) {
118 $line = fgets($fps, 4096);
119 $dist = PMA_kanji_str_conv($line, $enc, $kana);
120 fputs($fpd, $dist);
121 } // end while
122 PMA_change_enc_order();
123 fclose($fps);
124 fclose($fpd);
125 unlink($file);
127 return $tmpfname;
128 } // end of the 'PMA_kanji_file_conv' function
132 * Defines radio form fields to switch between encoding modes
133 * 2002/1/4 by Y.Kawada
135 * @param string spaces character to prepend the output with
137 * @return string xhtml code for the radio controls
139 function PMA_set_enc_form($spaces) {
140 return "\n"
141 . $spaces . '<input type="radio" name="knjenc" value="" checked="checked" />non' . "\n"
142 . $spaces . '<input type="radio" name="knjenc" value="EUC-JP" />EUC' . "\n"
143 . $spaces . '<input type="radio" name="knjenc" value="SJIS" />SJIS' . "\n"
144 . $spaces . '&nbsp;' . $GLOBALS['strEncto'] . '<br />' . "\n"
145 . $spaces . '<input type="checkbox" name="xkana" value="kana" />' . "\n"
146 . $spaces . '&nbsp;' . $GLOBALS['strXkana'] . '<br />' . "\n";
147 } // end of the 'PMA_set_enc_form' function
150 PMA_internal_enc_check();