Priamo funkcia uvedená v téme
Užitočné a často používané skripty je funkcia na BBCode a tam sa to dá v podstate vidieť:
Kód:
function bbcode($str)
{
$BBCode = array("/\[b\](.*)\[\/b\]/is", "/\[u\](.*)\[\/u\]/is", "/\[i\](.*)\[\/i\]/is", "/\[url\=(.*?)\](.*?)\[\/url\]/is");
$BBTag = array("<strong>$1</strong>", "<u>$1</u>", "<i>$1</i>", "<a href=\"$1\" target=\"_blank\" rel=\"nofollow\">$2</a>");
$str = preg_replace($BBCode, $BBTag, $str);
return $str;
}
Konkrétny príklad na preg_replace:
Kód:
$mojtext = 'Ahoj [b]svet[/b] môj [b]prenádherný';
$text = preg_replace('/\[b\](.*)\[\/b\]/is', '<b>$1</b>', $mojtext);
Výsledok:
Kód:
Ahoj <b>svet</b> môj [b]prenádherný
Následne nadbytočné BBCode odstrániš pomocou str_replace:
Kód:
$text = str_replace('[b]', '', $text);
$text = str_replace('[/b]', '', $text);