搜尋
首頁後端開發php教程如何使用php 替換敏感字串的相關操作

StrFilter.class.php

<?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 = &#39;*&#39;;
    private $_LTAG = &#39;[[##&#39;;
    private $_RTAG = &#39;##]]&#39;;
    /**
    * @param Array  $white_list
    * @param Array  $black_list
    * @param String $replacement
    */
    public function __construct($white_list=array(), $black_list=array(), $replacement=&#39;*&#39;){
        $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==&#39;&#39;){
            return &#39;&#39;;
        }
        // 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==&#39;&#39;){
            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)!=&#39;&#39;){
                    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, &#39;getval&#39;), $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]] : &#39;&#39;; // key->val
    }
} // class end
?>

#demo##

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


##本文說明如何使用php 取代敏感字串的相關操作,更多相關內容請注意php中文網。

#########相關推薦:#########關於PHP 遍歷資料夾及檔案類別及處理類別############如何通過php取得頁面中的指定內容類別################php 支援斷點續傳的檔案下載類別的相關講解######

以上是如何使用php 替換敏感字串的相關操作的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
PHP如何識別用戶的會話?PHP如何識別用戶的會話?May 01, 2025 am 12:23 AM

phpIdentifiesauser'ssessionSessionSessionCookiesAndSessionId.1)whiwsession_start()被稱為,phpgeneratesainiquesesesessionIdStoredInacookInAcookInAcienamedInAcienamedphpsessIdontheuser'sbrowser'sbrowser.2)thisIdallowSphptpptpptpptpptpptpptpptoretoreteretrieetrieetrieetrieetrieetrieetreetrieetrieetrieetrieetremthafromtheserver。

確保PHP會議的一些最佳實踐是什麼?確保PHP會議的一些最佳實踐是什麼?May 01, 2025 am 12:22 AM

PHP會話的安全可以通過以下措施實現:1.使用session_regenerate_id()在用戶登錄或重要操作時重新生成會話ID。 2.通過HTTPS協議加密傳輸會話ID。 3.使用session_save_path()指定安全目錄存儲會話數據,並正確設置權限。

PHP會話文件默認存儲在哪裡?PHP會話文件默認存儲在哪裡?May 01, 2025 am 12:15 AM

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()

您如何從PHP會話中檢索數據?您如何從PHP會話中檢索數據?May 01, 2025 am 12:11 AM

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

您如何使用會議來實施購物車?您如何使用會議來實施購物車?May 01, 2025 am 12:10 AM

利用會話構建高效購物車系統的步驟包括:1)理解會話的定義與作用,會話是服務器端的存儲機制,用於跨請求維護用戶狀態;2)實現基本的會話管理,如添加商品到購物車;3)擴展到高級用法,支持商品數量管理和刪除;4)優化性能和安全性,通過持久化會話數據和使用安全的會話標識符。

您如何在PHP中創建和使用接口?您如何在PHP中創建和使用接口?Apr 30, 2025 pm 03:40 PM

本文解釋瞭如何創建,實施和使用PHP中的接口,重點關注其對代碼組織和可維護性的好處。

crypt()和password_hash()有什麼區別?crypt()和password_hash()有什麼區別?Apr 30, 2025 pm 03:39 PM

本文討論了PHP中的crypt()和password_hash()的差異,以進行密碼哈希,重點介紹其實施,安全性和對現代Web應用程序的適用性。

如何防止PHP中的跨站點腳本(XSS)?如何防止PHP中的跨站點腳本(XSS)?Apr 30, 2025 pm 03:38 PM

文章討論了通過輸入驗證,輸出編碼以及使用OWASP ESAPI和HTML淨化器之類的工具來防止PHP中的跨站點腳本(XSS)。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具