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-2019 iDB Support - https://idb.osdn.jp/support/category.php?act=view&id=1
12 Copyright 2004-2019 Game Maker 2k - https://idb.osdn.jp/support/category.php?act=view&id=2
14 $FileInfo: utf8.php - Last Update: 08/02/2019 SVN 905 - 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($str,'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);
40 if(func_num_args() >= 3) {
41 $end = func_get_arg(2);
42 return join("",array_slice($ar[0],$start,$end));
44 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 ) {
88 for ($i = 0; $i < strlen( $str ); $i++
) {
89 $thisValue = ord( $str[ $i ] );
90 if ( $thisValue < 128 ) $unicode[] = $thisValue;
92 if ( count( $values ) == 0 ) $lookingFor = ( $thisValue < 224 ) ?
2 : 3;
93 $values[] = $thisValue;
94 if ( count( $values ) == $lookingFor ) {
95 $number = ( $lookingFor == 3 ) ?
96 ( ( $values[0] %
16 ) * 4096 ) +
( ( $values[1] %
64 ) * 64 ) +
( $values[2] %
64 ):
97 ( ( $values[0] %
32 ) * 64 ) +
( $values[1] %
64 );
107 // author: Scott Michael Reynen "scott@randomchaos.com"
108 // url: http://www.randomchaos.com/document.php?source=php_and_unicode
109 function unicode_to_utf8( $str ) {
111 foreach( $str as $unicode ) {
112 if ( $unicode < 128 ) {
113 $utf8.= chr( $unicode );
114 } elseif ( $unicode < 2048 ) {
115 $utf8.= chr( 192 +
( ( $unicode - ( $unicode %
64 ) ) / 64 ) );
116 $utf8.= chr( 128 +
( $unicode %
64 ) );
118 $utf8.= chr( 224 +
( ( $unicode - ( $unicode %
4096 ) ) / 4096 ) );
119 $utf8.= chr( 128 +
( ( ( $unicode %
4096 ) - ( $unicode %
64 ) ) / 64 ) );
120 $utf8.= chr( 128 +
( $unicode %
64 ) );