Here is a back-port function that takes care of special chars, formatting, bbcode and censor all in one. I post here, because I plan to gain feedback on new things coming to this function such as:
- Filter validation
- Improved bbcode array
- Whatever else fits here
Requires: mbstring extension
This function is critical for our descriptions, so its been with years of active attention.
function format_comment(string $text, bool $utf8 = false): string {
$bbcode = strtr($text, array_combine(array('', '', '', '', '', '', '[img]', '[/img]', '[img=', '[/img]'), array('<b>', '</b>', '<i>', '</i>', '<u>', '</u>', '<img src=', ' />', '<img src=', ' />')));
$string = str_replace(' ', ' ', nl2br(mb_convert_encoding($bbcode, 'UTF-8', 'ISO-8859-1')));
$smilies = array(':)' => 'smile.png', ':(' => 'sad.png', ';)' => 'wink.png', ':P' => 'razz.png', ':D' => 'grin.png', ':|' => 'plain.png', ':O' => 'suprise.png', ':?' => 'confused.png', '8)' => 'glasses.png', '8o' => 'eek.png', 'B)' => 'cool.png', ':-)' => 'smile-big.png', ':-(' => 'crying.png', ':-*' => 'kiss.png', 'O:-D' => 'angel.png', ':-@' => 'devilish.png', ':o)' => 'monkey.png', ':help' => 'help.png', ':love' => 'love.png', ':warn' => 'warn.png', ':bomb' => 'bomb.png', ':idea' => 'idea.png', ':bad' => 'bad.png', ':!' => 'important.png', 'brb' => 'brb.png');
$badword = false;
foreach ($smilies as $code => &$url) {
$string = str_replace($code, '<img style="width:25px;height:25px;vertical-align:middle" src="/images/smilies/' . $url . '" alt="' . $code . '" title="' . $code . '" />', $string);
}
$r = SQL_Query_exec('SELECT word, censor FROM censor');
foreach (mysqli_fetch_assoc($r) as $rr) {
$badword = true;
$string = str_replace(trim($rr['word']), '<img src="/images/censored.png" alt="Censored" title="Censored">', $string);
}
$encoding = $utf8 ? 'UTF-8' : 'HTML-ENTITIES';
return mb_convert_encoding($string, $encoding, 'ISO-8859-1');
}
Usage
format_comment($row['descr'], true);
-----
This one is much better. It uses ICONV for better character encoding/detection, enable/disable HTML, support bbcode, censor and UTF8 (optional). I'll be replacing the old function (above) with this function.
Requires: iconv + mbstring extensions
UPDATED Jan/15/23
function format_comment(string $text, bool $utf8 = false, $html = false): mixed {
$encoding = mb_detect_encoding($text);
if ($encoding !== 'UTF-8') {
$string = iconv($encoding, 'UTF-8', $text);
}
$bbcode = strtr($text, array_combine(array('', '', '', '', '', '', '[img]', '[/img]', '[img=', '[/img]'), array('<b>', '</b>', '<i>', '</i>', '<u>', '</u>', '<img src=', ' />', '<img src=', ' />')));
$string = preg_replace('/\r\n|\r|\n/', '<br>', $bbcode, -1, $count);
if ($count === 0) {
$string = strip_tags($string, '<br>');
}
$smilies = array(':)' => 'smile.png', ':(' => 'sad.png', ';)' => 'wink.png', ':P' => 'razz.png', ':D' => 'grin.png', ':|' => 'plain.png', ':O' => 'suprise.png', ':?' => 'confused.png', '8)' => 'glasses.png', '8o' => 'eek.png', 'B)' => 'cool.png', ':-)' => 'smile-big.png', ':-(' => 'crying.png', ':-*' => 'kiss.png', 'O:-D' => 'angel.png', ':-@' => 'devilish.png', ':o)' => 'monkey.png', ':help' => 'help.png', ':love' => 'love.png', ':warn' => 'warn.png', ':bomb' => 'bomb.png', ':idea' => 'idea.png', ':bad' => 'bad.png', ':!' => 'important.png', 'brb' => 'brb.png');
foreach ($smilies as $code => &$url) {
$string = str_replace($code, '<img style="width:25px;height:25px;vertical-align:middle" src="/images/smilies/' . $url . '" alt="' . $code . '" title="' . $code . '" />', $string);
}
$result = SQL_Query_exec('SELECT word, censor FROM censor');
$censoredWords = mysqli_fetch_all($result, MYSQLI_ASSOC);
foreach ($censoredWords as $rr) {
$string = str_replace(trim($rr['word']), '<img src="/images/censored.png" alt="Censored" title="Censored">', $string);
}
if ($utf8) {
$string = iconv('UTF-8', 'ISO-8859-1//IGNORE', $string);
} else {
$string = mb_convert_encoding($string, 'HTML-ENTITIES', 'ISO-8859-1');
}
if (strlen($string) > 0) {
return $string;
}
return false;
}
Usage
format_comment($row['descr'], false, true);
if ($text = format_comment($descr, false, true)) {
echo $text;
} else {
echo 'Unable to convert charset.';
}
NOTE:
mysql_fetch_all caches the table, that way its not being hit with tons of requests for just a censor image. Eliminates unnecessary computations (just use PDO lol)