= $limit) { // ----------------------------------------------------- // Trim the output. // ----------------------------------------------------- $out = trim($out); // ----------------------------------------------------- // Add the ending character to the string. // ----------------------------------------------------- return (strlen($out) == strlen($value)) ? $out : $out.$end; } } } /** * Censor a string. * * @param string $value * @param array $censored * @param string $replacement * @return string */ public static function censor($value, $censored, $replacement = '####') { // ----------------------------------------------------- // Pad the value with spaces. // ----------------------------------------------------- $value = ' '.$value.' '; // ----------------------------------------------------- // Assume the word will be book-ended by the following. // ----------------------------------------------------- $delim = '[-_\'\"`(){}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]'; // ----------------------------------------------------- // Replace the censored words. // ----------------------------------------------------- foreach ($censored as $word) { if ($replacement != '') { $value = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($word, '/')).")({$delim})/i", "\\1{$replacement}\\3", $value); } else { $value = preg_replace("/({$delim})(".str_replace('\*', '\w*?', preg_quote($word, '/')).")({$delim})/ie", "'\\1'.str_repeat('#', strlen('\\2')).'\\3'", $value); } } return trim($value); } }