Home >Backend Development >PHP Tutorial >PHP character filtering method_PHP tutorial

PHP character filtering method_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:49:32985browse

/**
*Character filter
* $santype 1: Strip HTML, XML and PHP tags,
* 2: Strip HTML, XML and PHP tags, convert characters into HTML entities, encode double quotes and single quotes
* 3: Strip HTML, XML and PHP tags, add backslash before specified predefined characters Single quote (') Double quote (") Backslash () NULL
* 4: Used to clean data retrieved from databases or HTML forms (stripping HTML, XML and PHP tags)
* 6: Add a backslash before the specified predefined character Single quote (') Double quote (") Backslash () 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', "'<'.sanitize('\1',5).'>'",strip_tags($var, $allowable_tags)));
  }
  elseif ($santype == 5) {
   return preg_replace('/sonw+s*=/is','',$var);
  }
  elseif ($santype == 6 ){
   return addslashes($var);
  }
 } 
 
摘自 adamboy

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478337.htmlTechArticle/** * 字符过滤 * $santype 1 : 剥去 HTML、XML 以及 PHP 的标签, * 2 : 剥去 HTML、XML 以及 PHP 的标签,字符转换为 HTML 实体 , 编码双引号和单引号 * 3...
Statement:
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