OSDN Git Service

00f583acebb4e4d2f45b29a79a2d99fc45b125b2
[idb/iDB.git.git] / inc / misc / utf8.php
1 <?php
2 /*
3     This program is free software; you can redistribute it and/or modify
4     it under the terms of the Revised BSD License.
5
6     This program is distributed in the hope that it will be useful,
7     but WITHOUT ANY WARRANTY; without even the implied warranty of
8     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9     Revised BSD License for more details.
10
11     Copyright 2004-2008 Cool Dude 2k - http://idb.berlios.de/
12     Copyright 2004-2008 Game Maker 2k - http://intdb.sourceforge.net/
13
14     $FileInfo: utf8.php - Last Update: 1/13/2008 SVN 227 - Author: cooldude2k $
15 */
16 // UTF8 helper functions
17 // author: Scott Michael Reynen "scott@randomchaos.com"
18 // url: http://www.randomchaos.com/document.php?source=php_and_unicode
19 // utf8_substr by frank at jkelloggs dot dk
20 $File3Name = basename($_SERVER['SCRIPT_NAME']);
21 if ($File3Name=="utf8.php"||$File3Name=="/utf8.php") {
22         require('index.php');
23         exit(); }
24
25 function utf8_strlen($str) {
26   return strlen(utf8_decode($_GET['text'])); }
27 function pre_strlen($str) {
28 global $chkcharset;
29 if($chkcharset=="UTF-8") {
30 if(!defined('UTF8_NOMBSTRING')&&function_exists('mb_strlen')) {
31 return mb_strlen($string,'utf-8'); }
32 else { return utf8_strlen($str); } }
33 if($chkcharset!="UTF-8") { return strlen($str); } }
34
35 // utf8_substr by frank at jkelloggs dot dk
36 // http://us3.php.net/manual/en/function.substr.php#55107
37 function utf8_substr($str,$start)
38 {
39    preg_match_all("/./su", $str, $ar);
40
41    if(func_num_args() >= 3) {
42        $end = func_get_arg(2);
43        return join("",array_slice($ar[0],$start,$end));
44    } else {
45        return join("",array_slice($ar[0],$start));
46    }
47 }
48 function pre_substr($string,$start,$length) {
49 global $chkcharset;
50 if($chkcharset=="UTF-8") {
51 if(!defined('UTF8_NOMBSTRING')&&function_exists('mb_substr')) {
52 return mb_substr($string,$start,$length,'utf-8'); }
53 else { return utf8_substr($string,$start,$length); } }
54 if($chkcharset!="UTF-8") { return substr($string,$start,$length); } }
55 if(isset($_GET['text'])) {
56 echo pre_substr($_GET['text'],0,6); }
57
58 // author: Scott Michael Reynen "scott@randomchaos.com"
59 // url: http://www.randomchaos.com/document.php?source=php_and_unicode
60 function utf8_strpos($haystack, $needle,$offset=0) {
61   if(!defined('UTF8_NOMBSTRING')&&function_exists('mb_strpos')) {
62   return mb_strpos($haystack,$needle,$offset,'utf-8'); }
63   $haystack = utf8_to_unicode($haystack);
64   $needle   = utf8_to_unicode($needle);
65   $position = $offset;
66   $found = false;
67   while( (! $found ) && ( $position < count( $haystack ) ) ) {
68     if ( $needle[0] == $haystack[$position] ) {
69       for ($i = 1; $i < count( $needle ); $i++ ) {
70         if ( $needle[$i] != $haystack[ $position + $i ] ) break;
71       } // for
72       if ( $i == count( $needle ) ) {
73         $found = true;
74         $position--;
75       } // if
76     } // if
77     $position++;
78   } // while
79   return ( $found == true ) ? $position : false;
80 } // strpos_unicode
81
82 // author: Scott Michael Reynen "scott@randomchaos.com"
83 // url: http://www.randomchaos.com/document.php?source=php_and_unicode
84 function utf8_to_unicode( $str ) {
85   $unicode = array();  
86   $values = array();
87   $lookingFor = 1;
88   
89   for ($i = 0; $i < strlen( $str ); $i++ ) {
90     $thisValue = ord( $str[ $i ] );
91     if ( $thisValue < 128 ) $unicode[] = $thisValue;
92     else {
93       if ( count( $values ) == 0 ) $lookingFor = ( $thisValue < 224 ) ? 2 : 3;
94       $values[] = $thisValue;
95       if ( count( $values ) == $lookingFor ) {
96   $number = ( $lookingFor == 3 ) ?
97     ( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ):
98         ( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 );
99   $unicode[] = $number;
100   $values = array();
101   $lookingFor = 1;
102       } // if
103     } // if
104   } // for
105   return $unicode;
106 } // utf8_to_unicode
107
108 // author: Scott Michael Reynen "scott@randomchaos.com"
109 // url: http://www.randomchaos.com/document.php?source=php_and_unicode
110 function unicode_to_utf8( $str ) {
111   $utf8 = '';
112   foreach( $str as $unicode ) {
113     if ( $unicode < 128 ) {
114       $utf8.= chr( $unicode );
115     } elseif ( $unicode < 2048 ) {
116       $utf8.= chr( 192 +  ( ( $unicode - ( $unicode % 64 ) ) / 64 ) );
117       $utf8.= chr( 128 + ( $unicode % 64 ) );
118     } else {
119       $utf8.= chr( 224 + ( ( $unicode - ( $unicode % 4096 ) ) / 4096 ) );
120       $utf8.= chr( 128 + ( ( ( $unicode % 4096 ) - ( $unicode % 64 ) ) / 64 ) );
121       $utf8.= chr( 128 + ( $unicode % 64 ) );
122     } // if
123   } // foreach
124   return $utf8;
125 } // unicode_to_utf8
126 ?>