OSDN Git Service

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