PHP implementation of replacing sensitive string class instances, string instances_PHP tutorial

WBOY
Release: 2016-07-13 10:18:32
Original
893 people have browsed it

PHP implementation of replacement sensitive string class instance, string instance

The example in this article describes the replacement sensitive string class implemented by PHP and its usage, which has a very wide range of application value in PHP program development. Share it with everyone for your reference. The specific method is as follows:

StrFilter.class.php class file is as follows:

<&#63;php 
/** string filter class 
* Date:   2013-01-09 
* Author:  fdipzone 
* Ver:   v1.0 
* 
* Func: 
* public replace      替换非法字符 
* public check       检查是否含有非法字符 
* private protect_white_list 保护白名单 
* private resume_white_list 还原白名单 
* private getval       白名单 key转为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; 
  } 
 
  /** 替换非法字符 
  * @param String $content 要替換的字符串 
  * @return String     替換后的字符串 
  */ 
  public function replace($content){ 
 
    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 = str_replace($val, $this->_replacement, $content); 
      } 
    } 
 
    // resume white list 
    $content = $this->resume_white_list($content); 
 
    return $content; 
  } 
 
  /** 检查是否含有非法自符 
  * @param String $content 字符串 
  * @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; 
  } 
 
  /** 保护白名单 
  * @param String $content 字符串 
  * @return String 
  */ 
  private function protect_white_list($content){ 
    if($this->_white_list){ 
      foreach($this->_white_list as $key=>$val){ 
        $content = str_replace($val, $this->_LTAG.$key.$this->_RTAG, $content); 
      } 
    } 
    return $content; 
  } 
 
  /** 还原白名单 
  * @param String $content 
  * @return String 
  */ 
  private function resume_white_list($content){ 
    if($this->_white_list){ 
      $content = preg_replace_callback("/\[\[##(.*&#63;)##\]\].*&#63;/si", array($this, 'getval'), $content); 
    } 
    return $content; 
  } 
 
  /** 白名单 key还原为value 
  * @param Array $matches 匹配white_list的key 
  * @return String white_list val 
  */ 
  private function getval($matches){ 
    return isset($this->_white_list[$matches[1]])&#63; $this->_white_list[$matches[1]] : ''; // key->val 
  } 
} // class end 
&#63;>

Copy after login

The demo example is as follows:

<&#63;php 
  header("content-type:text/html;charset=utf8"); 
 
  require("StrFilter.class.php"); 
 
  $white = array('屌丝', '曹操'); 
  $black = array('屌', '操'); 
 
  $content = "我操,曹操你是屌丝,我屌你啊"; 
 
  $obj = new StrFilter($white, $black); 
  echo $obj->replace($content); 
&#63;>

Copy after login

Click here to download the complete example code

I hope this article will be helpful to everyone’s learning of PHP programming.

php string replacement

baidu", $string);echo $string ;?>

php replaces the content between two strings (including these two strings) in the specified string

It is best not to use regular expressions for this type of replacement, because the only content you are searching for does not need to be used, and the replacement text is large and regular expressions are too inefficient.

The method is to find the positions of these two special strings, and then use substr to delete the middle part. Example code:

$str='....The string you want to process... ...';
$s1='...start string...';
$s2='...end string...';
$i1=strpos($ str,$s1);//Start position
$i2=strpos($str,$s2);//End position
if ($i1!==false && $i2!==false)// Find
$str=substr($str,0,$i1-1) . substr($str,$i2+strlen($s2));

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/882905.htmlTechArticle Examples of the replacement sensitive string class implemented by php, string examples This article describes the replacement of sensitive strings implemented by php Classes and their usage have a very wide application value in PHP program development...
Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template