Menu

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

Download this file

291 lines (232 with data), 7.4 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
#
# 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";
$resp = trim( fgets(STDIN) );
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} != '%' ? my_char_urlencode($str{$i}) : '%';
}
return $nstr;
}
function my_char_urlencode($ch) {
$spec = array('+'=>'%2B', '?'=>'%3F');
if ( isset( $spec[$ch] ) ) {
return $spec[$ch];
}
return urlencode($ch);
}
# returns the text inside the $element element
function getCDATA($element, $data) {
$start = strpos ( $data, ">", strpos ( $data, "<$element" ) );
$data = substr_replace ( $data, "", 0, $start + 1 );
$end = strpos ( $data, "</$element>" );
return substr_replace ( $data, "", $end );
/*
Taken out by Grigor Gatchev -
seems that PHP PCRE functions cannot handle subpatterns longer than 64K.
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);
}
# ----- Smuggled in by Grigor Gatchev ----- #
function strip_parentheses ( $text ) {
$start = strpos ( $text, "(" );
$end = strrpos ( $text, ")" );
return substr ( $text, $start + 1, ( $end - $start - 1 ) );
}
function extract_element ( $regex, $string, $flags = '', $offset = 0 ) {
preg_match ( $regex, $string, $matches, $start, $offset );
return $matches[1];
}
function extract_span_content ( $text ) {
return extract_element ( '/<span .*>(.*)<\/span>/Us', $text );
}
function extract_first_element ( $element, $line ) {
if ( preg_match ( '/(' . $element . ')/Us', $line, $matches ) > 0 ) {
return $matches[1];
} else {
return false;
}
}
function strip_first_element ( $element, $line, $things_before_it = true ) {
$found = extract_first_element ( $element, $line );
if ( $found ) {
$offset = strlen ( $found );
if ( $things_before_it ) {
$offset += strpos ( $line, $found );
}
return substr ( $line, $offset );
} else {
return false;
}
}
function parse_first_href ( $line ) {
$href = extract_first_element ( '<a href=.*<\/a>', $line );
if ( $href ) {
preg_match ( '/<a href="(.*)".*title="(.*)">(.*)<\/a>/Us', $href, $matches );
$parts = array();
$parts['url'] = $matches[1];
$parts['title'] = $matches[2];
$parts['text'] = $matches[3];
return $parts;
} else {
return false;
}
}
function strip_first_href ( $line, $things_before_it = true ) {
return strip_first_element ( '<a href=.*<\/a>', $line, $things_before_it );
}
function parse_first_maybe_href ( $href ) {
$parts = parse_first_href ( $href );
if ( ! ( $parts ) ) {
$parts = array();
$parts['text'] = $href;
}
return $parts;
}
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.