4 * written by Takashi Sakamoto as of Jun.5, 2011
5 * This is wrapper functions of iconv and mbstring
6 * for multibyte processing.
10 private static $charset = '';
11 private static $iconv = FALSE;
12 private static $mbstring = FALSE;
14 public static function init ($charset) {
15 if (extension_loaded('iconv')) {
17 if (iconv_set_encoding ('output_encoding', $charset)
18 && iconv_set_encoding ('internal_encoding', $charset)){
23 } else if (extension_loaded('mbstring')) {
24 self::$mbstring = TRUE;
25 if (mb_http_output($charset)
26 && mb_internal_encoding($charset)
27 && mb_regex_encoding($charset)) {
35 self::$charset = $charset;
36 return (boolean) $return;
39 public static function hsc ($string, $quotation=ENT_QUOTES) {
40 return (string) htmlspecialchars($string, $quotation, self::$charset);
43 public static function convert ($target, $from, $to='') {
49 $string = iconv($from, $to.'//TRANSLIT', $string);
50 } else if (self::$mbstring) {
51 $string = mb_convert_encoding($string, $to, $from);
53 return (string) $string;
56 public static function strlen ($string) {
59 $length = iconv_strlen ($string, self::$charset);
60 } else if (self::$mbstring) {
61 $length = mb_strlen ($string, self::$charset);
63 $length = strlen ($string);
65 return (integer) $length;
68 public static function strpos ($haystack, $needle, $offset=0) {
71 $position = iconv_strpos ($haystack, $needle, $offset, self::$charset);
72 } else if (self::$mbstring) {
73 $position = mb_strpos ($haystack, $needle, $offset, self::$charset);
75 $position = strpos ($haystack, $needle, $offset);
77 return (integer) $position;
80 public static function strrpos ($haystack, $needle) {
83 $position = iconv_strrpos ($haystack, $needle, self::$charset);
84 } else if (self::$mbstring) {
85 $position = mb_strrpos ($haystack, $needle, 0, self::$charset);
87 $position = strrpos ($haystack, $needle, 0);
89 return (integer) $position;
92 public static function substr ($string, $start, $length=0) {
95 $return = iconv_substr ($string, $start, $length, self::$charset);
96 } else if (self::$mbstring) {
97 $return = mb_substr ($string, $start, $length, self::$charset);
99 $return = strrpos ($string, $start, $length);
101 return (string) $return;
104 public static function explode ($delimiter, $target, $limit=0) {
106 if (preg_match("#$delimiter#", $target) === 0) {
109 for ($count=0; $limit == 0 || $count < $limit; $count++) {
110 $offset = self::strpos($target, $delimiter);
111 if ($array != array() && $offset == 0) {
115 $array[] = self::substr($target, 0, $offset);
116 $length = self::strlen($target) - $offset;
117 $target = self::substr($target, $offset+1, $length);
120 return (array) $array;
123 public static function strftime ($format, $timestamp='') {
126 if (preg_match('#%#', $format) === 0) {
129 $format = trim(preg_replace('#(%.)#', ',$1,', $format), ',');
130 $elements = self::explode(',', $format);
132 foreach ($elements as $element) {
133 if (preg_match('#^%.$#', $element)) {
134 $formatted .= strftime($element, $timestamp);
136 $formatted .= $element;
140 return (string) $formatted;
144 * This function is experimental, need to be tested in various PHP environment
146 public static function mail($to, $subject, $message, $from) {
150 'input-charset' => self::$charset,
151 'output-charset' => self::$charset,
153 'line-break-chars' => "\r\n");
155 $subject = iconv_mime_encode('Subject', $subject, $options);
156 $addition = iconv_mime_encode('From', $from, $options);
158 $result = mail($to, $subject, $message, $addition);
159 } else if (self::$mbstring) {
160 $result = mb_send_mail ($to, $subject, $message, "From: $from");
162 $result = mail ($to, $subject, $message, "From: $from");
164 return (boolean) $result;