Kyberia v1.0
[mirrors/Kyberia-bloodline.git] / inc / string.inc
1 <?php
2 /*
3 * String.inc 1.00 2001/03/08
4 *
5 * Copyright 2001 Czech On Line a.s. All Rights Reserved.
6 */
7
8 class String {
9
10 function String() {
11 }
12
13 function escapeSpecialChars($str, $dir = "to") {
14 $from[] = '\9c';
15 $to[] = '&#339;';
16 $from[] = 'ß';
17 $to[] = '&szlig;';
18 $from[] = 'â';
19 $to[] = '&acirc;';
20 $from[] = 'ê';
21 $to[] = '&ecirc;';
22 $from[] = 'à';
23 $to[] = '&agrave;';
24 $from[] = 'î';
25 $to[] = '&icirc;';
26 $from[] = 'ç';
27 $to[] = '&ccedil;';
28 $from[] = 'ô';
29 $to[] = '&ocirc;';
30 $from[] = 'û';
31 $to[] = '&ucirc;';
32
33 if ($dir == "to") {
34 return str_replace($from, $to, $str);
35 } elseif ($dir == "from") {
36 return str_replace($to, $from, $str);
37 }
38 }
39
40 function unhtmlspecialchars($str) {
41 $trans = get_html_translation_table(HTML_SPECIALCHARS);
42 $trans = array_flip($trans);
43 return strtr($str, $trans);
44 }
45
46
47 function cutString($string, $cut_limit, $cut_words = 0) {
48 // returns $string cutted to lenght $cut_limit or limited to $cut_words words
49 // if $cut_words is used, $cut_limit is ignored
50 // returned string is also choped and trimed
51 $str_tmp = trim(chop($string));
52 $StringArray=explode(" ", $str_tmp);
53 if ($cut_words) {
54 if (count($StringArray) < $cut_words) {
55 return $str_tmp;
56 }
57 $string_cut = "";
58 for ($i=0; $i < $cut_words; $i++) {
59 $string_cut .= " " . $StringArray[$i];
60 }
61 return ltrim($string_cut) . "...";
62 } else {
63 if ($cut_limit > strlen($str_tmp)) {
64 return $str_tmp;
65 }
66 $string_cut = "";
67 $threedots_length = 3;
68 $i = 0;
69 while (strlen($string_cut)+strlen($StringArray[$i])+$threedots_length < $cut_limit) {
70 $string_cut .= " " . $StringArray[$i];
71 $i++;
72 }
73 return ltrim($string_cut) . "...";
74 }
75 }
76
77 function escapeSql($str) {
78 # single quote ('), double quote ("), backslash (\) and
79 # NUL (the NULL byte)
80 $str = addslashes($str);
81 return $str;
82 }
83
84 function unescapeSql($str) {
85 $str = stripslashes($str);
86 return $str;
87 }
88
89 function escapeSqlLike($str) {
90 $str = $this->escapeSql($str);
91 $str = str_replace('%','\%', $str);
92 $str = str_replace('_','\_', $str);
93 return $sql;
94 }
95
96 function unescapeSqlLike($str) {
97 $str = $this->unescapeSql($str);
98 $str = str_replace('\%','%',$str);
99 $str = str_replace('\_','_',$str);
100 return $str;
101 }
102
103 /**
104 * Recode a <code>string</code> into plain ascii string
105 * replace <code>czech</code> characters
106 *
107 * @param string
108 * @return a string
109 */
110 function recodeToAscii($string) {
111 return strtr($string, "áèïéìíòóø\9a\9dúùý\9eÁÈÏÉÌÍÒÓØ\8a\8dÚÙÝ\8e¹»¾©«®", "acdeeinorstuuyzACDEEINORSTUUYZstzSTZ");
112 }
113
114 /*
115 * winToIso(string)
116 * isoToWin(string)
117 * converts czech code pages: Windows CP1250 to Latin ISO-8859-2
118 */
119
120 function winToIso($string) {
121 return strtr($string,'\9a\9d\9e\8a\8d\8e','¹»¾©«®');
122 }
123 function isoToWin($string) {
124 return strtr($string,'¹»¾©«®','\9a\9d\9e\8a\8d\8e');
125 }
126
127 /**
128 * Next 4 function are for changing case of a <code>string</code> within its coding.
129 *
130 * @param string
131 * @return a string
132 */
133
134 function strToUpperCZ($str) {
135 $str = strtoupper(strtr($str, 'áèïéìíå¾òóø\9a\9dúùý\9e¹»¾', 'ÁÈÏÉÌÍżÒÓØ\8a\8dÚÙÝ\8e©«®'));
136 return $str;
137 }
138
139 function strToUpperCS($str) {
140 return $this->strToUpperCZ($str);
141 }
142
143 function strToLowerCZ($str) {
144 $str = strtolower(strtr($str, 'ÁÈÏÉÌÍżÒÓØ\8a\8dÚÙÝ\8e©«®', 'áèïéìíå¾òóø\9a\9dúùý\9e¹»¾'));
145 return $str;
146 }
147
148 function strToLowerCS($str) {
149 return $this->strToLowerCZ($str);
150 }
151
152 function dateToSql($value) {
153 return (sprintf("%04d-%02d-%02d", $value["year"], $value["mon"], $value["mday"]));
154 }
155
156 function sqlToDate($value) {
157 $a["year"]=(int)substr($value,0,4);
158 $a["mon"]=(int)substr($value,5,2);
159 $a["mday"]=(int)substr($value,8,2);
160 return ($a);
161 }
162
163 function timeToSql($value) {
164 return (sprintf("%02d:%02d:%02d", $value["hours"], $value["minutes"], $value["seconds"]));
165 }
166
167 function sqlToTime($value) {
168 $a["hours"]=(int)substr($value,0,2);
169 $a["minutes"]=(int)substr($value,3,2);
170 $a["seconds"]=(int)substr($value,6,2);
171 return ($a);
172 }
173
174 function dateTimeToSql($value) {
175 return (sprintf("%04d-%02d-%02d %02d:%02d:%02d", $value["year"], $value["mon"], $value["mday"], $value["hours"], $value["minutes"], $value["seconds"]));
176 }
177
178 function sqlToDateTime($value) {
179 $a["year"]=(int)substr($value,0,4);
180 $a["mon"]=(int)substr($value,5,2);
181 $a["mday"]=(int)substr($value,8,2);
182 $a["hours"]=(int)substr($value,11,2);
183 $a["minutes"]=(int)substr($value,14,2);
184 $a["seconds"]=(int)substr($value,17,2);
185 return ($a);
186 }
187
188 function sqlToUnix($value) {
189 $dt = $this->sqlToDateTime($value);
190 return mktime($dt['hours'], $dt['minutes'], $dt['seconds'],
191 $dt['mon'], $dt['mday'], $dt['year']);
192 }
193
194 function czTime($value, $seconds=TRUE) {
195 if ($seconds) {
196 return sprintf("%d:%02d:%02d", $value['hours'], $value['minutes'],
197 $value['seconds']);
198 }
199 else {
200 return sprintf("%d:%02d", $value['hours'], $value['minutes']);
201 }
202 }
203
204 function czDate($value) {
205 return sprintf("%d.%d.%d", $value['mday'], $value['mon'],
206 $value['year']);
207 }
208
209 function encodeString($string) {
210 for ($i = 0;$i < strlen($string);$i++) {
211 $ch = substr($string,$i,1);
212 $ch = ord($ch)+100;
213 $encoded_string .= dechex($ch);
214 }
215 return $encoded_string;
216 }
217
218 function decodeString($string) {
219 for ($i = 0;$i < strlen($string);$i += 2) {
220 $ch = substr($string,$i,2);
221 $ch = hexdec($ch);
222 $ch -= 100;
223 $decode_string .= chr($ch);
224 }
225 return $decode_string;
226 }
227 }
228 ?>
This page took 0.312625 seconds and 4 git commands to generate.