几个值得放在common.php中的函数

安全HTML输出与字符串截取

Code
//纯文本输出
function t($text){
return hsc(trim($text));
}
//改进的htmlspecialchars()
function hsc($text)
{
return preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1',
str_replace(array('&', '"', '<', '>',' '), array('&amp;', '&quot;', '&lt;', '&gt;', '&nbsp;'), $text));
}
//输出安全的html
function h($text, $tags = null){
$text = trim($text);
//完全过滤注释
$text = preg_replace('/<!--?.*-->/','',$text);
//完全过滤动态代码
$text = preg_replace('/<\?|\?'.'>/','',$text);
//完全过滤js
$text = preg_replace('/<script?.*\/script>/','',$text);

$text = str_replace('[','[',$text);
$text = str_replace(']',']',$text);
$text = str_replace('|','|',$text);
//过滤换行符
$text = preg_replace('/\r?\n/','',$text);
//br
$text = preg_replace('/<br(\s\/)?'.'>/i','[br]',$text);
$text = preg_replace('/(\[br\]\s*){10,}/i','[br]',$text);
//过滤危险的属性,如:过滤on事件lang js
while(preg_match('/(<[^><]+)( lang|on|action|background|codebase|dynsrc|lowsrc)[^><]+/i',$text,$mat)){
$text=str_replace($mat[0],$mat[1],$text);
}
while(preg_match('/(<[^><]+)(window\.|javascript:|js:|about:|file:|document\.|vbs:|cookie)([^><]*)/i',$text,$mat)){
$text=str_replace($mat[0],$mat[1].$mat[3],$text);
}
if(empty($tags)) {
$tags = 'table|td|th|tr|i|b|u|strong|img|p|br|div|strong|em|ul|ol|li|dl|dd|dt|a';
}
//允许的HTML标签
$text = preg_replace('/<('.$tags.')( [^><\[\]]*)>/i','[\1\2]',$text);
//过滤多余html
$text = preg_replace('/<\/?(html|head|meta|link|base|basefont|body|bgsound|title|style|script|form|iframe|frame|frameset|applet|id|ilayer|layer|name|script|style|xml)[^><]*>/i','',$text);
//过滤合法的html标签
while(preg_match('/<([a-z]+)[^><\[\]]*>[^><]*<\/\1>/i',$text,$mat)){
$text=str_replace($mat[0],str_replace('>',']',str_replace('<','[',$mat[0])),$text);
}
//转换引号
while(preg_match('/(\[[^\[\]]*=\s*)(\"|\')([^\2=\[\]]+)\2([^\[\]]*\])/i',$text,$mat)){
$text=str_replace($mat[0],$mat[1].
'|'.$mat[3].'|'.$mat[4],$text);
}
//过滤错误的单个引号
while(preg_match(
'/\[[^\[\]]*(\"|\')[^\[\]]*\]/i',$text,$mat)){
$text=str_replace($mat[0],str_replace($mat[1],'',$mat[0]),$text);
}
//转换其它所有不合法的 < >
$text = str_replace('<','&lt;',$text);
$text = str_replace('>','&gt;',$text);
$text = str_replace('"','&quot;',$text);
//反转换
$text = str_replace(
'[','<',$text);
$text = str_replace(
']','>',$text);
$text = str_replace(
'|','"',$text);
//过滤多余空格
$text = str_replace(' ',' ',$text);
return
$text;
}

找了很久没有找到合适的字符串截取函数(主要问题是有乱码问题)

这个很好用

 

ContractedBlock.gifExpandedBlockStart.gifCode
function Cut($Str, $Length// $Str为截取字符串,$Length为需要截取的长度
{
    
global $s;
    
$i = 0;
    
$l = 0;
    
$ll = strlen($Str);
    
$s = $Str;
    
$f = true;

    
while ($i <= $ll) {
        
if (ord($Str{$i}) < 0x80) {
            
$l++;
            
$i++;
        } 
else if (ord($Str{$i}) < 0xe0) {
            
$l++;
            
$i += 2;
        } 
else if (ord($Str{$i}) < 0xf0) {
            
$l += 2;
            
$i += 3;
        } 
else if (ord($Str{$i}) < 0xf8) {
            
$l += 1;
            
$i += 4;
        } 
else if (ord($Str{$i}) < 0xfc) {
            
$l += 1;
            
$i += 5;
        } 
else if (ord($Str{$i}) < 0xfe) {
            
$l += 1;
            
$i += 6;
        }

        
if (($l >= $Length - 1&& $f) {
            
$s = substr($Str, 0, $i);
            
$f = false;
        }

        
if (($l > $Length&& ($i < $ll)) {
            
$s = $s . '';
            
break//如果进行了截取,字符串末尾加省略符号“
        }
    }
    
return $s;
}

转载于:https://www.cnblogs.com/zq535228/archive/2008/12/01/1345187.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值