在php网站开发过程中会遇到很多需要转义的地方,下面推荐几个很好的函数,可以很好地增强网站的输入输出规范化问题。
1. 纯文本输出,适合input
function t($text){ $text = h($text); $text = strip_tags($text); return $text;}
2. 多行纯文本 适合textarea
function text($text){ return trim(nl2br(str_replace(' ', ' ', htmlspecialchars($text))));}
3. 将html换行变成回车
function br2nl($text){ return trim(preg_replace('//i', '', $text));}
4. 输出安全的html
function h($text){ $text = trim($text); $text = stripslashes($text); //完全过滤注释 $text = preg_replace('//','',$text); //完全过滤动态代码 $text = preg_replace('//','',$text); //完全过滤js $text = preg_replace('//','',$text); $text = str_replace('[','[',$text); $text = str_replace(']',']',$text); $text = str_replace('|','|',$text); //过滤换行符 $text = preg_replace('/\r?\n/','',$text); //br $text = preg_replace('//i','[br]',$text); $text = preg_replace('/(\[br\]\s*){10,}/i','[br]',$text); //hr img area input $text = preg_replace('//i','[\1\2]',$text); //过滤多余html $text = preg_replace('//i','',$text); //过滤on事件lang js while(preg_match('/(<]+/i',$text,$mat)){ $text=str_replace($mat[0],$mat[1],$text); } while(preg_match('/(',']',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('',$text); $text = str_replace('','',$text); //反转换 $text = str_replace('[','',$text); $text = str_replace('|','',$text); //过滤多余空格 $text = str_replace(' ',' ',$text); return $text;}
5. 过滤脚本代码
function cleanjs($text){ $text = trim($text); $text = stripslashes($text); //完全过滤动态代码 $text = preg_replace('//','',$text); //完全过滤js $text = preg_replace('//','',$text); //过滤多余html $text = preg_replace('//i','',$text); //过滤on事件lang js while(preg_match('/(<]+/i',$text,$mat)){ $text=str_replace($mat[0],$mat[1],$text); } while(preg_match('/(<]*)/i',$text,$mat)){ $text=str_replace($mat[0],$mat[1].$mat[3],$text); } return $text;}
6. 在编辑器中显示纯文本
function et($text){ return trim(br2nl(str_replace(' ', ' ', $text )));}
7. 在html编辑器中显示html
function eh($text){ return trim(str_replace('','', $text));}
8. 判断时间距离
function friendlydate($stime,$type = 'normal',$alt = 'false') { //stime=源时间,ctime=当前时间,dtime=时间差 $ctime = time(); $dtime = $ctime - $stime; $dday = intval(date(ymd,$ctime)) - intval(date(ymd,$stime)); $dyear = intval(date(y,$ctime)) - intval(date(y,$stime)); //normal:n秒前,n分钟前,n小时前,日期 if($type=='normal'){ if( $dtime < 60 ) { echo $dtime.秒前; } elseif( $dtime = 3600 && $dday == 0 ) { echo intval($dtime/3600).小时前; } elseif($dyear==0) { echo date(m-d ,h:i,$stime); } else { echo date(y-m-d ,h:i,$stime); } //full: y-m-d , h:i:s } elseif($type=='full') { echo date(y-m-d , h:i:s,$stime); }}
http://www.bkjia.com/phpjc/752446.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/752446.htmltecharticle在php网站开发过程中会遇到很多需要转义的地方,下面推荐几个很好的函数,可以很好地增强网站的输入输出规范化问题。 1. 纯文本输出,...
