4 * written by Takashi Sakamoto as of Apr.18, 2011
5 * This is wrapper functions of iconv and mbstring
6 * for multibyte processing.
8 * Usage: Firstly i18n::initialize($charset),
9 * then access to each static functions.
14 private static $charset = '';
15 private static $iconv = FALSE;
16 private static $mbstring = FALSE;
18 public static function initialize ($charset) {
19 if (extension_loaded('iconv')) {
21 if (iconv_set_encoding ('output_encoding', $charset)
22 && iconv_set_encoding ('internal_encoding', $charset)){
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)) {
39 self::$charset = $charset;
43 public static function hsc ($string, $quotation) {
44 return htmlspecialchars($string, $quotation, self::$charset);
47 public static function convert ($target, $from, $to='') {
53 $string = iconv($from, $to.'//TRANSLIT', $string);
54 } else if (self::$mbstring) {
55 $string = mb_convert_encoding($string, $to, $from);
60 public static function strlen ($string) {
63 $length = iconv_strlen ($string, self::$charset);
64 } else if (self::$mbstring) {
65 $length = mb_strlen ($string, self::$charset);
67 $length = strlen ($string);
72 public static function strpos ($haystack, $needle, $offset=0) {
75 $position = iconv_strpos ($haystack, $needle, $offset, self::$charset);
76 } else if (self::$mbstring) {
77 $position = mb_strpos ($haystack, $needle, $offset, self::$charset);
79 $position = strpos ($haystack, $needle, $offset);
84 public static function strrpos ($haystack, $needle) {
87 $position = iconv_strrpos ($haystack, $needle, self::$charset);
88 } else if (self::$mbstring) {
89 $position = mb_strrpos ($haystack, $needle, 0, self::$charset);
91 $position = strrpos ($haystack, $needle, 0);
96 public static function substr ($string, $start, $length=0) {
99 $return = iconv_substr ($string, $start, $length, self::$charset);
100 } else if (self::$mbstring) {
101 $return = mb_substr ($string, $start, $length, self::$charset);
103 $return = strrpos ($string, $start, $length);
109 * we should use preg_split function instead of this, I think.
111 public static function explode ($delimiter, $target, $limit=0) {
113 for ($count=0; $limit == 0 || $count < $limit; $count++) {
114 $offset = self::strpos($target, $delimiter);
119 $before = self::substr($target, 0, $offset);
121 $length = self::strlen($target) - self::strlen($before);
122 $target = self::substr($target, $offset+1, $length);
127 public static function strftime ($format, $timestamp='') {
129 if (setlocale(LC_CTYPE, 0) == 'Japanese_Japan.932') {
130 $formatted = iconv('CP932', self::$charset, strftime(iconv(self::$charset, 'CP932', $format),$timestamp));
132 $formatted = strftime($format,$timestamp);