在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 = 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标签

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('/

//过滤多余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);

}

}

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

  1. ACM必学 C/C++文件输入输出利器—freopen函数

    相信很多人都用过freopen,但是是不是有时候在网上提交代码的时候需要把这句话注释掉,我一开始也是这样的,但还是觉得很麻烦,有时忘记注释,会多错一次,让人很不爽. 网上的资料很乱,什么代码都有,也不 ...

  2. 收藏 | 提高数据处理效率的 Pandas 函数方法

    作者:俊欣 来源:关于数据分析与可视化 前言 大家好,这里是俊欣,今天和大家来分享几个Pandas方法可以有效地帮助我们在数据分析与数据清洗过程当中提高效率,加快工作的进程,希望大家看了之后会有收获. ...

  3. word vba高效排版范例应用_收藏 | 原来word里面也有函数!

    Word里面的rand"函数" 可以在文档中快速生成文本,小伙伴在Word中练习排版时,可以用虚拟文本来练习排版.在任意位置输入=rand()必须为英文半角,按[Enter]键,就 ...

  4. Python练习 | Python3中的格式化输入输出之print函数

    博主github:https://github.com/MichaelBeechan 博主CSDN:https://blog.csdn.net/u011344545 Python2版本和Python3 ...

  5. 获取某个输入框的字符长度_收藏,最全的字符串函数方法,总有你用到的~

    点击上方"执行上下文",选择"置顶公众号" 关键时刻,第一时间送达! 在日常开发中,我们难免要和字符串打交道,各种各样的功能都可能用到上面的字符串函数,本文参考 ...

  6. 字符串的输入输出及字符串函数

    1.输入: (1)scanf() %s : 一串整个读入 %c : 一个一个读入 char a[10];scanf("%s",a); 格式:不加 & ,以回车或空格作为输入 ...

  7. 字符及字符串(数字串)输入输出字符串处理函数

    字符输入和输出函数 #include <stdio.h> int main() {int c;c = getchar();putchar(c);putchar('\n');return 0 ...

  8. 【C 语言】文件操作 ( 配置文件读写 | 框架搭建 | 头文件定义 | 头文件导入限制 | 兼容 C++ 语言 | 函数形参输入输出属性注释)

    文章目录 一.头文件定义 1.头文件导入限制 2.兼容 C++ 语言 3.函数形参输入输出属性 4.代码示例 一.头文件定义 1.头文件导入限制 头文件导入限制 : 防止头文件多次导入 , 将头文件的 ...

  9. 区分多种类型的输入输出

    区分多种类型的输入输出 文章目录 区分多种类型的输入输出 **1.printf函数** **2.sprintf函数** **3.fprintf函数** **4.snprintf函数** **5.wri ...

最新文章

  1. Delphi面向对象编程的20条规则
  2. php mpdf html 转pdf,使用 MPDF 将HTML转为PDF,然后将该PDF转为PNG图片的时候,中文报错... ......
  3. 3层、5层、3层一个卷积核BP神经网络性能比较
  4. Java中的一些零星容易被忽略的API(及时补充)
  5. PHP5魔术函数与魔术常量
  6. c# 流,字节数组及字符串之间的转换
  7. Windows下安装配置SubVersion的简明步骤
  8. 嘉年华回顾丨周振兴带你解密POLARDB产品架构与实现
  9. 炫彩渐变液态海报设计,太skr了!
  10. Java基础学习总结(64)——Java内存管理
  11. 【noip 2016】 蚯蚓(earthworm)
  12. win11虚拟内存如何设置 Windows11设置虚拟内存的步骤方法
  13. VisualRoute for Mac OS 体验
  14. 2022 分布式存储市场调研报告
  15. 小米10获取root权限_2020年小米红米Miflash新版刷机救砖恢复去除ROOT权限教程
  16. 俞敏洪:人生最重要的两件事是什么?
  17. 抖音、快手打起来了,互联网大佬集体内卷?
  18. 小米 android微博授权管理工具下载,微博
  19. Redis Setnx 命令(转)
  20. 如何制作渐变色二维码

热门文章

  1. IPC RFC call in Service Order scenario
  2. 如何修改SAP calendar 里一周开始的第一天
  3. Service order save debug for distribution lock set logic
  4. SAP WebIDE Initialization process - 初始化逻辑分析
  5. SAP CRM Fiori应用Simulation pipeline的刷新问题
  6. 查看SAP CRM和C4C的UI technical信息
  7. java运维工程师做什么_网络工程师和网络运维工程师有什么不同?
  8. Java入门算法(双指针篇)丨蓄力计划
  9. 如果要用thinkphp框架_php需要改哪些配置,thinkPHP框架动态配置用法实例分析
  10. python虚拟cpu性能_python实现可视化动态CPU性能监控