选择语言 :

 Core_Text::censor

Replaces the given words with a string.

// Displays "What the #####, man!" echo Text::censor('What the frick, man!', array( 'frick' => '#####', ));

string Core_Text::censor( string $str , array $badwords [, string $replacement = string(1) "#" , boolean $replace_partial_words = bool true ] )
uses
UTF8::strlen

参数列表

参数 类型 描述 默认值
$str string Phrase to replace words in
$badwords array Words to replace
$replacement string Replacement string string(1) "#"
$replace_partial_words boolean Replace words across word boundries (space, period, etc) bool true
返回值
  • string
File: ./core/classes/text.class.php
public static function censor($str, $badwords, $replacement = '#', $replace_partial_words = TRUE)
{
    foreach ( (array)$badwords as $key => $badword )
    {
        $badwords[$key] = str_replace('\*', '\S*?', preg_quote((string)$badword));
    }

    $regex = '(' . implode('|', $badwords) . ')';

    if ( $replace_partial_words === FALSE )
    {
        // Just using \b isn't sufficient when we need to replace a badword that already contains word boundaries itself
        $regex = '(?<=\b|\s|^)' . $regex . '(?=\b|\s|$)';
    }

    $regex = '!' . $regex . '!ui';

    if (UTF8::strlen($replacement) == 1)
    {
        $regex .= 'e';
        return preg_replace($regex, 'str_repeat($replacement, UTF8::strlen(\'$1\'))', $str);
    }

    return preg_replace($regex, $replacement, $str);
}