>  기사  >  백엔드 개발  >  php 字符过滤类,用于过滤各类用户输入的数据_PHP教程

php 字符过滤类,用于过滤各类用户输入的数据_PHP教程

WBOY
WBOY원래의
2016-07-21 15:46:451038검색

详细代码如下:

复制代码 代码如下:

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´]); //调用过滤方法

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/320092.htmlTechArticle详细代码如下: 复制代码 代码如下: ?php abstract class Filter { //filter parent class private $blackstr = array(); private $whitestr = array(); function filtit($str) {...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.