PHP character class

Source: Internet
Author: User
PHP character class 1 2 #====================================
3 # Filename: string. class. php
4 # Note: string management
5 # Update:
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 # [compatible with 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 # [set status attributes]
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 ("/ /IsU ","/ /IsU ","/<\/fram (. *)>/isU ","/ /IsU ","/<\/ifram (. *)>/isU ","/ /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 ("/ /IsU "," ", $ msg );
81}
82 if (! $ This-> iframe)
83 {
84 $ msg = preg_replace ("/ /IsU "," ", $ msg );
85 $ msg = preg_replace ("/<\/fram (. *)>/isU", "", $ msg );
86 $ msg = preg_replace ("/ /IsU "," ", $ msg );
87 $ msg = preg_replace ("/<\/ifram (. *)>/isU", "", $ msg );
88}
89 if (! $ This-> style)
90 {
91 $ msg = preg_replace ("/ /IsU "," ", $ msg );
92}
93 # [hyperlink opened in new window]
94 $ msg = preg_replace ("// isU", "", $ msg );
95 $ msg = preg_replace ("// isU", "", $ msg );
96 # [replacing the website]
97 $ url = $ this-> get_url ();
98 $ msg = str_replace ($ url, "", $ msg );
99 $ msg = addslashes ($ msg );
100}
101 return $ msg;
102}
103
104 # [truncated character length, only UTF-8 supported]
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 # [encoding and 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 {
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 $;
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?>

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.