日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

php br2nl,收藏一些规范化输入输出的PHP函数

發(fā)布時(shí)間:2023/12/19 php 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php br2nl,收藏一些规范化输入输出的PHP函数 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在PHP網(wǎng)站開發(fā)過程中會遇到很多需要轉(zhuǎn)義的地方,下面推薦幾個(gè)很好的函數(shù),可以很好地增強(qiáng)網(wǎng)站的輸入輸出規(guī)范化問題。

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);

//完全過濾動態(tài)代碼

$text = preg_replace('//','',$text);

//完全過濾js

$text = preg_replace('/

$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 js

while(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標(biāo)簽

while(preg_match('/<([a-z]+)[^><\[\]]*>[^><]*<\/\1>/i',$text,$mat)){

$text=str_replace($mat[0],str_replace('>',']',str_replace('<','[',$mat[0])),$text);

}

//轉(zhuǎn)換引號

while(preg_match('/(\[[^\[\]]*=\s*)(\"|\')([^\2=\[\]]+)\2([^\[\]]*\])/i',$text,$mat)){

$text=str_replace($mat[0],$mat[1].'|'.$mat[3].'|'.$mat[4],$text);

}

//過濾錯(cuò)誤的單個(gè)引號

while(preg_match('/\[[^\[\]]*(\"|\')[^\[\]]*\]/i',$text,$mat)){

$text=str_replace($mat[0],str_replace($mat[1],'',$mat[0]),$text);

}

//轉(zhuǎn)換其它所有不合法的 < >

$text = str_replace('<','<',$text);

$text = str_replace('>','>',$text);

$text = str_replace('"','"',$text);

//反轉(zhuǎn)換

$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);

//完全過濾動態(tài)代碼

$text = preg_replace('/<\?|\?'.'>/','',$text);

//完全過濾js

$text = preg_replace('/

//過濾多余html

$text = preg_replace('/<\/?(html|head|meta|link|base|body|title|style|script|form|iframe|frame|frameset)[^><]*>/i','',$text);

//過濾on事件lang js

while(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. 判斷時(shí)間距離

function friendlyDate($sTime,$type = 'normal',$alt = 'false') {

//sTime=源時(shí)間,cTime=當(dāng)前時(shí)間,dTime=時(shí)間差

$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小時(shí)前,日期

if($type=='normal'){

if( $dTime < 60 )

{

echo $dTime."秒前";

}

elseif( $dTime < 3600 )

{

echo intval($dTime/60)."分鐘前";

}

elseif( $dTime >= 3600 && $dDay == 0 )

{

echo intval($dTime/3600)."小時(shí)前";

}

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);

}

}

總結(jié)

以上是生活随笔為你收集整理的php br2nl,收藏一些规范化输入输出的PHP函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。