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.
Is there a way in which you could produce arrays for each of them, so that it only requires one preg_replace? I'm not asking you specifically to do it, but is there a way? :)
@excellent do this:
EDIT: simon beat me :(PHP Code:$whatever = "[b] bold [/b] [i] italics [/i]";
echo bbcode($whatever);
Ah thankyou :) I know this may sound dumb, but how do you know where to use slashes and all that with the bbcode bit(/\[b\]) etc? Cheers
If the character is used in reg ex, then you need to escape it.
this worked for me when changing font, size or color using BB:
PHP Code:$string = preg_replace("(\[font=(.+?)\](.+?)\[\/font\])","<font face=$1>$2</font>",$string);
$string = preg_replace("(\[color=(.+?)\](.+?)\[\/color\])","<font color=$1>$2</font>",$string);
$string = preg_replace("(\[size=(.+?)\](.+?)\[\/size\])","<font size=$1>$2</font>",$string);