Heim > Artikel > Backend-Entwicklung > 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>/isU " , " /<frame(.*)>/isU " , " /<\/fram(.*)>/isU " , " /<iframe(.*)>/isU " , " /<\/ifram(.*)>/isU " , " /<style(.*)<\/style>/isU " ); <br /> 59 $new = array ( "" , "" , "" , "" , "" , "" ); <br /> 60 $msg = preg_replace ( $old , $new , $msg ); <br /> 61 } <br /> 62 return $msg ; <br /> 63 } <br /> 64 <br /> 65 function html( $msg ) <br /> 66 { <br /> 67 if ( is_array ( $msg )) <br /> 68 { <br /> 69 foreach ( $msg AS $key => $value ) <br /> 70 { <br /> 71 $msg [ $key ] = $this -> html( $value ); <br /> 72 } <br /> 73 } <br /> 74 else <br /> 75 { <br /> 76 $msg = trim ( $msg ); <br /> 77 $msg = stripslashes ( $msg ); <br /> 78 if ( ! $this -> script) <br /> 79 { <br /> 80 $msg = preg_replace ( " /<script(.*)<\/script>/isU " , "" , $msg ); <br /> 81 } <br /> 82 if ( ! $this -> iframe) <br /> 83 { <br /> 84 $msg = preg_replace ( " /<frame(.*)>/isU " , "" , $msg ); <br /> 85 $msg = preg_replace ( " /<\/fram(.*)>/isU " , "" , $msg ); <br /> 86 $msg = preg_replace ( " /<iframe(.*)>/isU " , "" , $msg ); <br /> 87 $msg = preg_replace ( " /<\/ifram(.*)>/isU " , "" , $msg ); <br /> 88 } <br /> 89 if ( ! $this -> style) <br /> 90 { <br /> 91 $msg = preg_replace ( " /<style(.*)<\/style>/isU " , "" , $msg ); <br /> 92 } <br /> 93 # [超链接在新窗口打开] <br /> 94 $msg = preg_replace ( " /<a(.*)target=[ |'|\ " ]( .* )[ | ' |\"](.*)>/isU","<a\\1 \\3>",$msg); <br /> 95 $msg = preg_replace("/<a(.*)>/isU","<a\\1 target= ' _blank ' >",$msg); <br /> 96 #[替换网址] <br /> 97 $url = $this->get_url(); <br /> 98 $msg = str_replace($url,"",$msg); <br /> 99 $msg = addslashes($msg); <br /> 100 } <br /> 101 return $msg; <br /> 102 } <br /> 103 <br /> 104 #[截取字符长度,仅支持UTF-8] <br /> 105 function cut($string,$length,$dot="…") <br /> 106 { <br /> 107 if(strlen($string) <= $length) <br /> 108 { <br /> 109 return $string; <br /> 110 } <br /> 111 $strcut = '' ; <br /> 112 $n = $tn = $noc = 0; <br /> 113 while ($n < strlen($string)) <br /> 114 { <br /> 115 $t = ord($string[$n]); <br /> 116 if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) <br /> 117 { <br /> 118 $tn = 1; $n++; $noc++; <br /> 119 } <br /> 120 elseif(194 <= $t && $t <= 223) <br /> 121 { <br /> 122 $tn = 2; $n += 2; $noc += 2; <br /> 123 } <br /> 124 elseif(224 <= $t && $t < 239) <br /> 125 { <br /> 126 $tn = 3; $n += 3; $noc += 2; <br /> 127 } <br /> 128 elseif(240 <= $t && $t <= 247) <br /> 129 { <br /> 130 $tn = 4; $n += 4; $noc += 2; <br /> 131 } <br /> 132 elseif(248 <= $t && $t <= 251) <br /> 133 { <br /> 134 $tn = 5; $n += 5; $noc += 2; <br /> 135 } <br /> 136 elseif($t == 252 || $t == 253) <br /> 137 { <br /> 138 $tn = 6; $n += 6; $noc += 2; <br /> 139 } <br /> 140 else <br /> 141 { <br /> 142 $n++; <br /> 143 } <br /> 144 <br /> 145 if ($noc >= $length) <br /> 146 { <br /> 147 break; <br /> 148 } <br /> 149 } <br /> 150 if ($noc > $length) <br /> 151 { <br /> 152 $n -= $tn; <br /> 153 } <br /> 154 $strcut = substr($string, 0, $n); <br /> 155 return $strcut.$dot; <br /> 156 } <br /> 157 <br /> 158 #[编码转换,使用PHP里的iconv功能] <br /> 159 function charset($msg, $s_code="UTF-8", $e_code="GBK") <br /> 160 { <br /> 161 if(!$msg) <br /> 162 { <br /> 163 return false; <br /> 164 } <br /> 165 if(is_array($msg)) <br /> 166 { <br /> 167 foreach($msg AS $key=>$value) <br /> 168 { <br /> 169 $msg[$key] = $this->charset($value,$s_code,$e_code); <br /> 170 } <br /> 171 } <br /> 172 else <br /> 173 { <br /> 174 if(function_exists("iconv")) <br /> 175 { <br /> 176 $msg = iconv($s_code,$e_code,$msg); <br /> 177 } <br /> 178 } <br /> 179 return $msg; <br /> 180 } <br /> 181 <br /> 182 function format($msg,$f=false) <br /> 183 { <br /> 184 $status = get_magic_quotes_gpc(); <br /> 185 if(!$status || $f) <br /> 186 { <br /> 187 if(is_array($msg)) <br /> 188 { <br /> 189 foreach($msg AS $key=>$value) <br /> 190 { <br /> 191 $msg[$key] = $this->format($value,$f); <br /> 192 } <br /> 193 } <br /> 194 else <br /> 195 { <br /> 196 $msg = addslashes($msg); <br /> 197 } <br /> 198 } <br /> 199 return $msg; <br /> 200 } <br /> 201 <br /> 202 function num_format($a,$ext=2) <br /> 203 { <br /> 204 if(!$a || $a == 0) <br /> 205 { <br /> 206 return false; <br /> 207 } <br /> 208 if($a <= 1024) <br /> 209 { <br /> 210 $a = "1 KB"; <br /> 211 } <br /> 212 elseif($a>1024 && $a<(1024*1024)) <br /> 213 { <br /> 214 $a = round(($a/1024),$ext)." KB"; <br /> 215 } <br /> 216 elseif($a>=(1024*1024) && $a<(1024*1024*1024)) <br /> 217 { <br /> 218 $a = round(($a/(1024*1024)),$ext)." MB"; <br /> 219 } <br /> 220 else <br /> 221 { <br /> 222 $a = round(($a/(1024*1024*1024)),$ext)." GB"; <br /> 223 } <br /> 224 return $a; <br /> 225 } <br /> 226 <br /> 227 function get_url() <br /> 228 { <br /> 229 $myurl = "http://".str_replace("http://","",$_SERVER["SERVER_NAME"]); <br /> 230 $docu = $_SERVER["PHP_SELF"]; <br /> 231 $array = explode("/",$docu); <br /> 232 $count = count($array); <br /> 233 if($count>1) <br /> 234 { <br /> 235 foreach($array AS $key=>$value) <br /> 236 { <br /> 237 $value = trim($value); <br /> 238 if($value) <br /> 239 { <br /> 240 if(($key+1) < $count) <br /> 241 { <br /> 242 $myurl .= "/".$value; <br /> 243 } <br /> 244 } <br /> 245 } <br /> 246 } <br /> 247 $myurl .= "/"; <br /> 248 return $myurl; <br /> 249 } <br /> 250 } <br /> 251 ?> </script>