OSDN Git Service

1fc765b4f11a3987126be914f4566547d065f5be
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / i18n.php
1 <?php
2 /*
3  * i18n class
4  * written by Takashi Sakamoto as of Apr.18, 2011
5  * This is wrapper functions of iconv and mbstring
6  * for multibyte processing.
7  * 
8  * Usage: Firstly i18n::initialize($charset),
9  *  then access to each static functions.
10  * 
11  */
12
13 class i18n {
14         private static $charset = '';
15         private static $iconv = FALSE;
16         private static $mbstring = FALSE;
17         
18         public static function initialize ($charset) {
19                 if (extension_loaded('iconv')) {
20                         self::$iconv = TRUE;
21                         if (iconv_set_encoding ('output_encoding', $charset)
22                          && iconv_set_encoding ('internal_encoding', $charset)){
23                                 $return =TRUE;
24                         } else {
25                                 $return =FALSE;
26                         }
27                 } else if (extension_loaded('mbstring')) {
28                         self::$mbstring = TRUE;
29                         if (mb_http_output($charset)
30                          && mb_internal_encoding($charset)
31                          && mb_regex_encoding($charset)) {
32                                 $return = TRUE;
33                          } else {
34                                 $return =FALSE;
35                          }
36                 } else {
37                         $return =FALSE;
38                 }
39                 self::$charset = $charset;
40                 return $return;
41         }
42         
43         public static function hsc ($string, $quotation) {
44                 return htmlspecialchars($string, $quotation, self::$charset);
45         }
46         
47         public static function convert ($target, $from, $to='') {
48                 $string = '';
49                 if (!$to) {
50                         $to = self::$charset;
51                 }
52                 if (self::$iconv) {
53                         $string = iconv($from, $to.'//TRANSLIT', $string);
54                 } else if (self::$mbstring) {
55                         $string = mb_convert_encoding($string, $to, $from);
56                 }
57                 return $string;
58         }
59         
60         public static function strlen ($string) {
61                 $length = 0;
62                 if (self::$iconv) {
63                         $length = iconv_strlen ($string, self::$charset);
64                 } else if (self::$mbstring) {
65                         $length = mb_strlen ($string, self::$charset);
66                 } else {
67                         $length = strlen ($string);
68                 }
69                 return $length;
70         }
71         
72         public static function strpos ($haystack, $needle, $offset=0) {
73                 $position = 0;
74                 if (self::$iconv) {
75                         $position = iconv_strpos ($haystack, $needle, $offset, self::$charset);
76                 } else if (self::$mbstring) {
77                         $position = mb_strpos ($haystack, $needle, $offset, self::$charset);
78                 } else {
79                         $position = strpos ($haystack, $needle, $offset);
80                 }
81                 return $position;
82         }
83         
84         public static function strrpos ($haystack, $needle) {
85                 $position = 0;
86                 if (self::$iconv) {
87                         $position = iconv_strrpos ($haystack, $needle, self::$charset);
88                 } else if (self::$mbstring) {
89                         $position = mb_strrpos ($haystack, $needle, 0, self::$charset);
90                 } else {
91                         $position = strrpos ($haystack, $needle, 0);
92                 }
93                 return $position;
94         }
95         
96         public static function substr ($string, $start, $length=0) {
97                 $return = 0;
98                 if (self::$iconv) {
99                         $return = iconv_substr ($string, $start, $length, self::$charset);
100                 } else if (self::$mbstring) {
101                         $return = mb_substr ($string, $start, $length, self::$charset);
102                 } else {
103                         $return = strrpos ($string, $start, $length);
104                 }
105                 return $return;
106         }
107         
108         /*
109          * we should use preg_split function instead of this, I think.
110          */
111         public static function explode ($delimiter, $target, $limit=0) {
112                 $array = array();
113                 for ($count=0; $limit == 0 || $count < $limit; $count++) {
114                         $offset = self::strpos($target, $delimiter);
115                         if ($offset == 0) {
116                                         $array[] = $target;
117                                 break;
118                         }
119                         $before = self::substr($target, 0, $offset);
120                         $array[] = $before;
121                         $length = self::strlen($target) - self::strlen($before);
122                         $target = self::substr($target, $offset+1, $length);
123                 }
124                 return $array;
125         }
126         
127         public static function strftime ($format, $timestamp='') {
128                 $formatted = '';
129                 if (setlocale(LC_CTYPE, 0) == 'Japanese_Japan.932') {
130                         $formatted = iconv('CP932', self::$charset, strftime(iconv(self::$charset, 'CP932', $format),$timestamp));
131                 } else {
132                         $formatted = strftime($format,$timestamp);
133                 }
134                 return $formatted;
135         }
136 }