Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Senior Coder
    Join Date
    Dec 2004
    Location
    Essex, UK
    Posts
    2,636
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Style Code In Guestbook

    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.

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    PHP Code:
    $string str_replace('[b]''<strong>'$string);
    $string str_replace('[/b]''</strong>'$string);

    $string str_replace('[i]''<em>'$string);
    $string str_replace('[/i]''</em>'$string);

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

    // etc 

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Dec 2004
    Location
    Essex, UK
    Posts
    2,636
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    Last edited by mark87; 06-12-2005 at 06:02 PM.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #6
    Senior Coder
    Join Date
    Dec 2004
    Location
    Essex, UK
    Posts
    2,636
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #7
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    [PHP]
    //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:
                '/(?<!\\\\)\[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.

  • #8
    Senior Coder
    Join Date
    Dec 2004
    Location
    Essex, UK
    Posts
    2,636
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow thanks!

    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.

  • #9
    Senior Coder
    Join Date
    Dec 2004
    Location
    Essex, UK
    Posts
    2,636
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 -

    Code:
    $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.

    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.

  • #10
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •