Substitution sensitive String class instance _php techniques implemented by PHP

Source: Internet
Author: User
Tags php class php programming

This article illustrates the substitution sensitive string class of PHP implementation and its usage, which has a very wide application value in the development of PHP program. Share to everyone for your reference. The specific methods are as follows:

The StrFilter.class.php class files are as follows:

<?php/** String Filter class * date:2013-01-09 * author:fdipzone * ver:v1.0 * Func: * Public Replace Replace illegal characters * Public check check for illegal characters * Private protect_white_list protection Whitelist * Private resume_white_list restore WHITE list * PR 
  Ivate getval Whitelist Key to value */class strfilter{//class start Private $_white_list = Array (); 
  Private $_black_list = Array (); 
  Private $_replacement = ' * '; 
  Private $_ltag = ' [[#]; 
  Private $_rtag = ' # #]] '; /** * @param array $white _list * @param array $black _list * @param String $replacement/Public Function _ 
    _construct ($white _list=array (), $black _list=array (), $replacement = ' * ') {$this->_white_list = $white _list; 
    $this->_black_list = $black _list; 
  $this->_replacement = $replacement; /** Replace the illegal character * @param string $content the strings to be replaced * @return string replacement strings/Public function replace ($co 
 ntent) {if (!isset ($content) | | | | $content = = ') {return ';   }//Protect white list $content = $this->protect_white_list ($content); Replace black list if ($this->_black_list) {foreach ($this->_black_list as $val) {$content = s 
      Tr_replace ($val, $this->_replacement, $content); 
    }///Resume white list $content = $this->resume_white_list ($content); 
  return $content; 
    /** check for illegal characters * @param string $content Strings * @return Boolean/Public Function check ($content) { 
    if (!isset ($content) | | | | $content = = ") {return true; 
    }//Protect white list $content = $this->protect_white_list ($content); Check if ($this->_black_list) {foreach ($this->_black_list as $val) {if strstr ($content, $val) 
        != ') {return false; 
  }} return true; /** Protection Whitelist * @param string $content strings * @return string/Private function Protect_white_list ($conteNT) {if ($this->_white_list) {foreach ($this->_white_list as $key => $val) {$content = Str_repla 
      CE ($val, $this->_ltag $key. $this->_rtag, $content); 
  } return $content; 
    /** Restore White list * @param string $content * @return string/Private Function Resume_white_list ($content) { if ($this->_white_list) {$content = Preg_replace_callback ("/\[\[##" (. *?) 
    ##\]\].*?/si ", Array ($this, ' getval '), $content); 
  return $content;  /** White list key reverts to value * @param Array $matches match white_list key * @return String white_list val */Private function Getval ($matches) {return isset ($this->_white_list[$matches [1]])? $this->_white_list[$matches [1]]: ' ;

 Key->val}}//class end?>

The demo example is as follows:

  Header ("Content-type:text/html;charset=utf8"); 
  Require ("StrFilter.class.php"); 
  $white = array (' Cock silk ', ' Cao '); 
  $black = array (' Cock ', ' fuck '); 
  $content = "I fuck, caocao you are cock silk, I cock you ah"; 
  $obj = new Strfilter ($white, $black); 
  echo $obj->replace ($content); 
? >

Full instance code Click this site to download

I hope this article will help you with the learning of PHP programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.