收集一些有用的php函数---from cmstop
2010-11-04 17:03
260 查看
1,格式化输出字符串到js
2,text_format
3,file_ext
4,str_cut
5,str_cut
function js_format($string) { return addslashes(str_replace(array("\r", "\n"), array('', ''), $string)); }
2,text_format
function text_format($string) { return nl2br(str_replace(' ', ' ', htmlspecialchars($string))); }
3,file_ext
function fileext($filename) { return pathinfo($filename, PATHINFO_EXTENSION); }
4,str_cut
function str_cutword($string,$length=80,$charset="utf-8",$etc='...') { $start = 0; if (function_exists ("mb_substr" )) return mb_substr ( $string, $start, $length, $charset ); $re ['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"; $re ['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"; $re ['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"; $re ['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"; preg_match_all ( $re [$charset], $string, $match ); $slice = join ( "", array_slice ( $match [0], $start, $length ) ); return $slice.$etc; }
5,str_cut
function str_cut($string, $length, $dot = '...', $charset = 'utf-8') { $strlen = strlen($string); if($strlen <= $length) return $string; $specialchars = array('&', '"', ''', '<', '>'); $entities = array('&', '"', "'", '<', '>'); $string = str_replace($specialchars, $entities, $string); $strcut = ''; if(strtolower($charset) == 'utf-8') { $n = $tn = $noc = 0; while($n < $strlen) { $t = ord($string[$n]); if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) { $tn = 1; $n++; $noc++; } elseif(194 <= $t && $t <= 223) { $tn = 2; $n += 2; $noc += 2; } elseif(224 <= $t && $t < 239) { $tn = 3; $n += 3; $noc += 2; } elseif(240 <= $t && $t <= 247) { $tn = 4; $n += 4; $noc += 2; } elseif(248 <= $t && $t <= 251) { $tn = 5; $n += 5; $noc += 2; } elseif($t == 252 || $t == 253) { $tn = 6; $n += 6; $noc += 2; } else { $n++; } if($noc >= $length) break; } if($noc > $length) $n -= $tn; $strcut = substr($string, 0, $n); } else { $dotlen = strlen($dot); $maxi = $length - $dotlen - 1; for($i = 0; $i < $maxi; $i++) { $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i]; } } return $strcut.$dot; }
相关文章推荐
- 收集的一些有用的JAVASCript
- VB.NET中一些有用但很少用的方法(收集)
- JS一些有用的函数收集
- 收集一些有用的 Blog 地址
- 收集的一些有用的好玩的控件!
- 网络上收集的一些有用的网址
- IT路上收集的一些有用的话
- 收集整理的非常有用的PHP函数
- 收集整理的非常有用的PHP函数
- TOP Api一些有用的链接
- mac下的一些有用的命令收集,希望有时间不断完善
- 收集整理的非常有用的PHP函数
- 收集了一些我认为有用的Flex 组件
- 在学习C#过程中的一些总结和收集的一些材料望对初学者有用
- 收集的一些可能有用的东西
- 一些有用网址收集
- 收集整理的非常有用的PHP函数
- 收集整理的非常有用的PHP函数
- 最近收集的一些有用的网站
- 收集一些讲的比较好的有用的文章