0

0

整理了php过滤字符串几个例子

php中文网

php中文网

发布时间:2016-06-08 17:20:51

|

1312人浏览过

|

来源于php中文网

原创

php中过滤一些特殊字符我们通常用于在安全数据提交或者敏感词的过滤上,下面整理了一些常用的例子供大家参考,有需要了可进入参考。

例子

我们利用preg_replace与str_ireplace来进行替换操作

塔猫ChatPPT
塔猫ChatPPT

塔猫官网提供AI一键生成 PPT的智能工具,帮助您快速制作出专业的PPT。塔猫ChatPPT让您的PPT制作更加简单高效。

下载
 代码如下 复制代码

public static function filterStr( $value )
{
if ( empty( $value ) )
{
return "";
}
$value = trim( $value );
$badstr = array( "x00", "%00", "r", "&", """, "'", "", "%3C", "%3E" );
$newstr = array( "", "", "", "&", """, "'", "", "" );
$value = str_ireplace( $badstr, $newstr, $value );
$value = preg_replace( "/&((#(d{3,5}|x[a-fA-F0-9]{4}));)/", "&1", $value );
return $value;
}
public static function stripArray( &$_data )
{
if ( is_array( $_data ) )
{
foreach ( $_data as $_key => $_value )
{
$_data[$_key] = trim( self::striparray( $_value ) );
}
return $_data;
}
return stripslashes( trim( $_data ) );
}

另收藏:

 代码如下 复制代码

class XRequest
{
 
public static function getPost( $name = "" )
{
if ( empty( $name ) )
{
return $_POST;
}
if ( isset( $_POST[$name] ) )
{
return $_POST[$name];
}
return "";
}
 
public static function getGet( $name = "" )
{
if ( empty( $name ) )
{
return $_GET;
}
if ( isset( $_GET[$name] ) )
{
return $_GET[$name];
}
return "";
}
 
public static function getCookie( $name = "" )
{
if ( $name == "" )
{
return $_COOKIE;
}
if ( isset( $_COOKIE[$name] ) )
{
return $_COOKIE[$name];
}
return "";
}
 
public static function getSession( $name = "" )
{
if ( $name == "" )
{
return $_SESSION;
}
if ( isset( $_SESSION[$name] ) )
{
return $_SESSION[$name];
}
return "";
}
 
public static function fetchEnv( $name = "" )
{
if ( $name == "" )
{
return $_ENV;
}
if ( isset( $_ENV[$name] ) )
{
return $_ENV[$name];
}
return "";
}
 
public static function getService( $name = "" )
{
if ( $name == "" )
{
return $_SERVER;
}
if ( isset( $_SERVER[$name] ) )
{
return $_SERVER[$name];
}
return "";
}
 
public static function getPhpSelf( )
{
return strip_tags( self::getservice( "PHP_SELF" ) );
}
 
public static function getServiceName( )
{
return self::getservice( "SERVER_NAME" );
}
 
public static function getRequestTime( )
{
return self::getservice( "REQUEST_TIME" );
}
 
public static function getUserAgent( )
{
return self::getservice( "HTTP_USER_AGENT" );
}
 
public static function getUri( )
{
return self::getservice( "REQUEST_URI" );
}
 
public static function isPost( )
{
if ( strtolower( self::getservice( "REQUEST_METHOD" ) ) == "post" )
{
return TRUE;
}
return FALSE;
}
 
public static function isGet( )
{
if ( strtolower( self::getservice( "REQUEST_METHOD" ) ) == "get" )
{
return TRUE;
}
return FALSE;
}
 
public static function isAjax( )
{
if ( self::getservice( "HTTP_X_REQUESTED_WITH" ) && strtolower( self::getservice( "HTTP_X_REQUESTED_WITH" ) ) == "xmlhttprequest" )
{
return TRUE;
}
if ( self::getservice( "HTTP_REQUEST_TYPE" ) && strtolower( self::getservice( "HTTP_REQUEST_TYPE" ) ) == "ajax" )
{
return TRUE;
}
if ( self::getpost( "oe_ajax" ) || self::getget( "oe_ajax" ) )
{
return TRUE;
}
return FALSE;
}
 
public static function getip( )
{
static $realip = NULL;
if ( isset( $_SERVER ) )
{
if ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
{
$realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else if ( isset( $_SERVER['HTTP_CLIENT_IP'] ) )
{
$realip = $_SERVER['HTTP_CLIENT_IP'];
}
else
{
$realip = $_SERVER['REMOTE_ADDR'];
}
}
else if ( getenv( "HTTP_X_FORWARDED_FOR" ) )
{
$realip = getenv( "HTTP_X_FORWARDED_FOR" );
}
else if ( getenv( "HTTP_CLIENT_IP" ) )
{
$realip = getenv( "HTTP_CLIENT_IP" );
}
else
{
$realip = getenv( "REMOTE_ADDR" );
}
$one = "([0-9]|[0-9]{2}|1dd|2[0-4]d|25[0-5])";
if ( !@preg_match( "/".$one.".".$one.".".$one.".".$one."$/", $realip ) )
{
$realip = "0.0.0.0";
}
return $realip;
}
 
protected static function uri( )
{
$uri = self::geturi( );
$file = dirname( $_SERVER['SCRIPT_NAME'] );
$request = str_replace( $file, "", $uri );
$request = explode( "/", trim( $request, "/" ) );
if ( isset( $request[0] ) )
{
$GLOBALS['_GET']['c'] = $request[0];
unset( $request[0] );
}
if ( isset( $request[1] ) )
{
$GLOBALS['_GET']['a'] = $request[1];
unset( $request[1] );
}
if ( 1 {
$mark = 0;
$val = $key = array( );
foreach ( $request as $value )
{
++$mark;
if ( $mark % 2 == 0 )
{
$val[] = $value;
}
else
{
$key[] = $value;
}
}
if ( count( $key ) !== count( $val ) )
{
$val[] = NULL;
}
$get = array_combine( $key, $val );
foreach ( $get as $key => $value )
{
$GLOBALS['_GET'][$key] = $value;
}
}
return TRUE;
}
 
public static function getGpc( $value, $isfliter = TRUE )
{
if ( !is_array( $value ) )
{
if ( isset( $_GET[$value] ) )
{
$temp = trim( $_GET[$value] );
}
if ( isset( $_POST[$value] ) )
{
$temp = trim( $_POST[$value] );
}
$temp = $isfliter === TRUE ? XFilter::filterstr( $temp ) : $temp;
return trim( $temp );
}
$temp = array( );
foreach ( $value as $val )
{
if ( isset( $_GET[$val] ) )
{
$temp[$val] = trim( $_GET[$val] );
}
if ( isset( $_POST[$val] ) )
{
$temp[$val] = trim( $_POST[$val] );
}
$temp[$val] = $isfliter === TRUE ? XFilter::filterstr( $temp[$val] ) : $temp[$val];
}
return $temp;
}
 
public static function getArgs( $value, $default = NULL, $isfliter = TRUE )
{
if ( !empty( $value ) )
{
if ( isset( $_GET[$value] ) )
{
$temp = trim( $_GET[$value] );
}
if ( isset( $_POST[$value] ) )
{
$temp = trim( $_POST[$value] );
}
if ( $isfliter )
{
$temp = XFilter::filterstr( $temp );
}
else
{
$temp = XFilter::striparray( $temp );
}
if ( empty( $temp ) && !empty( $default ) )
{
$temp = $default;
}
return trim( $temp );
}
return "";
}
 
public static function getInt( $value, $default = NULL )
{
if ( !empty( $value ) )
{
if ( isset( $_GET[$value] ) )
{
$temp = $_GET[$value];
}
if ( isset( $_POST[$value] ) )
{
$temp = $_POST[$value];
}
$temp = XFilter::filterstr( $temp );
if ( empty( $temp ) || FALSE === XValid::isnumber( $temp ) )
{
if ( TRUE === XValid::isnumber( $default ) )
{
$temp = $default;
}
else
{
$temp = 0;
}
}
return intval( $temp );
}
return 0;
}
 
public static function getArray( $value )
{
if ( !empty( $value ) )
{
if ( isset( $_GET[$value] ) )
{
$temp = $_GET[$value];
}
if ( isset( $_POST[$value] ) )
{
$temp = $_POST[$value];
}
return $temp;
}
return "";
}
 
public static function recArgs( $value )
{
if ( !empty( $value ) )
{
if ( isset( $_GET[$value] ) )
{
$temp = $_GET[$value];
}
if ( isset( $_POST[$value] ) )
{
$temp = $_POST[$value];
}
return XFilter::filterbadchar( $temp );
}
return "";
}
 
public static function getComArgs( $itemname )
{
$args = "";
$array = self::getarray( $itemname );
if ( !empty( $array ) )
{
$ii = 0;
for ( ; $ii {
$val = XFilter::filterbadchar( $array[$ii] );
if ( !empty( $val ) )
{
if ( $ii == 0 )
{
$args = $val;
}
else if ( $args == "" )
{
$args = $val;
}
else
{
$args = $args.",".$val;
}
}
}
}
return $args;
}
 
public static function getComInts( $name )
{
$args = "";
$array = self::getarray( $name );
if ( !empty( $array ) )
{
$ii = 0;
for ( ; $ii {
$val = intval( XFilter::filterbadchar( $array[$ii] ) );
if ( !empty( $val ) )
{
if ( $ii == 0 )
{
$args = $val;
}
else if ( $args == "" )
{
$args = $val;
}
else
{
$args = $args.",".$val;
}
}
}
}
return $args;
}
 
}
 
if ( !defined( "IN_OESOFT" ) )
{
exit( "Access Denied" );
}
?>
class XFilter
{
 
public static function filterBadChar( $str )
{
if ( empty( $str ) || $str == "" )
{
return;
}
$badstring = array( "'", """, """, "=", "#", "$", ">", " $newstring = array( "", "", "", "", "", "", "", "", "", "", "", "", "", "" );
$str = str_replace( $badstring, $newstring, $str );
return trim( $str );
}
 
public static function stripArray( &$_data )
{
if ( is_array( $_data ) )
{
foreach ( $_data as $_key => $_value )
{
$_data[$_key] = trim( self::striparray( $_value ) );
}
return $_data;
}
return stripslashes( trim( $_data ) );
}
 
public static function filterSlashes( &$value )
{
if ( get_magic_quotes_gpc( ) )
{
return FALSE;
}
$value = ( array )$value;
foreach ( $value as $key => $val )
{
if ( is_array( $val ) )
{
self::filterslashes( $value[$key] );
}
else
{
$value[$key] = addslashes( $val );
}
}
}
 
public static function filterScript( $value )
{
if ( empty( $value ) )
{
return "";
}
$value = preg_replace( "/(javascript:)?on(click|load|key|mouse|error|abort|move|unload|change|dblclick|move|reset|resize|submit)/i", "&111n2", $value );
$value = preg_replace( "//si", "", $value );
$value = preg_replace( "//si", "", $value );
$value = preg_replace( "//iesU", "", $value );
return $value;
}
 
public static function filterHtml( $value )
{
if ( empty( $value ) )
{
return "";
}
if ( function_exists( "htmlspecialchars" ) )
{
return htmlspecialchars( $value );
}
return str_replace( array( "&", """, "'", "" ), array( "&", """, "'", "" ), $value );
}
 
public static function filterSql( $value )
{
if ( empty( $value ) )
{
return "";
}
$sql = array( "select", "insert", "update", "delete", "'", "/*", "../", "./", "union", "into", "load_file", "outfile" );
$sql_re = array( "", "", "", "", "", "", "", "", "", "", "", "" );
return str_ireplace( $sql, $sql_re, $value );
}
 
public static function filterStr( $value )
{
if ( empty( $value ) )
{
return "";
}
$value = trim( $value );
$badstr = array( "x00", "%00", "r", "&", """, "'", "", "%3C", "%3E" );
$newstr = array( "", "", "", "&", """, "'", "", "" );
$value = str_ireplace( $badstr, $newstr, $value );
$value = preg_replace( "/&((#(d{3,5}|x[a-fA-F0-9]{4}));)/", "&1", $value );
return $value;
}
 
public static function filterUrl( )
{
if ( preg_replace( "/https?://([^:/]+).*/i", "1", $_SERVER['HTTP_REFERER'] ) !== preg_replace( "/([^:]+).*/", "1", $_SERVER['HTTP_HOST'] ) )
{
return FALSE;
}
return TRUE;
}
 
public static function filterForbidChar( $content )
{
$new_content = $content;
$forbidargs = X::$cfg['forbidargs'];
if ( !empty( $forbidargs ) )
{
$array = explode( ",", $forbidargs );
$i = 0;
for ( ; $i {
$new_content = str_ireplace( $array[$i], "", $content );
}
}
return $new_content;
}
 
public static function checkExistsForbidChar( $content )
{
$flag = FALSE;
$forbidargs = X::$cfg['forbidargs'];
if ( !empty( $forbidargs ) )
{
$array = explode( ",", $forbidargs );
$i = 0;
for ( ; $i {
if ( FALSE === strpos( strtolower( $content ), strtolower( $array[$i] ) ) )
{
continue;
}
$flag = TRUE;
break;
}
}
return $flag;
}
 
public static function checkExistsForbidUserName( $username )
{
$flag = FALSE;
$forbidargs = X::$cfg['lockusers'];
if ( !empty( $forbidargs ) )
{
$array = explode( ",", $forbidargs );
$i = 0;
for ( ; $i {
if ( FALSE === strpos( strtolower( $username ), strtolower( $array[$i] ) ) )
{
continue;
}
$flag = TRUE;
break;
}
}
return $flag;
}
 
}
 
if ( !defined( "IN_OESOFT" ) )
{
exit( "Access Denied" );
}
?>

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
java连接字符串方法汇总
java连接字符串方法汇总

本专题整合了java连接字符串教程合集,阅读专题下面的文章了解更多详细操作。

1

2026.02.05

java中fail含义
java中fail含义

本专题整合了java中fail的含义、作用相关内容,阅读专题下面的文章了解更多详细内容。

2

2026.02.05

控制反转和依赖注入区别
控制反转和依赖注入区别

本专题整合了控制反转和依赖注入区别、解释、实现方法相关内容。阅读专题下面的文章了解更多详细教程。

3

2026.02.05

钉钉脑图插图教程合集
钉钉脑图插图教程合集

本专题整合了钉钉脑图怎么插入图片、钉钉脑图怎么用相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.02.05

python截取字符串方法汇总
python截取字符串方法汇总

本专题整合了python截取字符串方法相关合集,阅读专题下面的文章了解更多详细内容。

2

2026.02.05

Java截取字符串方法合集
Java截取字符串方法合集

本专题整合了Java截取字符串方法汇总,阅读专题下面的文章了解更多详细操作教程。

1

2026.02.05

java 抽象方法
java 抽象方法

本专题整合了java抽象方法定义、作用教程等内容,阅读专题下面的文章了解更多详细内容。

2

2026.02.05

Eclipse创建jsp文件教程合集
Eclipse创建jsp文件教程合集

本专题整合了Eclipse创建jsp文件、创建jsp项目等等内容,阅读专题下面的文章了解更多详细教程。

10

2026.02.05

java 字符串转数字
java 字符串转数字

本专题整合了java如何字符串转数字相关内容,阅读专题下面的文章了解更多详细教程。

2

2026.02.05

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Excel 教程
Excel 教程

共162课时 | 15.6万人学习

Pandas 教程
Pandas 教程

共15课时 | 1万人学习

C# 教程
C# 教程

共94课时 | 8.6万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号