Home > php教程 > php手册 > php字符过滤方法

php字符过滤方法

WBOY
Release: 2016-06-13 10:47:58
Original
1102 people have browsed it

/**
  * 字符过滤
  * $santype 1 : 剥去 HTML、XML 以及 PHP 的标签,
  *    2 : 剥去 HTML、XML 以及 PHP 的标签,字符转换为 HTML 实体 , 编码双引号和单引号
  *    3 : 剥去 HTML、XML 以及 PHP 的标签,在指定的预定义字符前添加反斜杠    单引号 (') 双引号 (") 反斜杠 (\) NULL
  *    4 : 用于清理从数据库或 HTML 表单中取回的数据 (剥去 HTML、XML 以及 PHP 的标签)
  *    6 : 在指定的预定义字符前添加反斜杠    单引号 (') 双引号 (") 反斜杠 (\) NULL
  *
  * */
  
 function sanitize($var, $santype = 1, $allowable_tags = ''){
  if ($santype == 1) {return strip_tags($var, $allowable_tags = '');}
  elseif ($santype == 2) {return htmlentities(strip_tags($var, $allowable_tags),ENT_QUOTES,'UTF-8');}
  elseif ($santype == 3) {
     return addslashes(strip_tags($var, $allowable_tags));
  }
  elseif ($santype == 4) {
     return stripslashes(preg_replace('/]+)>/es', "''",strip_tags($var, $allowable_tags)));
  }
  elseif ($santype == 5) {
   return preg_replace('/\son\w+\s*=/is','',$var);
  }
  elseif ($santype == 6 ){
   return addslashes($var);
  }
 } 
 
摘自 adamboy

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template