home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / xampp-win32-1.6.5-installer.exe / phpMyAdmin / libraries / kanji-encoding.lib.php < prev    next >
Encoding:
PHP Script  |  2007-12-20  |  3.9 KB  |  149 lines

  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)
  6.  *
  7.  * PHP4 configure requirements:
  8.  *     --enable-mbstring --enable-mbstr-enc-trans --enable-mbregex
  9.  *
  10.  * 2002/2/22 - by Yukihiro Kawada <kawada@den.fujifilm.co.jp>
  11.  *
  12.  * @version $Id: kanji-encoding.lib.php 10142 2007-03-20 10:32:13Z cybot_tm $
  13.  */
  14.  
  15. /**
  16.  * Gets the php internal encoding codes and sets the available encoding
  17.  * codes list
  18.  * 2002/1/4 by Y.Kawada
  19.  *
  20.  * @global  string   the current encoding code
  21.  * @global  string   the available encoding codes list
  22.  *
  23.  * @return  boolean  always true
  24.  */
  25. function PMA_internal_enc_check() {
  26.     global $internal_enc, $enc_list;
  27.  
  28.     $internal_enc = mb_internal_encoding();
  29.     if ($internal_enc == 'EUC-JP') {
  30.         $enc_list = 'ASCII,EUC-JP,SJIS,JIS';
  31.     } else {
  32.         $enc_list = 'ASCII,SJIS,EUC-JP,JIS';
  33.     }
  34.  
  35.     return TRUE;
  36. } // end of the 'PMA_internal_enc_check' function
  37.  
  38.  
  39. /**
  40.  * Reverses SJIS & EUC-JP position in the encoding codes list
  41.  * 2002/1/4 by Y.Kawada
  42.  *
  43.  * @global  string   the available encoding codes list
  44.  *
  45.  * @return  boolean  always true
  46.  */
  47. function PMA_change_enc_order() {
  48.     global $enc_list;
  49.  
  50.     $p            = explode(',', $enc_list);
  51.     if ($p[1] == 'EUC-JP') {
  52.         $enc_list = 'ASCII,SJIS,EUC-JP,JIS';
  53.     } else {
  54.         $enc_list = 'ASCII,EUC-JP,SJIS,JIS';
  55.     }
  56.  
  57.     return TRUE;
  58. } // end of the 'PMA_change_enc_order' function
  59.  
  60.  
  61. /**
  62.  * Kanji string encoding convert
  63.  * 2002/1/4 by Y.Kawada
  64.  *
  65.  * @param   string   the string to convert
  66.  * @param   string   the destinasion encoding code
  67.  * @param   string   set 'kana' convert to JIS-X208-kana
  68.  *
  69.  * @global  string   the available encoding codes list
  70.  *
  71.  * @return  string   the converted string
  72.  */
  73. function PMA_kanji_str_conv($str, $enc, $kana) {
  74.     global $enc_list;
  75.  
  76.     if ($enc == '' && $kana == '') {
  77.         return $str;
  78.     }
  79.     $nw       = mb_detect_encoding($str, $enc_list);
  80.  
  81.     if ($kana == 'kana') {
  82.         $dist = mb_convert_kana($str, 'KV', $nw);
  83.         $str  = $dist;
  84.     }
  85.     if ($nw != $enc && $enc != '') {
  86.         $dist = mb_convert_encoding($str, $enc, $nw);
  87.     } else {
  88.         $dist = $str;
  89.     }
  90.     return $dist;
  91. } // end of the 'PMA_kanji_str_conv' function
  92.  
  93.  
  94. /**
  95.  * Kanji file encoding convert
  96.  * 2002/1/4 by Y.Kawada
  97.  *
  98.  * @param   string   the name of the file to convert
  99.  * @param   string   the destinasion encoding code
  100.  * @param   string   set 'kana' convert to JIS-X208-kana
  101.  *
  102.  * @return  string   the name of the converted file
  103.  */
  104. function PMA_kanji_file_conv($file, $enc, $kana) {
  105.     if ($enc == '' && $kana == '') {
  106.         return $file;
  107.     }
  108.  
  109.     $tmpfname = tempnam('', $enc);
  110.     $fpd      = fopen($tmpfname, 'wb');
  111.     $fps      = fopen($file, 'r');
  112.     PMA_change_enc_order();
  113.     while (!feof($fps)) {
  114.         $line = fgets($fps, 4096);
  115.         $dist = PMA_kanji_str_conv($line, $enc, $kana);
  116.         fputs($fpd, $dist);
  117.     } // end while
  118.     PMA_change_enc_order();
  119.     fclose($fps);
  120.     fclose($fpd);
  121.     unlink($file);
  122.  
  123.     return $tmpfname;
  124. } // end of the 'PMA_kanji_file_conv' function
  125.  
  126.  
  127. /**
  128.  * Defines radio form fields to switch between encoding modes
  129.  * 2002/1/4 by Y.Kawada
  130.  *
  131.  * @param   string   spaces character to prepend the output with
  132.  *
  133.  * @return  string   xhtml code for the radio controls
  134.  */
  135. function PMA_set_enc_form($spaces) {
  136.     return "\n"
  137.            . $spaces . '<input type="radio" name="knjenc" value="" checked="checked" />non' . "\n"
  138.            . $spaces . '<input type="radio" name="knjenc" value="EUC-JP" />EUC' . "\n"
  139.            . $spaces . '<input type="radio" name="knjenc" value="SJIS" />SJIS' . "\n"
  140.            . $spaces . ' ' . $GLOBALS['strEncto'] . '<br />' . "\n"
  141.            . $spaces . '<input type="checkbox" name="xkana" value="kana" />' . "\n"
  142.            . $spaces . ' ' . $GLOBALS['strXkana'] . '<br />' . "\n";
  143. } // end of the 'PMA_set_enc_form' function
  144.  
  145.  
  146. PMA_internal_enc_check();
  147.  
  148. ?>
  149.