• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php实现的替换敏感字符串类实例_PHP

php 搞代码 4年前 (2022-01-25) 31次浏览 已收录 0个评论

本文实例讲述了php实现的替换敏感字符串类及其用法,在php程序开发中有着非常广泛的应用价值。分享给大家供大家参考。具体方法如下:

StrFilter.class.php类文件如下:

_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->_r<strong>*本文来源gaodai#ma#com搞@代~码^网+</strong><strong>搞代gaodaima码</strong>eplacement, $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("/\[\[##(.*?)##\]\].*?/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]])? $this->_white_list[$matches[1]] : ''; // key->val   } } // class end ?>

demo示例如下:

replace($content); ?>

完整实例代码点击本站下载

希望本文所述对大家php程序设计的学习有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php实现的替换敏感字符串类实例_PHP
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址