Jump to content

code your own bb codes


gordsmash

Recommended Posts

Why would preg_replace be better? If gordsmash just wishes to substitute one string for the other surely str_replace would be better.

If you were doing str_replace you'd do something like this:

 

$text = str_replace(Array('[b]', '[/b]'), Array('<b>', '</b>'), $text);

 

Now say if someone entered this text:

 

[b]Hello world

 

There would be no closing </b> tag, which can cause problems (be it display problems or validation issues).

Link to comment
Share on other sites

Here is some sample code I have laying around. You can add your own options by modifying $patters and $replacements accordingly

 

<?php

//bbcode

$patterns = array(

  //BB Code
  "/\[[b]\](.*?)\[\/b\]/is",
  "/\[[i]\](.*?)\[\/i\]/is",
  "/\[[u]\](.*?)\[\/u\]/is",
  "/\[[s]\](.*?)\[\/s\]/is",
  "/\[marquee\](.*?)\[\/marquee\]/is",
  "/\[url\](.*?)\[\/url\]/is",
  "/\[url=(.*?)\](.*?)\[\/url\]/is",
  "/\[img\](.*?)\[\/img\]/is",
  "/\[quote\](.*?)\[\/quote\]/is",
  "/\[(size|color)=(.*?)\](.*?)\[\/\\1\]/is",
  "/\[br\]/i",

  //Emoticons
  "/ \:\) /",
  "/ \:\( /",
  "/ \ /",
  "/ \ /",
  "/ \:\| /",
  "/ \ /",
  "/ \:\? /",
  "/ \;\) /");

$replacements = array(

  //BB Code
  "<b>\\1</b>",
  "<i>\\1</i>",
  "<u>\\1</u>",
  "<s>\\1</s>",
  "<marquee>\\1</marquee>",
  "<a href=\"\\1\">\\1</a>",
  "<a href=\"\\1\" target=\"_blank\">\\2</a>",
  "<img border=\"0\" src=\"\\1\">",
  "<div><b>Quote:</b> <i>\\1</i></div>",
  "<font \\1=\"\\2\">\\3</font>",
  "<br />",

  //Emoticons
  " <img src=\"smilies/happy.gif\" border=\"0\"> ",
  " <img src=\"smilies/angry.gif\" border=\"0\"> ",
  " <img src=\"smilies/omg.gif\" border=\"0\"> ",
  " <img src=\"smilies/tounge.gif\" border=\"0\"> ",
  " <img src=\"smilies/dry.gif\" border=\"0\"> ",
  " <img src=\"smilies/biggrin.gif\" border=\"0\"> ",
  " <img src=\"smilies/confused.gif\" border=\"0\"> ",
  " <img src=\"smilies/wink.gif\" border=\"0\"> "
);

$string = "[b]This is bold text[/b] [i]this is italic text[/i] [b][i]This is bold, italic text[/b][/i]";

$result = preg_replace($patterns,$replacements,$string);

echo $result;

?>

 

EDIT: had to remove the BBCode optins for [ code ] as it was screwing up my post.

Link to comment
Share on other sites

Why would preg_replace be better? If gordsmash just wishes to substitute one string for the other surely str_replace would be better.

If you were doing str_replace you'd do something like this:

 

$text = str_replace(Array('[b]', '[/b]'), Array('<b>', '</b>'), $text);

 

Now say if someone entered this text:

 

[b]Hello world

 

There would be no closing </b> tag, which can cause problems (be it display problems or validation issues).

 

Not only that, there is no way you could use str_replace() to parse something like "Go here" into a valid link without regular expressions. Well, I suppose you could do it with a lot of various scripts to process the text multiple times, but it would be pointless since you can do it very easily with regular expressions.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.