src/View/Helper/ObfuscateHelper.php
public function wordCensor($str, array $censored, $replacement = null) {
		if (empty($censored)) {
			return $str;
		}
		$str = ' ' . $str . ' ';

		// \w, \b and a few others do not match on a unicode character
		// set for performance reasons. As a result words like ..ber
		// will not match on a word boundary. Instead, we'll assume that
		// a bad word will be bookended by any of these characters.
		$delim = '[-_\'\"`() {}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';

		foreach ($censored as $badword) {
			if ($replacement !== null) {
				$str = preg_replace("/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/i", "\\1{$replacement}\\3", $str);
			} else {
				$str = preg_replace_callback("/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/i", function ($matches) {
					return $matches[1] . str_repeat('#', strlen($matches[2])) . $matches[3];
				}, $str);
			}
		}

		return trim($str);
	}
public function wordCensor($str, array $censored, $replacement = null) {
		if (empty($censored)) {
			return $str;
		}
		$str = ' ' . $str . ' ';

		// \w, \b and a few others do not match on a unicode character
		// set for performance reasons. As a result words like ..ber
		// will not match on a word boundary. Instead, we'll assume that
		// a bad word will be bookended by any of these characters.
		$delim = '[-_\'\"`() {}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';

		foreach ($censored as $badword) {
			if ($replacement !== null) {
				$str = preg_replace("/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/i", "\\1{$replacement}\\3", $str);
			} else {
				$str = preg_replace_callback("/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/i", function ($matches) {
					return $matches[1] . str_repeat('#', strlen($matches[2])) . $matches[3];
				}, $str);
			}
		}

		return trim($str);
	}
public function wordCensor ( $ str , array $ censored , $ replacement = null ) { if ( empty ( $ censored ) ) { return $ str ; } $ str = ' ' . $ str . ' ' ; // \w, \b and a few others do not match on a unicode character // set for performance reasons. As a result words like ..ber // will not match on a word boundary. Instead, we'll assume that // a bad word will be bookended by any of these characters. $ delim = '[-_\'\"`() {}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]' ; foreach ( $ censored as $ badword ) { if ( $ replacement !== null ) { $ str = preg_replace ( "/({$delim})(" . str_replace ( '\*' , '\w*?' , preg_quote ( $ badword , '/' ) ) . ")({$delim})/i" , "\\1{$replacement}\\3" , $ str ) ; } else { $ str = preg_replace_callback ( "/({$delim})(" . str_replace ( '\*' , '\w*?' , preg_quote ( $ badword , '/' ) ) . ")({$delim})/i" , function ( $ matches ) { return $ matches [ 1 ] . str_repeat ( '#' , strlen ( $ matches [ 2 ] ) ) . $ matches [ 3 ] ; } , $ str ) ; } } return trim ( $ str ) ; }
Word Censoring Function

Supply a string and an array of disallowed words and any
matched words will be converted to #### or to the replacement
word you've submitted.

@param string $str the text string
@param array $censored the array of censored words
@param string|null $replacement the optional replacement value
@return string
Word Censoring Function