php 表单敏感字符过滤代码
/**
* 表单生成验证文件
*/
$_form = new formHtmlFind();
class formHtmlFind{
/**
* 输出表单函数
* $formKey 表单键
* $infoArray 更新时的原始信息数组
*/
public function formHtml($array,$infoArray='')
{
// 检测数组是否存在
if(empty($array))return false;
$newform = null;
// 信息数组(更新信息)
$this->infoArray = !empty($infoArray)?$infoArray:array();
$this->array['class'] = get_class_methods(get_class());
foreach ($array as $key =>$arr)
{
// 键值转换为纯英文
$key = preg_replace("/[^a-z]/i",'',$key);
// 生成表单
$newform .= $this->outputForm($arr,$key);
}
// 输出表单
return $newform.$this->jsError();
}
/**
* 生成表单函数
*/
private function outputForm($arr,$key)
{
$value = null;
if(empty($arr))return false;
// input Type
$type = $key;
// input NAME
$name = trim($arr[0]);
// input 初始值 不包含多选,单选类
$value = (!empty($this->infoArray[$name]))? trim($this->infoArray[$name]):trim($arr[1]);
$value = empty($this->post[$name])? $value :trim($this->post[$name]);
// input Title
$title = trim($arr[2]);
// 样式
$style = trim($arr[3]);
if($key!=="hidden")
{
$dt = "
}
else $data = "";
// 输出新的数据
return $data;
}
/**
* 多选类表单生成
*/
private function formSelect($type,$name,$value,$title,$style)
{
$outform = null;
// 触发更新和提交动作时的初始
$nowvalue = (!empty($this->post[$name]))?$this->post[$name]:$this->infoarray[$name];
// 兼容多选的识别,转为数组
if(!empty($nowvalue))$valueArray = explode(",",$nowvalue);
// 选项标题
if(is_array($title))
{
array_unshift($title,'选择');
$titarray = array_values($title);
}else $titarray = explode("|",$title);
// 选项值
if(is_array($value))
{
array_unshift($value,'选择');
$valarray = array_keys($value);
if(empty($title))$titarray = array_values($value);
}
else $valarray = explode("|",$value);
// 取消表单的初始默认值
if(!empty($this->post)&&!empty($this->infoArray))$value = preg_replace("/Y_/i",'',$value);
foreach ($valarray as $key =>$varl)
{
// 非默认的识别
if(!empty($valueArray))$select = (in_array($varl,$valueArray))?'Y':'';
// 判断是否为默认
else $select = (eregi("Y_",$varl))? 'Y':'';
if($key >'0')
{
$_title=($titarray[$key])? $titarray[$key]:$title;
switch ($type)
{
case 'select':
if('Y' == $select)$select = 'selected';
$outform .= sprintf("%srn"
,$select,preg_replace("/Y_/i",'',$varl),$_title);
break;
case 'radio':
if('Y' == $select)$select = 'checked';
$outform .= sprintf("rn",
$_title,$select,$name,$varl,$style);
break;
case 'checkbox':
if('Y' == $select)$select = 'checked';
$outform .= sprintf("rn",$_title,$select,$name,$varl,$style);
break;
}
$select =null;
}
}
// 下拉选择
if($type =='select')$outform = sprintf('',$name,$style,$outform);
return sprintf("
// 演示:
$form[1] =array(
'text'=>array('title','','产品名称','size=40','产品名称不可缺少!','Y','cn,1-30'),
'text1'=>array('categories','','产品名称','','','Y_base64'),
'select'=>array('superiors','||1|2|Y_3','产品类别|选择|1|2|3','','必选项','Y'),
'radio'=>array('superiors1','|1|Y_2|3','产品xun|产品1|产品2|产品3','','必选项','Y'),
'checkbox'=>array('superiors2',array(1=>'11',2=>'22',3=>'33'),'','','必选项','Y'),
'file'=>array('ddd','','文件'),
);
$form =array (
'login' =>
array (
'text' =>
array (
0 => 'user',
1 => '',
2 => '用户名',
3 => 'size=20',
4 => '!',
5 => 'Y',
6 => 'numen,6-12',
),
'password' =>
array (
0 => 'pass',
1 => '',
2 => '密 码',
3 => 'size=22',
4 => '密码格式错误!',
5 => 'Y_md5',
6 => 'numen,6-12',
),
'radio' =>
array (
0 => 'time',
1 => '|7200|3600|1800',
2 => 'cookies有效时间|2小时|1小时|30分钟',
3 => '',
4 => '',
5 => 'N_delete',
6 => '',
),
),
);
// 表单提交效验
$past = $_form->postForm($form['login']);
$dd = array('title'=>'标题','categories'=>'类别');
// $dd 为已有的信息(如更新时的信息输出) POST数据位内部处理具有优先权
if(!empty($past))
{
echo "
";";
print_r($past);
echo"