3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the Revised BSD License.
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.
11 Copyright 2004-2008 Cool Dude 2k - http://idb.berlios.de/
12 Copyright 2004-2008 Game Maker 2k - http://intdb.sourceforge.net/
14 $FileInfo: utf8.php - Last Update: 1/13/2008 SVN 227 - Author: cooldude2k $
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") {
25 function utf8_strlen($str) {
26 return strlen(utf8_decode($_GET['text'])); }
27 function pre_strlen($str) {
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); } }
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)
39 preg_match_all("/./su", $str, $ar);
41 if(func_num_args() >= 3) {
42 $end = func_get_arg(2);
43 return join("",array_slice($ar[0],$start,$end));
45 return join("",array_slice($ar[0],$start));
48 function pre_substr($string,$start,$length) {
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); }
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);
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;
72 if ( $i == count( $needle ) ) {
79 return ( $found == true ) ? $position : false;
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 ) {
89 for ($i = 0; $i < strlen( $str ); $i++ ) {
90 $thisValue = ord( $str[ $i ] );
91 if ( $thisValue < 128 ) $unicode[] = $thisValue;
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 );
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 ) {
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 ) );
119 $utf8.= chr( 224 + ( ( $unicode - ( $unicode % 4096 ) ) / 4096 ) );
120 $utf8.= chr( 128 + ( ( ( $unicode % 4096 ) - ( $unicode % 64 ) ) / 64 ) );
121 $utf8.= chr( 128 + ( $unicode % 64 ) );