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).

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.