午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    php實現(xiàn)的替換敏感字符串類實例
    來源:易賢網(wǎng) 閱讀:927 次 日期:2014-10-11 10:12:36
    溫馨提示:易賢網(wǎng)小編為您整理了“php實現(xiàn)的替換敏感字符串類實例”,方便廣大網(wǎng)友查閱!

    StrFilter.class.php類文件如下:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    83

    84

    85

    86

    87

    88

    89

    90

    91

    92

    93

    94

    95

    96

    97

    98

    99

    100

    101

    102

    103

    104

    105

    106

    107

    108

    109

    110

    111

    112

    113

    114

    115

    <?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轉(zhuǎn)為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("/[[##(.*?)##]].*?/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示例如下:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    <?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);

    ?>

    更多信息請查看IT技術(shù)專欄

    更多信息請查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機網(wǎng)站地址:php實現(xiàn)的替換敏感字符串類實例

    2025國考·省考課程試聽報名

    • 報班類型
    • 姓名
    • 手機號
    • 驗證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)