收集一些有用的php函数---from cmstop
时间:2010-11-04 来源:wingle
1 function js_format($string)
2 {
3 return addslashes(str_replace(array("\r", "\n"), array('', ''), $string));
4 }
2,text_format
1 function text_format($string)
2 {
3 return nl2br(str_replace(' ', ' ', htmlspecialchars($string)));
4 }
3,file_ext
1 function fileext($filename)
2 {
3 return pathinfo($filename, PATHINFO_EXTENSION);
4 }
4,str_cut
1 function str_cutword($string,$length=80,$charset="utf-8",$etc='...')
2 {
3 $start = 0;
4 if (function_exists ("mb_substr" ))
5 return mb_substr ( $string, $start, $length, $charset );
6 $re ['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
7 $re ['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
8 $re ['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
9 $re ['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
10 preg_match_all ( $re [$charset], $string, $match );
11 $slice = join ( "", array_slice ( $match [0], $start, $length ) );
12
13 return $slice.$etc;
14 }
5,str_cut
1 function str_cut($string, $length, $dot = '...', $charset = 'utf-8')
2 {
3 $strlen = strlen($string);
4 if($strlen <= $length) return $string;
5 $specialchars = array('&', '"', ''', '<', '>');
6 $entities = array('&', '"', "'", '<', '>');
7 $string = str_replace($specialchars, $entities, $string);
8 $strcut = '';
9 if(strtolower($charset) == 'utf-8')
10 {
11 $n = $tn = $noc = 0;
12 while($n < $strlen)
13 {
14 $t = ord($string[$n]);
15 if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
16 $tn = 1; $n++; $noc++;
17 } elseif(194 <= $t && $t <= 223) {
18 $tn = 2; $n += 2; $noc += 2;
19 } elseif(224 <= $t && $t < 239) {
20 $tn = 3; $n += 3; $noc += 2;
21 } elseif(240 <= $t && $t <= 247) {
22 $tn = 4; $n += 4; $noc += 2;
23 } elseif(248 <= $t && $t <= 251) {
24 $tn = 5; $n += 5; $noc += 2;
25 } elseif($t == 252 || $t == 253) {
26 $tn = 6; $n += 6; $noc += 2;
27 } else {
28 $n++;
29 }
30 if($noc >= $length) break;
31 }
32 if($noc > $length) $n -= $tn;
33 $strcut = substr($string, 0, $n);
34 }
35 else
36 {
37 $dotlen = strlen($dot);
38 $maxi = $length - $dotlen - 1;
39 for($i = 0; $i < $maxi; $i++)
40 {
41 $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
42 }
43 }
44 return $strcut.$dot;
45 }
相关阅读 更多 +