PHP 字符类
1
<?php
2
#============================
3
# Filename: string.class.php
4
# Note : 字符串管理
5
# Update : 2008-5-8
6
# Cool!!
7
#============================
8
class QG_C_STRING
9
{
10
var $script = false;
11
var $iframe = false;
12
var $style = false;
13
14
function __construct($script=false,$iframe=false,$style=false)
15
{
16
$this->script = $script;
17
$this->iframe = $iframe;
18
$this->style = $style;
19
}
20
21
#[兼容PHP4]
22
function QG_C_STRING($script=false,$iframe=false,$style=false)
23
{
24
$this->__construct($script,$iframe,$style);
25
}
26
27
function __destruct()
28
{
29
return true;
30
}
31
32
#[设置状态属性]
33
function set($var,$status)
34
{
35
$this->$var = $status;
36
}
37
38
function safe($msg)
39
{
40
if(!$msg)
41
{
42
return false;
43
}
44
if(is_array($msg))
45
{
46
foreach($msg AS $key=>$value)
47
{
48
$msg[$key] = $this->safe($value);
49
}
50
}
51
else
52
{
53
$msg = trim($msg);
54
$old = array("&"," ","'",'"',"<",">","\t","\r");
55
$new = array("&"," ","'",""","<",">"," ","");
56
$msg = str_replace($old,$new,$msg);
57
$msg = str_replace(" "," ",$msg);
58
$old = array("/<script(.*)<\/script>/isU","/<frame(.*)>/isU","/<\/fram(.*)>/isU","/<iframe(.*)>/isU","/<\/ifram(.*)>/isU","/<style(.*)<\/style>/isU");
59
$new = array("","","","","","");
60
$msg = preg_replace($old,$new,$msg);
61
}
62
return $msg;
63
}
64
65
function html($msg)
66
{
67
if(is_array($msg))
68
{
69
foreach($msg AS $key=>$value)
70
{
71
$msg[$key] = $this->html($value);
72
}
73
}
74
else
75
{
76
$msg = trim($msg);
77
$msg = stripslashes($msg);
78
if(!$this->script)
79
{
80
$msg = preg_replace("/<script(.*)<\/script>/isU","",$msg);
81
}
82
if(!$this->iframe)
83
{
84
$msg = preg_replace("/<frame(.*)>/isU","",$msg);
85
$msg = preg_replace("/<\/fram(.*)>/isU","",$msg);
86
$msg = preg_replace("/<iframe(.*)>/isU","",$msg);
87
$msg = preg_replace("/<\/ifram(.*)>/isU","",$msg);
88
}
89
if(!$this->style)
90
{
91
$msg = preg_replace("/<style(.*)<\/style>/isU","",$msg);
92
}
93
#[超链接在新窗口打开]
94
$msg = preg_replace("/<a(.*)target=[ |'|\"](.*)[ |'|\"](.*)>/isU","<a\\1 \\3>",$msg);
95
$msg = preg_replace("/<a(.*)>/isU","<a\\1 target='_blank'>",$msg);
96
#[替换网址]
97
$url = $this->get_url();
98
$msg = str_replace($url,"",$msg);
99
$msg = addslashes($msg);
100
}
101
return $msg;
102
}
103
104
#[截取字符长度,仅支持UTF-8]
105
function cut($string,$length,$dot="…")
106
{
107
if(strlen($string) <= $length)
108
{
109
return $string;
110
}
111
$strcut = '';
112
$n = $tn = $noc = 0;
113
while ($n < strlen($string))
114
{
115
$t = ord($string[$n]);
116
if($t == 9 || $t == 10 || (32 <= $t && $t <= 126))
117
{
118
$tn = 1; $n++; $noc++;
119
}
120
elseif(194 <= $t && $t <= 223)
121
{
122
$tn = 2; $n += 2; $noc += 2;
123
}
124
elseif(224 <= $t && $t < 239)
125
{
126
$tn = 3; $n += 3; $noc += 2;
127
}
128
elseif(240 <= $t && $t <= 247)
129
{
130
$tn = 4; $n += 4; $noc += 2;
131
}
132
elseif(248 <= $t && $t <= 251)
133
{
134
$tn = 5; $n += 5; $noc += 2;
135
}
136
elseif($t == 252 || $t == 253)
137
{
138
$tn = 6; $n += 6; $noc += 2;
139
}
140
else
141
{
142
$n++;
143
}
144
145
if ($noc >= $length)
146
{
147
break;
148
}
149
}
150
if ($noc > $length)
151
{
152
$n -= $tn;
153
}
154
$strcut = substr($string, 0, $n);
155
return $strcut.$dot;
156
}
157
158
#[编码转换,使用PHP里的iconv功能]
159
function charset($msg, $s_code="UTF-8", $e_code="GBK")
160
{
161
if(!$msg)
162
{
163
return false;
164
}
165
if(is_array($msg))
166
{
167
foreach($msg AS $key=>$value)
168
{
169
$msg[$key] = $this->charset($value,$s_code,$e_code);
170
}
171
}
172
else
173
{
174
if(function_exists("iconv"))
175
{
176
$msg = iconv($s_code,$e_code,$msg);
177
}
178
}
179
return $msg;
180
}
181
182
function format($msg,$f=false)
183
{
184
$status = get_magic_quotes_gpc();
185
if(!$status || $f)
186
{
187
if(is_array($msg))
188
{
189
foreach($msg AS $key=>$value)
190
{
191
$msg[$key] = $this->format($value,$f);
192
}
193
}
194
else
195
{
196
$msg = addslashes($msg);
197
}
198
}
199
return $msg;
200
}
201
202
function num_format($a,$ext=2)
203
{
204
if(!$a || $a == 0)
205
{
206
return false;
207
}
208
if($a <= 1024)
209
{
210
$a = "1 KB";
211
}
212
elseif($a>1024 && $a<(1024*1024))
213
{
214
$a = round(($a/1024),$ext)." KB";
215
}
216
elseif($a>=(1024*1024) && $a<(1024*1024*1024))
217
{
218
$a = round(($a/(1024*1024)),$ext)." MB";
219
}
220
else
221
{
222
$a = round(($a/(1024*1024*1024)),$ext)." GB";
223
}
224
return $a;
225
}
226
227
function get_url()
228
{
229
$myurl = "http://".str_replace("http://","",$_SERVER["SERVER_NAME"]);
230
$docu = $_SERVER["PHP_SELF"];
231
$array = explode("/",$docu);
232
$count = count($array);
233
if($count>1)
234
{
235
foreach($array AS $key=>$value)
236
{
237
$value = trim($value);
238
if($value)
239
{
240
if(($key+1) < $count)
241
{
242
$myurl .= "/".$value;
243
}
244
}
245
}
246
}
247
$myurl .= "/";
248
return $myurl;
249
}
250
}
251
?>
<?php2
#============================3
# Filename: string.class.php4
# Note : 字符串管理5
# Update : 2008-5-86
# Cool!!7
#============================8
class QG_C_STRING9
{10
var $script = false;11
var $iframe = false;12
var $style = false;13

14
function __construct($script=false,$iframe=false,$style=false)15
{16
$this->script = $script;17
$this->iframe = $iframe;18
$this->style = $style;19
}20

21
#[兼容PHP4]22
function QG_C_STRING($script=false,$iframe=false,$style=false)23
{24
$this->__construct($script,$iframe,$style);25
}26

27
function __destruct()28
{29
return true;30
}31

32
#[设置状态属性]33
function set($var,$status)34
{35
$this->$var = $status;36
}37

38
function safe($msg)39
{40
if(!$msg)41
{42
return false;43
}44
if(is_array($msg))45
{46
foreach($msg AS $key=>$value)47
{48
$msg[$key] = $this->safe($value);49
}50
}51
else52
{53
$msg = trim($msg);54
$old = array("&"," ","'",'"',"<",">","\t","\r");55
$new = array("&"," ","'",""","<",">"," ","");56
$msg = str_replace($old,$new,$msg);57
$msg = str_replace(" "," ",$msg);58
$old = array("/<script(.*)<\/script>/isU","/<frame(.*)>/isU","/<\/fram(.*)>/isU","/<iframe(.*)>/isU","/<\/ifram(.*)>/isU","/<style(.*)<\/style>/isU");59
$new = array("","","","","","");60
$msg = preg_replace($old,$new,$msg);61
}62
return $msg;63
}64

65
function html($msg)66
{67
if(is_array($msg))68
{69
foreach($msg AS $key=>$value)70
{71
$msg[$key] = $this->html($value);72
}73
}74
else75
{76
$msg = trim($msg);77
$msg = stripslashes($msg);78
if(!$this->script)79
{80
$msg = preg_replace("/<script(.*)<\/script>/isU","",$msg);81
}82
if(!$this->iframe)83
{84
$msg = preg_replace("/<frame(.*)>/isU","",$msg);85
$msg = preg_replace("/<\/fram(.*)>/isU","",$msg);86
$msg = preg_replace("/<iframe(.*)>/isU","",$msg);87
$msg = preg_replace("/<\/ifram(.*)>/isU","",$msg);88
}89
if(!$this->style)90
{91
$msg = preg_replace("/<style(.*)<\/style>/isU","",$msg);92
}93
#[超链接在新窗口打开]94
$msg = preg_replace("/<a(.*)target=[ |'|\"](.*)[ |'|\"](.*)>/isU","<a\\1 \\3>",$msg);95
$msg = preg_replace("/<a(.*)>/isU","<a\\1 target='_blank'>",$msg);96
#[替换网址]97
$url = $this->get_url();98
$msg = str_replace($url,"",$msg);99
$msg = addslashes($msg);100
}101
return $msg;102
}103

104
#[截取字符长度,仅支持UTF-8]105
function cut($string,$length,$dot="…")106
{107
if(strlen($string) <= $length)108
{109
return $string;110
}111
$strcut = '';112
$n = $tn = $noc = 0;113
while ($n < strlen($string))114
{115
$t = ord($string[$n]);116
if($t == 9 || $t == 10 || (32 <= $t && $t <= 126))117
{118
$tn = 1; $n++; $noc++;119
}120
elseif(194 <= $t && $t <= 223)121
{122
$tn = 2; $n += 2; $noc += 2;123
}124
elseif(224 <= $t && $t < 239)125
{126
$tn = 3; $n += 3; $noc += 2;127
}128
elseif(240 <= $t && $t <= 247)129
{130
$tn = 4; $n += 4; $noc += 2;131
}132
elseif(248 <= $t && $t <= 251)133
{134
$tn = 5; $n += 5; $noc += 2;135
}136
elseif($t == 252 || $t == 253)137
{138
$tn = 6; $n += 6; $noc += 2;139
}140
else141
{142
$n++;143
}144

145
if ($noc >= $length)146
{147
break;148
}149
}150
if ($noc > $length)151
{152
$n -= $tn;153
}154
$strcut = substr($string, 0, $n);155
return $strcut.$dot;156
}157

158
#[编码转换,使用PHP里的iconv功能]159
function charset($msg, $s_code="UTF-8", $e_code="GBK")160
{161
if(!$msg)162
{163
return false;164
}165
if(is_array($msg))166
{167
foreach($msg AS $key=>$value)168
{169
$msg[$key] = $this->charset($value,$s_code,$e_code);170
}171
}172
else173
{174
if(function_exists("iconv"))175
{176
$msg = iconv($s_code,$e_code,$msg);177
}178
}179
return $msg;180
}181

182
function format($msg,$f=false)183
{184
$status = get_magic_quotes_gpc();185
if(!$status || $f)186
{187
if(is_array($msg))188
{189
foreach($msg AS $key=>$value)190
{191
$msg[$key] = $this->format($value,$f);192
}193
}194
else195
{196
$msg = addslashes($msg);197
}198
}199
return $msg;200
}201

202
function num_format($a,$ext=2)203
{204
if(!$a || $a == 0)205
{206
return false;207
}208
if($a <= 1024)209
{210
$a = "1 KB";211
}212
elseif($a>1024 && $a<(1024*1024))213
{214
$a = round(($a/1024),$ext)." KB";215
}216
elseif($a>=(1024*1024) && $a<(1024*1024*1024))217
{218
$a = round(($a/(1024*1024)),$ext)." MB";219
}220
else221
{222
$a = round(($a/(1024*1024*1024)),$ext)." GB";223
}224
return $a;225
}226

227
function get_url()228
{229
$myurl = "http://".str_replace("http://","",$_SERVER["SERVER_NAME"]);230
$docu = $_SERVER["PHP_SELF"];231
$array = explode("/",$docu);232
$count = count($array);233
if($count>1)234
{235
foreach($array AS $key=>$value)236
{237
$value = trim($value);238
if($value)239
{240
if(($key+1) < $count)241
{242
$myurl .= "/".$value;243
}244
}245
}246
}247
$myurl .= "/";248
return $myurl;249
}250
}251
?>
浙公网安备 33010602011771号