Be a Supporter!

bb code

  • 540 Views
  • 17 Replies
New Topic Respond to this Topic
Loccie
Loccie
  • Member since: Feb. 27, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
bb code 2006-07-22 17:44:40 Reply

I've been working on a posting system a bid and what I want to do is create a bb code. I know I could use the basic html tags but I would also like users to be able to type html tags in the post so I think a bb code is better then. I've gotten this far:


//show html
$GuideContent = str_replace("<", "&lt;", $GuideContent);
$GuideContent = str_replace(">", "&gt;", $GuideContent);

//smilies
$GuideContent = str_replace(":@", "<img src=\"/layout/smilies/angry.gif\" border=\"0\" alt=\":@\" />", $GuideContent);

//tags
$GuideContent = str_replace("[i]", "", $GuideContent);
$GuideContent = str_replace("[/i]", "
", $GuideContent);

$GuideContent = str_replace("[b]", "", $GuideContent);
$GuideContent = str_replace("[/b]", "
", $GuideContent);

$GuideContent = str_replace("[tt]", "<tt>", $GuideContent);
$GuideContent = str_replace("[/tt]", "</tt>", $GuideContent);

$GuideContent = str_replace("[u]", "", $GuideContent);
$GuideContent = str_replace("[/u]", "
", $GuideContent);

//line breaks
$GuideContent = nl2br($GuideContent);

As you can all see I learned how to use str_replace :). but now to links and maybe images to. This is a big problem I can't just use str_replace can I? Anyone has an idea how to make a bb code for a link or image?

DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to bb code 2006-07-22 17:51:06 Reply

For something like a URL bbcode, used like:
[url=http://www.orangefoxgames.com]my site[/url]

$text = preg_replace('/\[url=(.*)\](.*)\[\/url\]/U
si','$2', $text);


BBS Signature
DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to bb code 2006-07-22 17:54:43 Reply

SORRY about that.

The BBS screwed it up. http://pastebin.ca/96396


BBS Signature
Loccie
Loccie
  • Member since: Feb. 27, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to bb code 2006-07-22 18:25:44 Reply

At 7/22/06 05:54 PM, GamesCool wrote: SORRY about that.

The BBS screwed it up. http://pastebin.ca/96396

Thanks a lot, this is what I needed. But I also want the target of the link like on newgrounds if it's a page of my site target = _top if not target = _blank. I tried this:

$GuideContent = preg_replace('/\[url=(.*)\](.*)\[\/url\]/U
si','$2', $GuideContent);

if ($1 == "loc.orgfree.com") {
$target = "_top";
} else }
$target = "_blank";
}

But this of course does not work, also I don't know how to put wildcards next to loc.orgfree.com :). Can you also help me with this please?

DFox
DFox
  • Member since: Aug. 9, 2003
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to bb code 2006-07-22 19:41:01 Reply

At 7/22/06 06:25 PM, Loccie wrote: But this of course does not work, also I don't know how to put wildcards next to loc.orgfree.com :). Can you also help me with this please?

Wait so what HAS to be in the URL in order for it to be on your site?

Like give me an example URL.


BBS Signature
Loccie
Loccie
  • Member since: Feb. 27, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to bb code 2006-07-22 20:02:24 Reply

At 7/22/06 07:41 PM, GamesCool wrote:
At 7/22/06 06:25 PM, Loccie wrote: But this of course does not work, also I don't know how to put wildcards next to loc.orgfree.com :). Can you also help me with this please?
Wait so what HAS to be in the URL in order for it to be on your site?

Like give me an example URL.

well actually I found out how to do it, here is the code:

$GuideContent = preg_replace('/\[url=(.*orgfree.*)\](.*)\[
\/url\]/Usi','$2', $GuideContent);

$GuideContent = preg_replace('/\[url=(.*)\](.*)\[\/url\]/U
si','$2', $GuideContent);

Like this I always have target _top on my own domain. Anyway I go sleep now but I might ask some questions tomorrow.

henke37
henke37
  • Member since: Sep. 10, 2004
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to bb code 2006-07-23 06:06:15 Reply

That is not a secure code.
You need to separate content and markup. And please just use htmlspecialchars instead of a homemade function.
and please try to make sure this forum doesn't corupt your code.

I can do loads of "funny" stuff in that code.
[url=" style="cover screen" onmouseover="steal login]word[/url]


Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.

Loccie
Loccie
  • Member since: Feb. 27, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to bb code 2006-07-23 06:36:35 Reply

At 7/23/06 06:06 AM, henke37 wrote: That is not a secure code.
You need to separate content and markup. And please just use htmlspecialchars instead of a homemade function.
and please try to make sure this forum doesn't corupt your code.

I can do loads of "funny" stuff in that code.
[url=" style="cover screen" onmouseover="steal login]word[/url]

well I actually tried this with:

[url=style="background:#000000;"]

But it just showed [url=style="background:#000000;"] and nothing happened to the background. Are you sure about this? also php is not show it just shows what you've written.

Zendra
Zendra
  • Member since: Sep. 7, 2003
  • Offline.
Forum Stats
Member
Level 51
Blank Slate
Response to bb code 2006-07-23 06:39:15 Reply

Better cover every HTML by using htmlspecialchars.

CyberLemming
CyberLemming
  • Member since: Aug. 9, 2005
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to bb code 2006-07-23 09:13:28 Reply

this looks useful...

JeremysFilms
JeremysFilms
  • Member since: Feb. 18, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to bb code 2006-07-23 10:57:27 Reply

Here's how I make my own "bbcode" and use it.

1) Run htmlentities() on the message being posted to make it safe before it's added to a database.
2) Insert this message that has been altered with htmlentities() into the database.
3) When you are outputting the messages to the page, that's when you want to run something to replace the bbcode with html, this way, if you ever want to alter what a specific tag does, you won't have to go through every post and change the html there, since it'll just have the bbcode in there.

Just use a preg_replace to replace the bbcode with html during output, it's pretty simple:
__________________________________________
________
$pattern=array(
'/\[b\](.*)\[\/b\]/i',
'/\[i\](.*)\[\/i\]/i',
'/\[u\](.*)\[\/u\]/i',
'/\[color=(.*)\](.*)\[\/color\]/i'
);

$replace=array(
'\\1',
'\\1',
'\\1',
'<font color=\"\\1\">\\2</font>'
);

preg_replace($pattern,$replace,$stringToOu
tput);
__________________________________________
________

$stringToOutput would be the string that is being outputted from the database.
Example snippet:

while($row = mysql_fetch_array($result)){
echo preg_replace($pattern,$replace,stripslashe
s($row['message'])).'<br /><br />;
}

JeremysFilms
JeremysFilms
  • Member since: Feb. 18, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to bb code 2006-07-23 11:06:23 Reply

At 7/23/06 10:57 AM, JeremysFilms wrote: $replace=array(
'\\1',
'\\1',
'\\1',
'<font color=\"\\1\">\\2</font>'
);

Damn, NG messed up my post, here's the entire post from pastebin:
http://pastebin.ca/97285

Loccie
Loccie
  • Member since: Feb. 27, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to bb code 2006-07-23 13:28:58 Reply

Thx for all the comments and I'm allready pretty far with this but I would like to know how I can make sure people can't post a very long word that messes up my page layout. In other words how do I add a max length to a word or even better move the pieces of the word that don't fit to the next line.

JeremysFilms
JeremysFilms
  • Member since: Feb. 18, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to bb code 2006-07-23 15:38:19 Reply

At 7/23/06 01:28 PM, Loccie wrote: Thx for all the comments and I'm allready pretty far with this but I would like to know how I can make sure people can't post a very long word that messes up my page layout. In other words how do I add a max length to a word or even better move the pieces of the word that don't fit to the next line.

You could use my and gamescools' custom wordwrap2 function:
[thread] | [function]
(Visit Both)

Or if you want to do a lousy job of automatic line breaking, you could use the old predefined wordwrap() function, but I highly recommend wordwrap2 over the original as it doesn't use character counts, but instead it uses the width of each character to add up to a specific number and once that number is reached it will break. This way, it won't ever break too early because you just set it to reach as long as you want it to stay at.

Loccie
Loccie
  • Member since: Feb. 27, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to bb code 2006-07-23 19:45:12 Reply

At 7/23/06 03:38 PM, JeremysFilms wrote:
At 7/23/06 01:28 PM, Loccie wrote: Thx for all the comments and I'm allready pretty far with this but I would like to know how I can make sure people can't post a very long word that messes up my page layout. In other words how do I add a max length to a word or even better move the pieces of the word that don't fit to the next line.
You could use my and gamescools' custom wordwrap2 function:
[thread] | [function]
(Visit Both)
...

Well I used that function but it's not working 100%. First of all that 4th function (Word flag - This is an optional parameter. If you set this to true, the function will never break during a word.) does not work properly. I tried this code:

echo wordwrap2($row[2], 75, '<br />', true);

but like this the code dit not break up the long words, so thats pretty useless. And look at the picture to see the strange way the long words are broken. I really hope you can help me fix this. (this pic was quickly saved in MS Paint and cut+shrunk to have a width of 500 so ignore the bad quality)

Loccie
Loccie
  • Member since: Feb. 27, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to bb code 2006-07-23 19:46:51 Reply

sorry forgot pic

bb code

Loccie
Loccie
  • Member since: Feb. 27, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to bb code 2006-07-24 04:20:13 Reply

Click Here , This is a link to the page and look in previous posts for the codes. You see that the first message is messed up because I typed wwww non-stop in 1 line and also notice that ddd and kkk end to soon. The <br />'s in the post are not really perfect either if you look closer you'll see that there are sometimes pieces of the <br /> in the post (r /> and <) this is probably what makes that text bold to. I tried my bb code size and i tags and they worked. You might see words broken up but thats another problem because if I put on true for never break up a word the long words also stay the same size. Can someone help me, this wordwarp2 is not working.

Loccie
Loccie
  • Member since: Feb. 27, 2004
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to bb code 2006-07-24 07:00:55 Reply

Well I found out what's give those pieces of <br />, the <br />'s from

$GuideContent = nl2br($GuideContent);

are being broken in pieces if I set that never break a word = to true it works fine but then the long words are not broken up. So I guess there is no other way then putting a max lenght on a word, my question is: How do I do that?