検索
ホームページphp教程php手册收藏一些规范化输入输出的PHP函数

在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('/<br\\s*\/?'.'>/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('/<script?.*\/script>/','',$text);
	$text = str_replace('[','[',$text);
	$text = str_replace(']',']',$text);
	$text = str_replace('|','|',$text);
	//过滤换行符
	$text = preg_replace('/\r?\n/','',$text);
	//br
	$text = preg_replace('/<br(\s\/)?'.'>/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标签
	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('/<script?.*\/script>/','',$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);
	}
	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);
	}
}
声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

EditPlus 中国語クラック版

EditPlus 中国語クラック版

サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

SublimeText3 英語版

SublimeText3 英語版

推奨: Win バージョン、コードプロンプトをサポート!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。