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

前端之家收集整理的这篇文章主要介绍了php 字符过滤类,用于过滤各类用户输入的数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

详细代码如下:
<div class="codetitle"><a style="CURSOR: pointer" data="92458" class="copybut" id="copybut92458" onclick="doCopy('code92458')"> 代码如下:

<div class="codebody" id="code92458">
<?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);
}
}
?>

使用方法如:


<div class="codetitle"><a style="CURSOR: pointer" data="84172" class="copybut" id="copybut84172" onclick="doCopy('code84172')"> 代码如下:
<div class="codebody" id="code84172">
$filter = new FileNameFilter(); //定义实例
$downFile = $filter->filtit($_GET[´fn´]); //调用过滤方法

猜你在找的PHP相关文章