詳細代碼如下:
複製代碼 代碼如下:
<?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´]); //調用過濾方法