Jump to content

BBCodes


Anders Moen

Recommended Posts

Can anyone make the BBCodes work in this PHP script?

<?php$con = mysql_connect("localhost","username","password");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("database", $con);$result = mysql_query("SELECT * FROM table ORDER BY `id` DESC LIMIT 0, 10");while($row = mysql_fetch_array($result))  {  echo "<div style='color: black; background-color: lightgreen; padding: 2px; border: 1px solid lightgreen;'>";  echo "<div style='width: 70px; float: left;'>";  echo "<a href=\"mailto:$row[email]\">$row[navn]</a>";  echo "</div>";  echo "<div style='width: 68px; float: right;'>";  echo "$row[dato]";  echo "</div>";  echo "<br />";  echo "<br />";  echo "$row[kommentar]";  echo "</div>";  echo "<br />";  echo "<br />";  }mysql_close($con);?>

Link to comment
Share on other sites

Well.. you can make BBCode work better when you use preg_replace. 'Cause when I type '[ /b]' with the str_replace method there will be a </b> in the source, or with '[ b]' only a never ending bold tag.

preg_replace ('/\[b\](.*?)\[\/b\]/is', '<strong>$1</strong>', $str);

When u use this, it'll only replace when there is a [ b]...[/ b] match :)Edit; @Topicstarter: You could make a function called 'bbcode' with the text as param, and return de text param, only replaced. Something like this:

function bbcode ( $text ){	$text = preg_replace ('/\[b\](.*?)\[\/b\]/is', '<strong>$1</strong>', $text);	// etc	// etc	return $text;}

Link to comment
Share on other sites

<?function bbcode($tekst){$tekst = str_replace("[center]", "<center>", $tekst);$tekst = str_replace("[/center]", "</center>", $tekst);//So for spaceing you will need something like this : $tekst = str_replace(array("\r\n", "\n", "\r"), '<br>', $tekst); return $tekst;}//return function in forums/shoutbox ++ echo"".bbcode($fetch->tekst)."";?>

->Kristian_C.

Link to comment
Share on other sites

<?php$con = mysql_connect("localhost","username","password");if (!$con){die('Could not connect: ' . mysql_error());}mysql_select_db("database", $con);function bbcode($tekst){$tekst = str_replace("[center]", "<center>", $tekst);$tekst = str_replace("[/center]", "</center>", $tekst);//So for spaceing you will need something like this : $tekst = str_replace(array("\r\n", "\n", "\r"), '<br>', $tekst); return $tekst;}$result = mysql_query("SELECT * FROM table ORDER BY `id` DESC LIMIT 0, 10");while($row = mysql_fetch_array($result)){echo "<div style='color: black; background-color: lightgreen; padding: 2px; border: 1px solid lightgreen;'>";echo "<div style='width: 70px; float: left;'>";echo "<a href=\"mailto:$row[email]\">$row[navn]</a>";echo "</div>";echo "<div style='width: 68px; float: right;'>";echo "$row[dato]";echo "</div>";echo "<br />";echo "<br />";echo "".bbcode($row[kommentar])."";echo "</div>";echo "<br />";echo "<br />";}mysql_close($con);?>

You will need to add more bbcodes tho.more then what i posted now i really dont think you need to edit.->Kristian_C.

Link to comment
Share on other sites

I need more help :)I want people to be able to make links, like this: text but I don't know...I've tried for some time now, but I'm not able to make it happen :SIt only comes a link to the site their on at that moment...

Link to comment
Share on other sites

:)It's a syntax error there:)Can't find out what it is :S
Try this :
$tekst = str_replace("[url]", "<a target='newframe' href=", $tekst);$tekst = str_replace("[*url]", ">", $tekst);$tekst = str_replace("[/url]", "</a>", $tekst);

->Kristian_C.

Link to comment
Share on other sites

Here is one function that handles BBcodes:

<?php #************************************************************* #				   BBCODE FUNCTION V 0.1 #	 AUTHOR:  JUSTIN PALMER #	 WEBSITE:  HTTP://WWW.ISOLATED-DESIGNS.NET/CORE #	 LICENSE: GNU LESSER GENERAL PUBLIC LICENSE http://www.gnu.org/copyleft/lesser.html #	 IF YOU MAKE ANY MODIFICATIONS, FEEL FREE TO SEND THEM TO ME AND I'LL UPDATE THIS THING! # #   ALLOWS YOU TO USE BBCODE IN NEWS POSTINGS #   CURRENTLY SUPPORTS: #   [b]bold[/b]																						 : BOLD TEXT #   [i]Italic[/i]																				  : ITALIC TEXT #   [u]Underline[/u]																				: UNDERLINED TEXT #   [strike]Text[/strike]																		: STRIKE THROUGH TEXT #   [color=#ffffff]Colored Text[/color]										  : COLORED TEXT #   [email]me@email.com[/email]															: EMAIL LINK #   [img=http://www.blah.com/img.gif]									: IMAGE #   [url=http://www.domain.com]Text[/url]										: HYPERLINKED TEXT OR IMAGE #   [url]http://www.url.com[/url]														: HYPERLINK #   [url]www.yourdomain.com[/url]											  : HYPERLINK WWW #   [flash=width,height]http://blah.com/flash.swf[/flash]		: FLASH MOVIE #   [quote]Text![/quote]																		: QUOTE	 #   [indent]Text[/indent]																		: PREFORMATTED TEXT #   [size=1-6]Text[/size]																		: TEXT HEADINGS echo bbcode('[color=#999999]This[/color]  							[strike]is[/strike]  							[B]Bold[/B]  							[u]text[/u] 							[flash=100,100]http://www.purephotoshop.com/cartoonsmart_ad.swf[/flash] 							 [i]Italic[/i] 						  [email]me@me.com[/email] 							[url=http://www.purephotoshop.com] 							[img=http://www.purephotoshop.com/images/logo.gif][/url] 							[url]http://www.purephotoshop.com[/url] 							[url=http://www.purephotoshop.com]Purephotoshop[/url] 							[quote]Hey, this is a quote![/quote] 							[size=3]Size[/size]'); function bbcode($string){ $string = nl2br(htmlspecialchars($string)); $patterns = array( 									'`\[b\](.+?)\[/b\]`is', 									'`\[i\](.+?)\[/i\]`is', 									'`\[u\](.+?)\[/u\]`is', 									'`\[strike\](.+?)\[/strike\]`is', 									'`\[color=#([0-9]{6})\](.+?)\[/color\]`is', 									'`\[email\](.+?)\[/email\]`is', 									'`\[img\](.+?)\[/img\]`is', 									'`\[url=([a-z0-9]+://)([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\](.*?)\[/url\]`si', 									'`\[url\]([a-z0-9]+?://){1}([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)\[/url\]`si', 									'`\[url\]((www|ftp)\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\[/url\]`si', 									'`\[flash=([0-9]+),([0-9]+)\](.+?)\[/flash\]`is', 									'`\[quote\](.+?)\[/quote\]`is', 									'`\[indent](.+?)\[/indent\]`is', 									'`\[size=([1-6]+)\](.+?)\[/size\]`is' 								 								); $replaces =  array( 									'<strong>\\1</strong>', 									'<em>\\1</em>', 									'<span style="border-bottom: 1px dotted">\\1</span>', 									'<strike>\\1</strike>', 									'<span style="color:#\1;">\2</span>', 									'<a href="mailto:\1">\1</a>', 									'<img src="\1" alt="" style="border:0px;" />', 									'<a href="\1\2">\6</a>', 									'<a href="\1\2">\1\2</a>', 									'<a href="http://\1">\1</a>', 									'<object width="\1" height="\2"><param name="movie" value="\3" /><embed src="\3" width="\1" height="\2"></embed></object>', 									'<strong>Quote:</strong><div style="margin:0px 10px;padding:5px;background-color:#F7F7F7;border:1px dotted #CCCCCC;width:80%;"><em>\1</em></div>', 									'<pre>\\1</pre>', 									'<h\1>\2</h\1>' 									 									 									); $string = preg_replace($patterns, $replaces , $string); return $string; } ?>

The internet has a ton of examples like this that support more codes then this one does, but it's easy to add your own.http://www.google.com/search?client=opera&...-8&oe=utf-8

Link to comment
Share on other sites

That uses regular expressions. It's actually a pretty simple piece of code. The regular expressions are complex, but the code itself is pretty simple. It is just two arrays (one array of patterns to find and one array of replacements to make), and then a function call to preg_replace does all the finding and replacing. If you want to support more bbcodes, you just add a new regular expression to the patterns to find, and add a new expression to replace it with.

Link to comment
Share on other sites

Okay, it doesn't matter if it's an easy code because it works just perfect :)But there's one more trouble I have now. If someone types for example <html> my site wouldn't be valid anymore, so I want it to so <html> = <html>. Or else they can ###### up my site. I tried to use the same code as you gave me (preg_replace = "<", "<", $tekst) or something, I don't remember the code. But when I have that, they can't choose font-size and stuff, because <span style="font-size: 15pt;">text</span> gets replaced with <span style="font-size: 15pt;">text</span> so the code shows instead...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...