2 // PukiWiki - Yet another WikiWikiWeb clone.
3 // $Id: mbstring.php,v 1.3 2005/04/29 09:10:11 henoheno Exp $
4 // Copywrite (C) 2003-2005 PukiWiki Developers Team
5 // License: GPL v2 or (at your option) any later version
7 // mbstring-extension alternate functions
8 // (will work with LANG == 'ja' and EUC-JP environment only)
11 * mbstring extension ¤¬¥µ¡¼¥Ð¡¼Â¦¤Ë¸ºß¤·¤Ê¤¤»þ¤ÎÂåÂشؿô
15 * 1. ¼ÂºÝ¤Ë´Á»ú¥³¡¼¥ÉÊÑ´¹¤ò¹Ô¤ï¤»¤ë¤¿¤á¤Ë¤Ï¡¢ÊÌÅÓ jcode.php
16 * (TOMOºî)¤ò¥¤¥ó¥¹¥È¡¼¥ë¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£
18 * http://www.spencernetwork.org/jcode/ ¤è¤êjcode¤òÆþ¼ê¤·¡¢
19 * °Ê²¼¤ÎÍͤËŸ³«¤·¤Æ¤¯¤À¤µ¤¤¡£
21 * -+--- mbstring.php -r--
22 * +-+- jcode_1.34/ dr-x
23 * +--- readme.txt -r--
24 * +--- jcode.phps -r--
25 * +--- jcode_wrapper.php -r--
26 * +--- code_table.ucs2jis -r--
27 * +--- code_table.jis2ucs -r--
29 * 2. EUC-JPÀìÍѤǤ¹¡£(½ÐÎϤµ¤ì¤ë¥Ç¡¼¥¿¤¬EUC-JP¤Ç¤¢¤ëɬÍפ¬¤¢¤ê¤Þ¤¹)
34 define('JCODE_DIR', './jcode_1.34/');
35 define('JCODE_FILE', JCODE_DIR . 'jcode_wrapper.php');
37 if (is_readable(JCODE_FILE)) {
38 require_once(JCODE_FILE);
41 // jcode¤¬Â¸ºß¤·¤Ê¤¤¾ì¹ç¡¢¥Þ¥ë¥Á¥Ð¥¤¥Èʸ»ú¤ä´Á»ú¥³¡¼¥É¤ò°·¤¨¤Ê¤¤
42 if (! function_exists('jcode_convert_encoding')) {
44 // die_message('Multibyte functions cannot be used. Please read "mbstring.php" for an additional installation procedure of "jcode".');
46 function jstrlen($str)
51 function jsubstr($str, $start, $length)
53 return substr($str, $start, $length);
56 function AutoDetect($str)
61 function jcode_convert_encoding($str, $to_encoding, $from_encoding)
67 // mb_convert_encoding -- ʸ»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤òÊÑ´¹¤¹¤ë
68 function mb_convert_encoding($str, $to_encoding, $from_encoding = '')
70 // ³ÈÄ¥: ÇÛÎó¤ò¼õ¤±¤é¤ì¤ë¤è¤¦¤Ë
71 // mb_convert_variableÂкö
73 foreach ($str as $key=>$value) {
74 $str[$key] = mb_convert_encoding($value, $to_encoding, $from_encoding);
78 return jcode_convert_encoding($str, $to_encoding, $from_encoding);
81 // mb_convert_variables -- ÊÑ¿ô¤Îʸ»ú¥³¡¼¥É¤òÊÑ´¹¤¹¤ë
82 function mb_convert_variables($to_encoding, $from_encoding, &$vars)
84 // Ãí: ²ÄÊÑĹ°ú¿ô¤Ç¤Ï¤Ê¤¤¡£init.php¤«¤é¸Æ¤Ð¤ì¤ë1°ú¿ô¤Î¥Ñ¥¿¡¼¥ó¤Î¤ß¤ò¥µ¥Ý¡¼¥È
85 // ÀµÄ¾¤Ë¼ÂÁõ¤¹¤ë¤Ê¤é¡¢²ÄÊÑ°ú¿ô¤ò¥ê¥Õ¥¡¥ì¥ó¥¹¤Ç¼õ¤±¤ëÊýË¡¤¬É¬Í×
86 if (is_array($from_encoding) || $from_encoding == '' || $from_encoding == 'auto')
87 $from_encoding = mb_detect_encoding(join_array(' ', $vars));
89 if ($from_encoding != 'ASCII' && $from_encoding != SOURCE_ENCODING)
90 $vars = mb_convert_encoding($vars, $to_encoding, $from_encoding);
92 return $from_encoding;
95 // Êä½õ´Ø¿ô:ÇÛÎó¤òºÆµ¢Åª¤Ëjoin¤¹¤ë
96 function join_array($glue, $pieces)
99 foreach ($pieces as $piece) {
100 $arr[] = is_array($piece) ? join_array($glue, $piece) : $piece;
102 return join($glue, $arr);
105 // mb_detect_encoding -- ʸ»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤ò¸¡½Ð¤¹¤ë
106 function mb_detect_encoding($str, $encoding_list = '')
108 static $codes = array(0=>'ASCII', 1=>'EUC-JP', 2=>'SJIS', 3=>'JIS', 4=>'UTF-8');
110 // Ãí: $encoding_list¤Ï»ÈÍѤ·¤Ê¤¤¡£
111 $code = AutoDetect($str);
112 if (! isset($codes[$code])) $code = 0; // oh ;(
114 return $codes[$code];
117 // mb_detect_order -- ʸ»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¸¡½Ð½ç½ø¤ÎÀßÄê/¼èÆÀ
118 function mb_detect_order($encoding_list = NULL)
120 static $list = array();
122 // Ãí: ¾¤Î´Ø¿ô¤Ë±Æ¶Á¤òµÚ¤Ü¤µ¤Ê¤¤¡£¸Æ¤ó¤Ç¤â̵°ÕÌ£¡£
123 if ($encoding_list === NULL) return $list;
125 $list = is_array($encoding_list) ? $encoding_list : explode(',', $encoding_list);
129 // mb_encode_mimeheader -- MIME¥Ø¥Ã¥À¤Îʸ»úÎó¤ò¥¨¥ó¥³¡¼¥É¤¹¤ë
130 function mb_encode_mimeheader($str, $charset = 'ISO-2022-JP', $transfer_encoding = 'B', $linefeed = "\r\n")
132 // Ãí: $transfer_encoding¤Ë´Ø¤ï¤é¤ºbase64¥¨¥ó¥³¡¼¥É¤òÊÖ¤¹
133 $str = mb_convert_encoding($str, $charset, 'auto');
134 return '=?' . $charset . '?B?' . $str;
137 // mb_http_output -- HTTP½ÐÎÏʸ»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤ÎÀßÄê/¼èÆÀ
138 function mb_http_output($encoding = '')
140 return SOURCE_ENCODING; // Ãí: ²¿¤â¤·¤Ê¤¤
143 // mb_internal_encoding -- ÆâÉôʸ»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤ÎÀßÄê/¼èÆÀ
144 function mb_internal_encoding($encoding = '')
146 return SOURCE_ENCODING; // Ãí: ²¿¤â¤·¤Ê¤¤
149 // mb_language -- ¥«¥ì¥ó¥È¤Î¸À¸ì¤òÀßÄê/¼èÆÀ
150 function mb_language($language = NULL)
152 static $mb_language = FALSE;
154 if ($language === NULL) return $mb_language;
155 $mb_language = $language;
157 return TRUE; // Ãí: ¾ï¤ËTRUE¤òÊÖ¤¹
160 // mb_strimwidth -- »ØÄꤷ¤¿Éý¤Çʸ»úÎó¤ò´Ý¤á¤ë
161 function mb_strimwidth($str, $start, $width, $trimmarker = '', $encoding = '')
163 if ($start == 0 && $width <= strlen($str)) return $str;
165 // Ãí: EUC-JPÀìÍÑ, $encoding¤ò»ÈÍѤ·¤Ê¤¤
166 $chars = unpack('C*', $str);
169 while (! empty($chars) && $start > 0) {
171 if (array_shift($chars) >= 0x80)
174 if ($b_trimmarker = (count($chars) > $width)) {
175 $width -= strlen($trimmarker);
177 while (! empty($chars) && $width-- > 0) {
178 $char = array_shift($chars);
180 if ($width-- == 0) break;
181 $substr .= chr($char);
182 $char = array_shift($chars);
184 $substr .= chr($char);
186 if ($b_trimmarker) $substr .= $trimmarker;
191 // mb_strlen -- ʸ»úÎó¤ÎŤµ¤òÆÀ¤ë
192 function mb_strlen($str, $encoding = '')
194 // Ãí: EUC-JPÀìÍÑ, $encoding¤ò»ÈÍѤ·¤Ê¤¤
195 return jstrlen($str);
198 // mb_substr -- ʸ»úÎó¤Î°ìÉô¤òÆÀ¤ë
199 function mb_substr($str, $start, $length = NULL, $encoding = '')
201 // Ãí: EUC-JPÀìÍÑ, $encoding¤ò»ÈÍѤ·¤Ê¤¤
202 return jsubstr($str, $start, ($length === NULL) ? jstrlen($str) : $length);