在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('/<(hr|img|input|area|isindex)( [^><\[\]]*)>/i','[\1\2]',$text);//過濾多餘html$text = preg_replace('/<\/?(html|head|meta|link|base|body|title|style|script|form|iframe|frame|frameset)[^><]*>/i','',$text);//過濾on事件lang jswhile(preg_match('/(<[^><]+)( lang|onfinish|onmouse|onexit|onerror|onclick|onkey|onload|onchange|onfocus|onblur)[^><]+/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);}//過濾合法的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('<','<',$text);$text = str_replace('>','>',$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('/<\/?(html|head|meta|link|base|body|title|style|script|form|iframe|frame|frameset)[^><]*>/i','',$text);//過濾on事件lang jswhile(preg_match('/(<[^><]+)(lang|onfinish|onmouse|onexit|onerror|onclick|onkey|onload|onchange|onfocus|onblur)[^><]+/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);}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 ){ echo intval($dTime/60)."分鐘前";}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. 純文字輸出,...