Menu

[r33]: / trunk / utils.php  Maximize  Restore  History

Download this file

196 lines (164 with data), 5.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
# $Id$
#
# utils
#
# Copyright (C) 2004 Borislav Manolov
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# http://www.gnu.org/copyleft/gpl.html
#
# Author: Borislav Manolov <b.manolov at gmail dot com>
# http://purl.org/NET/borislav/
#############################################################################
$cyrs = array(
' а',' б',' в',' г',' д',' е',' ж',' з',' и',' й',
' к',' л',' м',' н',' о',' п',' р',' с',' т',' у',
' ф',' х',' ц',' ч',' ш',' щ',' ъ',' ю',' я',
' А',' Б',' В',' Г',' Д',' Е',' Ж',' З',' И',' Й',
' К',' Л',' М',' Н',' О',' П',' Р',' С',' Т',' У',
' Ф',' Х',' Ц',' Ч',' Ш',' Щ',' Ъ',' Ю',' Я',
' ');
$lats = array(
'a', 'b', 'v', 'g', 'd', 'e', 'zh', 'z', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u',
'f', 'h', 'c', 'ch', 'sh', 'sht', 'j', 'y', 'ju', 'ja',
'A', 'B', 'V', 'G', 'D', 'E', 'Zh', 'Z', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U',
'F', 'H', 'C', 'Ch', 'Sh', 'Sht', 'J', 'Y', 'Ju', 'Ja',
' ');
# change the string encoding from windows-1251 to UTF-8
function utf8($str) {
return iconv('windows-1251', 'UTF-8', $str);
}
function cyr2hex($str) {
return urlencode( iconv('windows-1251', 'UTF-8', $str) );
}
function cyr2lat($str) {
global $cyrs, $lats;
return str_replace($cyrs, $lats, $str);
}
# read a line from the console
function my_readline($str = 'Enter required data: ') {
echo "\n$str";
$fp = fopen('php://stdin', 'r');
$resp = trim( fgets($fp) );
fclose($fp);
return $resp;
}
function my_fwrite($file, $text, $mode='a+') {
$myFile = @fopen($file, $mode);
if (! $myFile) return false;
flock($myFile, LOCK_EX);
if (! @fputs($myFile, $text)) return false;
flock($myFile, LOCK_UN);
if (! @fclose($myFile)) return false;
return true;
}
function my_urlencode($str) {
$len = strlen($str);
for ($i = 0; $i < $len; $i++) {
$nstr .= $str{$i} != '%' ? urlencode($str{$i}) : '%';
}
return $nstr;
}
# returns the text inside the $element element
function getCDATA($element, $data) {
preg_match("/<$element.*>(.+)<\/$element>/Us", $data, $matches);
return $matches[1];
}
# returns the text inside the $element elements
function getCDATAs($element, $data) {
preg_match_all("/<$element.*>(.+)<\/$element>/Us", $data, $matches);
return $matches[1];
}
# original source: http://www.randomchaos.com/document.php?source=php_and_unicode
function utf82unicode($str) {
$unicode = '';
$values = array();
$lookingFor = 1;
for ($i = 0; $i < strlen($str); $i++ ) {
$thisValue = ord( $str{$i} );
if ( $thisValue < 128 ) {
$unicode .= $str{$i}; # don't convert ASCII characters
} else {
if ( count($values) == 0 ) {
$lookingFor = ( $thisValue < 224 ) ? 2 : 3;
}
$values[] = $thisValue;
if ( count( $values ) == $lookingFor ) {
$number = ( $lookingFor == 3 )
?
( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 )
:
( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 );
$unicode .= "&#$number;";
$values = array();
$lookingFor = 1;
}
}
}
return $unicode;
}
# encodes a unicode sequence as UTF-8
function unicode2utf8($unicode_str) {
$base = 10;
if ($unicode_str[0] == 'x') {
$unicode_str = substr($unicode_str, 1);
$base = 16;
}
$bin = base_convert($unicode_str, $base, 2);
$len = strlen($bin);
if ( $len < 8 ) {
// 0xxxxxxx
$bin = '0' . str_pad($bin, 7, '0', STR_PAD_LEFT);
} elseif ( $len < 12 ) {
// 110xxxxx 10xxxxxx
$bin = str_pad($bin, 11, '0', STR_PAD_LEFT);
$bin = substr_replace($bin, '10', 5, 0);
$bin = '110' . $bin;
} elseif ( $len < 17 ) {
// 1110xxxx 10xxxxxx 10xxxxxx
$bin = str_pad($bin, 16, '0', STR_PAD_LEFT);
$bin = substr_replace($bin, '10', 4, 0);
$bin = substr_replace($bin, '10', 12, 0);
$bin = '1110' . $bin;
} else {
// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
$bin = str_pad($bin, 21, '0', STR_PAD_LEFT);
$bin = substr_replace($bin, '10', 3, 0);
$bin = substr_replace($bin, '10', 11, 0);
$bin = substr_replace($bin, '10', 19, 0);
$bin = '11110' . $bin;
}
$hex = base_convert($bin, 2, 16);
$hex = strtoupper($hex);
$len = strlen($hex);
if ($len == 1) {
$utf8 = '%0' . $hex;
} else {
for ($i = 0; $i < strlen($hex); $i+=2) {
$utf8 .= '%' . $hex[$i] . $hex[$i+1];
}
}
return $utf8;
}
# escape all regexp meta-characters in a string
function escape_regexp($str) {
return str_replace(
array('\\', '^', '$', '.', '[', ']', '|', '(', ')', '?', '*', '+', '{', ']', '/'),
array('\\\\', '\^', '\$', '\.', '\[', '\]', '\|', '\(', '\)', '\?', '\*', '\+', '\{', '\]', '\/'),
$str);
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.