Hey, I'm making a little news system and the last thing I need to do is include the BBCode. I read that I could use str_replace to do this? How would I set about doing it? :)
Printable View
Hey, I'm making a little news system and the last thing I need to do is include the BBCode. I read that I could use str_replace to do this? How would I set about doing it? :)
http://w3schools.com/php/func_string_str_replace.asp
If I was you, i'd just insert the original data into the database, then convert it when displaying it. I'm not sure if converting it then inputting would be quicker, I imagine so, but meh.
You would need to use preg_replace otherwise someone could do [b] and leave it open and the rest of the page would be in bold.
That may not be 100% right but give it a go.PHP Code:$string = "[b]This[/b] text is in [b]bold[/b]";
preg_replace("/[b](.*?)[/b]/", "<b>$1</b>", $string);
Yeah I was thinking that way but I just wanted to say include the file onto the index page so when say.. <b>Hiya</b> is displayed it automatically changes it to bold.
You can make a quick function like this:
Code:function bbcode($content)
{
$content = preg_replace(”\[b\](.+?)\[\/b\]”, “<b>$1</b>”, $content);
$content = preg_replace(”\[i\](.+?)\[\/i\]”, “<i>$1</i>”, $content);
$content = preg_replace(”\[u\](.+?)\[\/u\]”, “<u>$1</u>”, $content);
$content = preg_replace(”\[img\](.+?)\[\/img\]”, “<img src=\”$1\” alt=\”\” />”, $content);
return($content);
}
I get this when doing DUB's method.
Quote:
Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in C:\wamp\www\User system revision\functions.php on line 64
Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in C:\wamp\www\User system revision\functions.php on line 65
Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in C:\wamp\www\User system revision\functions.php on line 66
Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in C:\wamp\www\User system revision\functions.php on line 67
oh yeah my bad
PHP Code:function bbcode($content)
{
$content = preg_replace(”/[b\](.+?)/[\/b\]”, “<b>$1</b>”, $content);
$content = preg_replace(”/[i\](.+?)/[\/i\]”, “<i>$1</i>”, $content);
$content = preg_replace(”/[u\](.+?)/[\/u\]”, “<u>$1</u>”, $content);
$content = preg_replace(”/[img\](.+?)/[\/img\]”, “<img src=\”$1\” alt=\”\” />”, $content);
return($content);
}
oh crap
that should workPHP Code:function bbcode($content)
{
$content = preg_replace('/\[b\](.*?)\[\/b\]/is', '<strong>$1</strong>', $content);
$content = preg_replace('/\[i\](.*?)\[\/i\]/is', '<em>$1</em>', $content);
$content = preg_replace('/\[u\](.*?)\[\/u\]/is', '<u>$1</u>', $content);
$content = preg_replace('/\[img\](.*?)\[\/img\]/is',
'<img src="$1" />', $content);
return($content);
}
Doesn't work when I apply the <strong></strong> tags.