View Full Version : BBcode help? :)
Excellent2
26-09-2008, 08:01 PM
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.
Decode
26-09-2008, 08:28 PM
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 and leave it open and the rest of the page would be in bold.
$string = "[b]This text is in bold";
preg_replace("/(.*?)/", "<b>$1</b>", $string);
That may not be 100% right but give it a go.
Excellent2
26-09-2008, 08:28 PM
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:
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.
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
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);
}
Excellent2
26-09-2008, 08:50 PM
oh yeah my bad
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);
}
Parse error: syntax error, unexpected '=', expecting ')' in /home/luke/public_html/syst/bbcode.php on line 5
oh crap
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);
}
that should work
Excellent2
26-09-2008, 09:14 PM
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? :)
Invent
26-09-2008, 09:51 PM
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? :)
<?php
$string = 'hay lol';
$bbcode = array(
'/\[b\](.*?)\[\/b\]/is',
'/\[u\](.*?)\[\/u\]/is'
);
$replace = array(
'<strong>$1</strong>',
'<u>$1</u>'
);
$string = preg_replace( $bbcode, $replace, $string );
echo $string;
?>
@excellent do this:
$whatever = " bold italics ";
echo bbcode($whatever);
EDIT: simon beat me :(
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
Invent
26-09-2008, 10:01 PM
If the character is used in reg ex, then you need to escape it.
HotelUser
27-09-2008, 02:07 PM
this worked for me when changing font, size or color using BB:
$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);
Want to hide these adverts? Register an account for free!
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.