收藏一些正常化輸入輸出的PHP函數_PHP教程

來源:互聯網
上載者:User
在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. 純文字輸出,...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.