OSDN Git Service

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