php 字符过滤类,用于过滤各类用户输入的数据
程序员文章站
2022-06-29 08:49:48
详细代码如下: 复制代码 代码如下:
详细代码如下:
<?php
abstract class filter { //filter parent class
private $blackstr = array();
private $whitestr = array();
function filtit($str) {
//do something
}
}
class loginfilter extends filter { //for user login filte username(过滤注册的用户名)
function filtit($str) {
$this -> blackstr = array(
´/[\x7f-\xff]/´, //filter chinese include chinese symbol
´/\w/´ //filter all english symbol
);
return preg_replace($this->blackstr, ´´, $str);
}
}
class editorfilter extends filter { //for article editor filter(过滤在线编辑器内容)
function filtit($str) {
$this -> blackstr = array(
´/\&/´,
´/\´/´,
´/\"/´,
´/\</´,
´/\>/´,
´/\\\\/´,
´/\//´,
´/-/´,
´/\*/´,
´/ /´
);
$this -> whitestr = array(
´&´,
´'´,
´"´,
´<´,
´>´,
´\´,
´/´,
´-´,
´*´,
´ ´
);
return preg_replace($this->blackstr, $this -> whitestr, $str);
}
}
class sqlfilter extends filter { //for filte sql query string(过滤如查询或其它sql语句)
function filtit($str) {
$this -> blackstr = array(
´/\´/´,
´/-/´
);
return preg_replace($this->blackstr, ´´, $str);
}
}
class filenamefilter extends filter { //for filte a file name(过滤文件名如下载文件名)
function filtit($str) {
$this -> blackstr = array(
´/[^a-za-z0-9_\.]|\\\\|\^|\[|\]/´
);
return preg_replace($this->blackstr, ´´, $str);
}
}
?>
使用方法如:
$filter = new filenamefilter(); //定义实例
$downfile = $filter->filtit($_get[´fn´]); //调用过滤方法
复制代码 代码如下:
<?php
abstract class filter { //filter parent class
private $blackstr = array();
private $whitestr = array();
function filtit($str) {
//do something
}
}
class loginfilter extends filter { //for user login filte username(过滤注册的用户名)
function filtit($str) {
$this -> blackstr = array(
´/[\x7f-\xff]/´, //filter chinese include chinese symbol
´/\w/´ //filter all english symbol
);
return preg_replace($this->blackstr, ´´, $str);
}
}
class editorfilter extends filter { //for article editor filter(过滤在线编辑器内容)
function filtit($str) {
$this -> blackstr = array(
´/\&/´,
´/\´/´,
´/\"/´,
´/\</´,
´/\>/´,
´/\\\\/´,
´/\//´,
´/-/´,
´/\*/´,
´/ /´
);
$this -> whitestr = array(
´&´,
´'´,
´"´,
´<´,
´>´,
´\´,
´/´,
´-´,
´*´,
´ ´
);
return preg_replace($this->blackstr, $this -> whitestr, $str);
}
}
class sqlfilter extends filter { //for filte sql query string(过滤如查询或其它sql语句)
function filtit($str) {
$this -> blackstr = array(
´/\´/´,
´/-/´
);
return preg_replace($this->blackstr, ´´, $str);
}
}
class filenamefilter extends filter { //for filte a file name(过滤文件名如下载文件名)
function filtit($str) {
$this -> blackstr = array(
´/[^a-za-z0-9_\.]|\\\\|\^|\[|\]/´
);
return preg_replace($this->blackstr, ´´, $str);
}
}
?>
使用方法如:
复制代码 代码如下:
$filter = new filenamefilter(); //定义实例
$downfile = $filter->filtit($_get[´fn´]); //调用过滤方法
上一篇: 关于分布式事务的实现梳理
下一篇: 怎样炖乌鸡汤好喝又营养,乌鸡汤热门做法