home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / phpMyAdmin / libraries / kanji-encoding.lib.php < prev    next >
Encoding:
PHP Script  |  2008-06-23  |  4.0 KB  |  152 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 11326 2008-06-17 21:32:48Z lem9 $
  13.  */
  14. if (! defined('PHPMYADMIN')) {
  15.     exit;
  16. }
  17.  
  18. /**
  19.  * Gets the php internal encoding codes and sets the available encoding
  20.  * codes list
  21.  * 2002/1/4 by Y.Kawada
  22.  *
  23.  * @global  string   the current encoding code
  24.  * @global  string   the available encoding codes list
  25.  *
  26.  * @return  boolean  always true
  27.  */
  28. function PMA_internal_enc_check() {
  29.     global $internal_enc, $enc_list;
  30.  
  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';
  36.     }
  37.  
  38.     return TRUE;
  39. } // end of the 'PMA_internal_enc_check' function
  40.  
  41.  
  42. /**
  43.  * Reverses SJIS & EUC-JP position in the encoding codes list
  44.  * 2002/1/4 by Y.Kawada
  45.  *
  46.  * @global  string   the available encoding codes list
  47.  *
  48.  * @return  boolean  always true
  49.  */
  50. function PMA_change_enc_order() {
  51.     global $enc_list;
  52.  
  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';
  58.     }
  59.  
  60.     return TRUE;
  61. } // end of the 'PMA_change_enc_order' function
  62.  
  63.  
  64. /**
  65.  * Kanji string encoding convert
  66.  * 2002/1/4 by Y.Kawada
  67.  *
  68.  * @param   string   the string to convert
  69.  * @param   string   the destinasion encoding code
  70.  * @param   string   set 'kana' convert to JIS-X208-kana
  71.  *
  72.  * @global  string   the available encoding codes list
  73.  *
  74.  * @return  string   the converted string
  75.  */
  76. function PMA_kanji_str_conv($str, $enc, $kana) {
  77.     global $enc_list;
  78.  
  79.     if ($enc == '' && $kana == '') {
  80.         return $str;
  81.     }
  82.     $nw       = mb_detect_encoding($str, $enc_list);
  83.  
  84.     if ($kana == 'kana') {
  85.         $dist = mb_convert_kana($str, 'KV', $nw);
  86.         $str  = $dist;
  87.     }
  88.     if ($nw != $enc && $enc != '') {
  89.         $dist = mb_convert_encoding($str, $enc, $nw);
  90.     } else {
  91.         $dist = $str;
  92.     }
  93.     return $dist;
  94. } // end of the 'PMA_kanji_str_conv' function
  95.  
  96.  
  97. /**
  98.  * Kanji file encoding convert
  99.  * 2002/1/4 by Y.Kawada
  100.  *
  101.  * @param   string   the name of the file to convert
  102.  * @param   string   the destinasion encoding code
  103.  * @param   string   set 'kana' convert to JIS-X208-kana
  104.  *
  105.  * @return  string   the name of the converted file
  106.  */
  107. function PMA_kanji_file_conv($file, $enc, $kana) {
  108.     if ($enc == '' && $kana == '') {
  109.         return $file;
  110.     }
  111.  
  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);
  125.  
  126.     return $tmpfname;
  127. } // end of the 'PMA_kanji_file_conv' function
  128.  
  129.  
  130. /**
  131.  * Defines radio form fields to switch between encoding modes
  132.  * 2002/1/4 by Y.Kawada
  133.  *
  134.  * @param   string   spaces character to prepend the output with
  135.  *
  136.  * @return  string   xhtml code for the radio controls
  137.  */
  138. function PMA_set_enc_form($spaces) {
  139.     return "\n"
  140.            . $spaces . '<input type="radio" name="knjenc" value="" checked="checked" />non' . "\n"
  141.            . $spaces . '<input type="radio" name="knjenc" value="EUC-JP" />EUC' . "\n"
  142.            . $spaces . '<input type="radio" name="knjenc" value="SJIS" />SJIS' . "\n"
  143.            . $spaces . ' ' . $GLOBALS['strEncto'] . '<br />' . "\n"
  144.            . $spaces . '<input type="checkbox" name="xkana" value="kana" />' . "\n"
  145.            . $spaces . ' ' . $GLOBALS['strXkana'] . '<br />' . "\n";
  146. } // end of the 'PMA_set_enc_form' function
  147.  
  148.  
  149. PMA_internal_enc_check();
  150.  
  151. ?>
  152.