...

View Full Version : Style Code In Guestbook



mark87
06-12-2005, 12:38 PM
I've modified a flat-file guestbook extensively, now I'd like to add some more features - starting off with support for styling messages (like BBCode vB Code etc). Any ideas how - I'm VERY new to PHP - Guess I'd need a way to convert all [B ]'s to <strong>, and [I ]'s to <em> etc. Any help appreciated. :)

delinear
06-12-2005, 01:10 PM
If you just want to let your users physically type in the codes then that's a fairly simple solution, you can just read the passage into a variable and use str_replace to change the relevant sections.

$string = str_replace('', '<strong>', $string);
$string = str_replace('', '</strong>', $string);

$string = str_replace('', '<em>', $string);
$string = str_replace('', '</em>', $string);

$string = str_replace('', '<font color="red">', $string);
$string = str_replace('', '</font>', $string);

// etc

Fou-Lu
06-12-2005, 02:46 PM
The only bad thing about that, is that you could end up with open tags. Not a huge huge deal, but still important enough.
Perhaps you should look into using a preg instead, though this depends entirely on what you are planning to allow. For something like <b> and <i>, then yeah, str_replace is a far better idea. Once you enter the domain of urls, mailto:s, ul/ol, etc, you need to look into the replacement patterns.
As for saving it, leave it as a bbcode that has been entered, and output on page request - don't save it as html, incase you ever do/need to adjust the coding.

mark87
06-12-2005, 05:24 PM
Thanks I'll give that a go.

What did you mean by 'As for saving it, leave it as a bbcode that has been entered, and output on page request - don't save it as html, incase you ever do/need to adjust the coding.' ?

EDIT - Nice, got that working for bold and italic, now I'll figure out the rest myself :P

... and I see what happens if a tag is not closed... it's not pretty! anyway I can easily stop that happening?

Fou-Lu
06-12-2005, 06:39 PM
Hmm, I always use regular expressions for any type of markup that I allow, I can give you some code if you want, or perhaps someone here may have another idea of getting around this.
As for saving, I mean whatever method you are storing your information in, leave it with the [b ]stuff [ /b] tags in it. Don't save it as outputted html.

mark87
06-12-2005, 06:45 PM
Still don't understand! Perhaps I'm just too tired!

The messages are stored in a flat file, when the data from the posting page is sent to the file - that's when I'm converting the BBCode to HTML. Did you mean I should convert the BBCode to HTML when the messages are being placed onto the guestbook page? So they are stored in the flat file with BBCode?

Kurashu
06-12-2005, 06:54 PM
//http://smarty.incutio.com/?page=BBCodePlugin
function BBCode ($message)
{
$preg = array(
'/(?<!\\\\)\[color(?::\w+)?=(.*?)\](.*?)\[\/color(?::\w+)?\]/si' => "<span style=\"color:\\1\">\\2</span>",
'/(?<!\\\\)\[size(?::\w+)?=(.*?)\](.*?)\[\/size(?::\w+)?\]/si' => "<span style=\"font-size:\\1\">\\2</span>",
'/(?<!\\\\)\[font(?::\w+)?=(.*?)\](.*?)\[\/font(?::\w+)?\]/si' => "<span style=\"font-family:\\1\">\\2</span>",
'/(?<!\\\\)\[align(?::\w+)?=(.*?)\](.*?)\[\/align(?::\w+)?\]/si' => "<div style=\"text-align:\\1\">\\2</div>",
'/(?<!\\\\)\[b(?::\w+)?\](.*?)\[\/b(?::\w+)?\]/si' => "<span style=\"font-weight:bold\">\\1</span>",
'/(?<!\\\\)\[i(?::\w+)?\](.*?)\[\/i(?::\w+)?\]/si' => "<span style=\"font-style:italic\">\\1</span>",
'/(?<!\\\\)\[u(?::\w+)?\](.*?)\[\/u(?::\w+)?\]/si' => "<span style=\"text-decoration:underline\">\\1</span>",
'/(?<!\\\\)\[center(?::\w+)?\](.*?)\[\/center(?::\w+)?\]/si' => "<div style=\"text-align:center\">\\1</div>",

// [code] & [php]
'/(?<!\\\\)\[code(?::\w+)?\](.*?)\[\/code(?::\w+)?\]/si' => "<div class=\"code\"><span>Code:</span><code>\\1</code></div>",
'/(?<!\\\\)\[php(?::\w+)?\](.*?)\[\/php(?::\w+)?\]/si' => "<div class=\"php\"><span>Code:</span><code>\\1</code></div>",

// [email]
'/(?<!\\\\)\[email(?::\w+)?\](.*?)\[\/email(?::\w+)?\]/si' => "<a href=\"mailto:\\1\" class=\"bb-email\">\\1</a>",
'/(?<!\\\\)\[email(?::\w+)?=(.*?)\](.*?)\[\/email(?::\w+)?\]/si' => "<a href=\"mailto:\\1\" class=\"bb-email\">\\2</a>",

// [url]
'/(?<!\\\\)\[url(?::\w+)?\](.*?)\[\/url(?::\w+)?\]/si' => "<a href=\"http://\\1\" target=\"_blank\">\\1</a>",
'/(?<!\\\\)\[url(?::\w+)?\](.*?)\[\/url(?::\w+)?\]/si' => "<a href=\"\\1\" target=\"_blank\">\\1</a>",
'/(?<!\\\\)\[url(?::\w+)?=(.*?)?\](.*?)\[\/url(?::\w+)?\]/si' => "<a href=\"\\1\" target=\"_blank\">\\2</a>",

// [img]
'/(?<!\\\\)\[img(?::\w+)?\](.*?)\[\/img(?::\w+)?\]/si' => "<img src=\"\\1\" alt=\"\\1\" />",
'/(?<!\\\\)\[img(?::\w+)?=(.*?)x(.*?)\](.*?)\[\/img(?::\w+)?\]/si' => "<img width=\"\\1\" height=\"\\2\" src=\"\\3\" alt=\"\\3\" />",


// [quote]
'/(?<!\\\\)\[quote(?::\w+)?\](.*?)\[\/quote(?::\w+)?\]/si' => "<div class=\"quote\"><span>Quote:</span><blockquote>"1</blockquote></div>",
'/(?<!\\\\)\[quote(?::\w+)?=(?:&quot;|"|')?(.*?)["\']?(?:&quot;|"|\')?\](.*?)\[\/quote\]/si' => "<div class=\"quote\"><span>Quote \\1:</span><blockquote>\\2</blockquote></div>",

//[list]
'/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\*(?::\w+)?\](.*?)(?=(?:\s*<br\s*\/?>\s*)?\[\*|(?:\s*<br\s*\/?>\s*)?\[\/?list)/si' => "\n<li>\\1</li>",
'/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/list(:(?!u|o)\w+)?\](?:<br\s*\/?>)?/si' => "\n</ul>",
'/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/list:u(:\w+)?\](?:<br\s*\/?>)?/si' => "\n</ul>",
'/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/list:o(:\w+)?\](?:<br\s*\/?>)?/si' => "\n</ol>",
'/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(:(?!u|o)\w+)?\]\s*(?:<br\s*\/?>)?/si' => "\n<ul>",
'/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list:u(:\w+)?\]\s*(?:<br\s*\/?>)?/si' => "\n<ul>",
'/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list:o(:\w+)?\]\s*(?:<br\s*\/?>)?/si' => "\n<ol>",
'/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(?::o)?(:\w+)?=1\]\s*(?:<br\s*\/?>)?/si' => "\n<ol>",
'/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(?::o)?(:\w+)?=i\]\s*(?:<br\s*\/?>)?/s' => "\n<ol>",
'/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(?::o)?(:\w+)?=I\]\s*(?:<br\s*\/?>)?/s' => "\n<ol>",
'/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(?::o)?(:\w+)?=a\]\s*(?:<br\s*\/?>)?/s' => "\n<ol>",
'/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(?::o)?(:\w+)?=A\]\s*(?:<br\s*\/?>)?/s' => "\n<ol>",

// escaped tags like \[b], \[color], \[url], ...
'/\\\\(\[\/?\w+(?::\w+)*\])/' => "\\1"
);
$message = preg_replace(array_keys($preg), array_values($preg), $message);
return $message;
}


There is a good BBCode function. Works nicely too.

mark87
06-12-2005, 07:03 PM
Wow thanks! :thumbsup:

Don't think I'll actually use it totally but I'll definately have a good look through - already implemented text formatting and images myself anyway. :)

mark87
06-12-2005, 08:34 PM
Got a lot working now, but major problems if tags aren't closed! eg. I've made 'quote' and 'code' tags which place the content into div's like so -



$vcomment = str_replace('[ code]', '<span style=\"font-size:small; font-weight:bold\">Code:</span>
<div style=\"font-family:\'courier new\'; background-color:#FFF; font-size:medium; border:1px solid #000; padding:10px\">', $vcomment);
$vcomment = str_replace('[/ code]', '</div>', $vcomment);


If the tag isn't closed it can result in the whole page being 'quoted' or in 'code'! :( Oh well, I'm happy with what I've done so far. :p

Oh - is there a way so the tags don't have to be case-sensitive - I tried adding 'strtolower' like so, but it turned the whole message into lowercase!

$vcomment = strtolower(str_replace('[/ code]', '</div>', $vcomment));

... I'm an idiot. I just realised as I've written that perhaps I should've tried something like -

$vcomment = str_replace(strtolower('[/ code]'), '</div>', $vcomment);

Don't know if that's exactly right... will have to test.

delinear
06-13-2005, 10:30 AM
str_ireplace() instead of str_replace() ignores case but Kurashu's preg_replace solution would still be better as it will prevent tags being left open.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum