php br2nl,收藏一些规范化输入输出的PHP函数
在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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git常用命令及冲突解决
- 下一篇: inurllay old.php id,