search
HomeBackend DevelopmentPHP TutorialCollect some PHP functions that normalize input and output_PHP tutorial

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/752446.htmlTechArticle在PHP网站开发过程中会遇到很多需要转义的地方,下面推荐几个很好的函数,可以很好地增强网站的输入输出规范化问题。 1. 纯文本输出,...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何通过PHP代码规范规范性能优化如何通过PHP代码规范规范性能优化Aug 11, 2023 pm 03:51 PM

如何通过PHP代码规范规范性能优化引言:随着互联网的迅速发展,越来越多的网站和应用程序基于PHP语言开发。在PHP开发过程中,性能优化是一个至关重要的方面。一个高性能的PHP代码可以显著提高网站的响应速度和用户体验。本文将探讨如何通过PHP代码规范来规范性能优化,并提供一些实际的代码示例供参考。一、减少数据库查询在开发过程中,频繁的数据库查询是一个常见的性能

如何解决:Java输入输出错误:文件读取错误如何解决:Java输入输出错误:文件读取错误Aug 17, 2023 pm 03:21 PM

如何解决:Java输入输出错误:文件读取错误在使用Java进行文件读取操作时,有时会遇到文件读取错误的情况。这种错误可能是由于文件路径不正确、文件不存在、权限不足等原因导致的。本文将介绍如何解决Java输入输出错误中的文件读取错误问题,并提供代码示例来说明解决方法。确认文件路径是否正确在Java中读取文件时,首先要确保文件路径的正确性。文件路径可以是绝对路径

如何在GitLab中进行代码样式检查和规范化如何在GitLab中进行代码样式检查和规范化Oct 25, 2023 am 08:38 AM

如何在GitLab中进行代码样式检查和规范化代码的风格和规范对于团队项目的开发非常重要。统一的代码规范可以提高代码的可读性、可维护性和可扩展性,减少潜在的Bug和错误。而在团队开发中,通过使用版本控制工具如GitLab来管理项目代码,可以方便地进行代码样式检查和规范化。本文将介绍如何在GitLab中进行代码样式检查和规范化,并提供具体的代码示例。配置代码检查

PyCharm代码规范化和格式化的实用技巧PyCharm代码规范化和格式化的实用技巧Feb 23, 2024 pm 02:54 PM

PyCharm是Python开发者常用的集成开发环境(IDE),它提供了丰富的功能和工具来提高代码的质量和效率。其中,代码规范化和格式化是编写高质量代码的重要步骤之一。本文将介绍PyCharm中一些实用的技巧和功能,帮助开发者规范化和格式化Python代码。自动PEP8规范检查PEP8是Python官方提供的代码规范指南,包含了一系列关于代码风格、命名规范等

PHP开发中如何优化表单验证和数据输入验证PHP开发中如何优化表单验证和数据输入验证Oct 08, 2023 am 09:17 AM

PHP开发中如何优化表单验证和数据输入验证【引言】在Web开发中,表单验证和数据输入验证是非常重要的步骤,它们能够保证用户输入的数据的合法性和安全性。不仅可以避免用户输入错误或恶意输入,还可以防止数据库或应用程序遭受SQL注入等攻击。本文将介绍如何优化PHP开发中的表单验证和数据输入验证,并提供具体的代码示例。【1.服务器端验证】第一步是在服务器端对用户提

C语言中scanf函数的使用方法详解C语言中scanf函数的使用方法详解Feb 21, 2024 pm 06:30 PM

C语言中scanf函数的使用方法详解及代码示例C语言是一门广泛应用于各种软件开发的编程语言,其中的输入输出函数在编写程序时起着非常重要的作用。其中,scanf函数是C语言中用于读取标准输入的函数之一,它可以根据特定的格式从键盘读取数据并将其存储到指定的变量中。本文将详细介绍scanf函数的使用方法,并提供一些实例代码进行示例。首先,我们来看一下scanf函数

c++输入输出语句有哪些c++输入输出语句有哪些Feb 01, 2023 pm 05:31 PM

c++输入输出语句有:1、scanf()语句,用于读取从键盘输入的数据;2、printf()语句,用于向标准输出设备输出指定的格式信息;3、cout语句,表示标准输出,使用cout进行输出时需要紧跟“<<”运算符;4、cin语句,表示标准输入,使用cin进行输入时需要紧跟“>>”运算符。

Vue项目中设计RESTful API的规范化实践Vue项目中设计RESTful API的规范化实践Jun 09, 2023 pm 04:11 PM

随着前端框架的不断发展和普及,单页应用程序成为Web应用程序中的主流之一。其中,Vue.js因其简单易学和高效开发而备受开发者的喜爱。然而,与传统Web应用程序不同,单页应用程序需要与后端API进行交互来获取数据和执行各种操作。为了使前端和后端之间的交互更加方便、高效和可维护,在Vue项目中设计RESTfulAPI的规范化实践非常重要。REST(Repre

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)