PHP character class

Source: Internet
Author: User
1 2 # ============================
3 # Filename:string.class.php
4 # Note: string management
5 # Update:2008-5-8
6 # cool!!
7 # ============================
8 Class Qg_c_string
9 {
var $script = false;
One-to-one var $iframe = false;
var $style = false;
13
function __construct ($script = False, $iframe = False, $style = False)
15 {
$this, script = $script;
$this, iframe = $iframe;
$this style = $style;
19}
20
21 # [Compatible with PHP4]
function qg_c_string ($script = False, $iframe = False, $style = False)
23 {
__construct, $this ($script, $iframe, $style);
25}
26
function __destruct ()
28 {
return true;
30}
31
32 # [Set state Properties]
function set ($var, $status)
34 {
$this $var = $status;
36}
37
function Safe ($msg)
39 {
if (! $msg)
41 {
return false;
43}
if (Is_array ($msg))
45 {
($msg as $key = $value)
47 {
$msg [$key] = $this, safe ($value);
49}
50}
Or else
52 {
$msg = Trim ($msg);
$old = Array ("&", "" "," "", "" "," "<", ">", "\ T", "\ R");
$new = Array ("&", "", "'", "" "," < "," > "," "," ");
$msg = Str_replace ($old, $new, $msg);
$msg = Str_replace ("", "", $msg);
$old = Array ("/ /isu ","/ /isu ","/<\/fram (. *) >/isu ","/ /isu ","/<\/ifram (. *) >/isu ","/ /isu ");
$new = Array ("", "", "", "", "", "");
$msg = Preg_replace ($old, $new, $msg);
61}
return $msg;
63}
64
function html ($msg)
66 {
if (Is_array ($msg))
68 {
foreach ($msg as $key = $value)
70 {
$msg [$key] = $this, html ($value);
72}
73}
"Else"
75 {
$msg = Trim ($msg);
$msg = Stripslashes ($msg);
if (! $this, script)
79 {
$msg = Preg_replace ("/ /isu "," ", $msg);
81}
if (! $this, IFRAME)
83 {
$msg = Preg_replace ("/ /isu "," ", $msg);
$msg = Preg_replace ("/<\/fram (. *) >/isu", "", $msg);
$msg = Preg_replace ("/ /isu "," ", $msg);
$msg = Preg_replace ("/<\/ifram (. *) >/isu", "", $msg);
88}
if (! $this-style)
90 {
$msg = Preg_replace ("/ /isu "," ", $msg);
92}
93 # [Hyperlink opens in new window]
94 $msg = Preg_replace ("//isu", "", $msg);
$msg = Preg_replace ("//isu", "", $msg);
96 #[Replacement URL]
$url = $this->get_url ();
98 $msg = Str_replace ($url, "", $msg);
$msg = Addslashes ($msg);
100}
101 return $msg;
102}
103
104 #[Intercept character length, only supports UTF-8]
The function cut ($string, $length, $dot = "...")
106 {
107 if (strlen ($string) <= $length)
108 {
109 return $string;
110}
111 $strcut = ";
$n = $tn = $noc = 0;
113 while ($n < strlen ($string))
114 {
$t = Ord ($string [$n]);
if ($t = = 9 | | $t = = 10 | | (<= $t && $t <= 126))
117 {
118 $tn = 1; $n + +; $noc + +;
119}
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}
ElseIf (<= $t && $t <= 247)
129 {
$tn = 4; $n + = 4; $noc + = 2;
131}
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}
$ else
141 {
142 $n + +;
143}
144
145 if ($noc >= $length)
146 {
147 break;
148}
149}
if ($noc > $length)
151 {
$n-= $tn;
153}
154 $strcut = substr ($string, 0, $n);
155 return $strcut. $dot;
156}
157
158 #[encoding conversion, using the Iconv function in PHP]
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 {
$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}
$ 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"]);
$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 {
if (($key + 1) < $count)
241 {
242 $myurl. = "/". $value;
243}
244}
245}
246}
247 $myurl. = "/";
248 return $myurl;
249}
250}
251?>
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.