OSDN Git Service

* jcodeの将来的なアップデートのため、'JCODE_DIR'のdefineを追加
[pukiwiki/pukiwiki.git] / mbstring.php
1 <?php
2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
4 //
5 // $Id: mbstring.php,v 1.11 2004/06/19 03:58:43 henoheno Exp $
6 //
7
8 /*
9  * mbstring extension ¤¬¥µ¡¼¥Ð¡¼Â¦¤Ë¸ºß¤·¤Ê¤¤»þ¤ÎÂåÂشؿô
10  *
11  * Ãí°Õ»ö¹à
12  *
13  * 1. ¼ÂºÝ¤Ë´Á»ú¥³¡¼¥ÉÊÑ´¹¤ò¹Ô¤ï¤»¤ë¤¿¤á¤Ë¤Ï¡¢ÊÌÅÓjcode¤ò¥¤¥ó¥¹¥È¡¼¥ë¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£
14  *
15  *   http://www.spencernetwork.org/jcode/ ¤è¤êjcode¤òÆþ¼ê¤·¡¢
16  *   °Ê²¼¤ÎÍͤËŸ³«¤·¤Æ¤¯¤À¤µ¤¤¡£
17  * 
18  *   -+--- mbstring.php          -r--
19  *    +-+- jcode_1.34/           dr-x
20  *      +--- readme.txt          -r--
21  *      +--- jcode.phps          -r--
22  *      +--- jcode_wrapper.php   -r--
23  *      +--- code_table.ucs2jis  -r--
24  *      +--- code_table.jis2ucs  -r--
25  *
26  * 2. EUC-JPÀìÍѤǤ¹¡£(½ÐÎϤµ¤ì¤ë¥Ç¡¼¥¿¤¬EUC-JP¤Ç¤¢¤ëɬÍפ¬¤¢¤ê¤Þ¤¹)
27  *
28  */
29
30 // jcode¤Î½êºß
31 define('JCODE_DIR','./jcode_1.34');
32
33 if (is_readable(JCODE_DIR . '/jcode_wrapper.php'))
34 {
35         require_once(JCODE_DIR . '/jcode_wrapper.php');
36 }
37
38 // jcode¤¬Â¸ºß¤·¤Ê¤¤¾ì¹ç¡¢¥Þ¥ë¥Á¥Ð¥¤¥Èʸ»ú¤ä´Á»ú¥³¡¼¥É¤ò°·¤¨¤Ê¤¤
39 if (!function_exists('jcode_convert_encoding'))
40 {
41 //      die_message('Multibyte functions cannot be used. Please read "mbstring.php" for an additional installation procedure of "jcode".');
42         function jstrlen($str)
43         {
44                 return strlen($str);
45         }
46         function jsubstr($str,$start,$length)
47         {
48                 return substr($str,$start,$length);
49         }
50         function AutoDetect($str)
51         {
52                 return 0;
53         }
54         function jcode_convert_encoding($str,$to_encoding,$from_encoding)
55         {
56                 return $str;
57         }
58 }
59
60 // mb_convert_encoding -- Ê¸»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤òÊÑ´¹¤¹¤ë
61 function mb_convert_encoding($str,$to_encoding,$from_encoding='')
62 {
63         // ³ÈÄ¥: ÇÛÎó¤ò¼õ¤±¤é¤ì¤ë¤è¤¦¤Ë
64         // mb_convert_variableÂкö
65         if (is_array($str))
66         {
67                 foreach ($str as $key=>$value)
68                 {
69                         $str[$key] = mb_convert_encoding($value,$to_encoding,$from_encoding);
70                 }
71                 return $str;
72         }
73         return jcode_convert_encoding($str,$to_encoding,$from_encoding);
74 }
75
76 // mb_convert_variables -- ÊÑ¿ô¤Îʸ»ú¥³¡¼¥É¤òÊÑ´¹¤¹¤ë
77 function mb_convert_variables($to_encoding,$from_encoding,&$vars)
78 {
79         // Ãí: ²ÄÊÑĹ°ú¿ô¤Ç¤Ï¤Ê¤¤¡£init.php¤«¤é¸Æ¤Ð¤ì¤ë1°ú¿ô¤Î¥Ñ¥¿¡¼¥ó¤Î¤ß¤ò¥µ¥Ý¡¼¥È
80         // ÀµÄ¾¤Ë¼ÂÁõ¤¹¤ë¤Ê¤é¡¢²ÄÊÑ°ú¿ô¤ò¥ê¥Õ¥¡¥ì¥ó¥¹¤Ç¼õ¤±¤ëÊýË¡¤¬É¬Í×
81         if (is_array($from_encoding) or $from_encoding == '' or $from_encoding == 'auto')
82         {
83                 $from_encoding = mb_detect_encoding(join_array(' ',$vars));
84         }   
85         if ($from_encoding != 'ASCII' and $from_encoding != SOURCE_ENCODING)
86         {
87                 $vars = mb_convert_encoding($vars,$to_encoding,$from_encoding);
88         }
89         return $from_encoding;
90 }
91
92 // Êä½õ´Ø¿ô:ÇÛÎó¤òºÆµ¢Åª¤Ëjoin¤¹¤ë
93 function join_array($glue,$pieces)
94 {
95         $arr = array();
96         foreach ($pieces as $piece)
97         {
98                 $arr[] = is_array($piece) ? join_array($glue,$piece) : $piece;
99         }
100         return join($glue,$arr);
101 }
102
103 // mb_detect_encoding -- Ê¸»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤ò¸¡½Ð¤¹¤ë
104 function mb_detect_encoding($str,$encoding_list='')
105 {
106         static $codes = array(0=>'ASCII',1=>'EUC-JP',2=>'SJIS',3=>'JIS',4=>'UTF-8');
107         
108         // Ãí: $encoding_list¤Ï»ÈÍѤ·¤Ê¤¤¡£
109         $code = AutoDetect($str);
110         if (!array_key_exists($code,$codes))
111         {
112                 $code = 0; // oh ;(
113         }
114         return $codes[$code];
115 }
116
117 // mb_detect_order --  Ê¸»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¸¡½Ð½ç½ø¤ÎÀßÄê/¼èÆÀ 
118 function mb_detect_order($encoding_list=NULL)
119 {
120         static $list = array();
121         
122         // Ãí: Â¾¤Î´Ø¿ô¤Ë±Æ¶Á¤òµÚ¤Ü¤µ¤Ê¤¤¡£¸Æ¤ó¤Ç¤â̵°ÕÌ£¡£
123         if ($encoding_list === NULL)
124         {
125                 return $list;
126         }
127         $list = is_array($encoding_list) ? $encoding_list : explode(',',$encoding_list);
128         return TRUE;
129 }
130
131 // mb_encode_mimeheader -- MIME¥Ø¥Ã¥À¤Îʸ»úÎó¤ò¥¨¥ó¥³¡¼¥É¤¹¤ë
132 function mb_encode_mimeheader($str,$charset='ISO-2022-JP',$transfer_encoding='B',$linefeed="\r\n")
133 {
134         // Ãí: $transfer_encoding¤Ë´Ø¤ï¤é¤ºbase64¥¨¥ó¥³¡¼¥É¤òÊÖ¤¹
135         $str = mb_convert_encoding($str,$charset,'auto');
136         return '=?'.$charset.'?B?'.$str;
137 }
138
139 // mb_http_output -- HTTP½ÐÎÏʸ»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤ÎÀßÄê/¼èÆÀ
140 function mb_http_output($encoding='')
141 {
142         // Ãí: ²¿¤â¤·¤Ê¤¤
143         return SOURCE_ENCODING;
144 }
145
146 // mb_internal_encoding --  ÆâÉôʸ»ú¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤ÎÀßÄê/¼èÆÀ
147 function mb_internal_encoding($encoding='')
148 {
149         // Ãí: ²¿¤â¤·¤Ê¤¤
150         return SOURCE_ENCODING;
151 }
152
153 // mb_language --  ¥«¥ì¥ó¥È¤Î¸À¸ì¤òÀßÄê/¼èÆÀ 
154 function mb_language($language=NULL)
155 {
156         static $mb_language = FALSE;
157         if ($language === NULL)
158         {
159                 return $mb_language;
160         }
161         // Ãí: ¾ï¤ËTRUE¤òÊÖ¤¹
162         $mb_language = $language;
163         return TRUE;
164 }
165
166 // mb_strimwidth -- »ØÄꤷ¤¿Éý¤Çʸ»úÎó¤ò´Ý¤á¤ë
167 function mb_strimwidth($str,$start,$width,$trimmarker='',$encoding='')
168 {
169         if ($start == 0 and $width <= strlen($str))
170         {
171                 return $str;
172         }
173         
174         // Ãí: EUC-JPÀìÍÑ, $encoding¤ò»ÈÍѤ·¤Ê¤¤
175         $chars = unpack('C*', $str);
176         $substr = '';
177
178         while (count($chars) and $start > 0)
179         {
180                 $start--;
181                 if (array_shift($chars) >= 0x80)
182                 {
183                         array_shift($chars);
184                 }
185         }
186         if ($b_trimmarker = (count($chars) > $width))
187         {
188                 $width -= strlen($trimmarker);
189         }
190         while (count($chars) and $width-- > 0)
191         {
192                 $char = array_shift($chars);
193                 if ($char >= 0x80)
194                 {
195                         if ($width-- == 0)
196                         {
197                                 break;
198                         }
199                         $substr .= chr($char);
200                         $char = array_shift($chars);
201                 }
202                 $substr .= chr($char);
203         }
204         if ($b_trimmarker)
205         {
206                 $substr .= $trimmarker;
207         }
208         return $substr;
209 }
210
211 // mb_strlen -- Ê¸»úÎó¤ÎŤµ¤òÆÀ¤ë
212 function mb_strlen($str,$encoding='')
213 {
214         // Ãí: EUC-JPÀìÍÑ, $encoding¤ò»ÈÍѤ·¤Ê¤¤
215         return jstrlen($str);
216 }
217
218 // mb_substr -- Ê¸»úÎó¤Î°ìÉô¤òÆÀ¤ë
219 function mb_substr($str,$start,$length=NULL,$encoding='')
220 {
221         // Ãí: EUC-JPÀìÍÑ, $encoding¤ò»ÈÍѤ·¤Ê¤¤
222         return jsubstr($str,$start,($length === NULL) ? jstrlen($str) : $length);
223 }
224 ?>