Small update
[iDB.git] / inc / misc / utf8.php
blob2cd0c64e8b259fbe10b44279e556e57caae9cf60
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.
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") {
22 require('index.php');
23 exit(); }
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($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));
43 } else {
44 return join("",array_slice($ar[0],$start));
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); }
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
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 for ($i = 0; $i < strlen( $str ); $i++ ) {
89 $thisValue = ord( $str[ $i ] );
90 if ( $thisValue < 128 ) $unicode[] = $thisValue;
91 else {
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 );
98 $unicode[] = $number;
99 $values = array();
100 $lookingFor = 1;
101 } // if
102 } // if
103 } // for
104 return $unicode;
105 } // utf8_to_unicode
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 ) {
110 $utf8 = '';
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 ) );
117 } else {
118 $utf8.= chr( 224 + ( ( $unicode - ( $unicode % 4096 ) ) / 4096 ) );
119 $utf8.= chr( 128 + ( ( ( $unicode % 4096 ) - ( $unicode % 64 ) ) / 64 ) );
120 $utf8.= chr( 128 + ( $unicode % 64 ) );
121 } // if
122 } // foreach
123 return $utf8;
124 } // unicode_to_utf8