SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: HTML -> BBcode

  1. #1
    SitePoint Addict mserms's Avatar
    Join Date
    Jun 2001
    Location
    Scotland
    Posts
    230
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    HTML -> BBcode

    I'm using this class for creating BBCode - http://www.phpclasses.org/browse/package/951.html

    Anyone know of anything similar to convert BBCode back to HTML?

  2. #2
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    to convert bbcode abck to html, just use a series of str_replace and preg_replace

    e.g
    PHP Code:
    //bold
    str_replace("[*b*]","<*strong*>",$string);

    //bold
    str_replace("[*/b*]","<*/strong*>",$string); 
    without the *

    and so on.

    use preg_replace for [URL] tags

  3. #3
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    found something for ya:

    [php]<?php
    function parsebbcode($message) {
    $message = stripslashes($message);
    $message = htmlspecialchars($message, ENT_QUOTES);
    /* [quote] */
    $message = str_replace ("
    ","<blockquote><span style=\"font-size : small;\">quote/span><hr />",$message);
    $message = str_replace ("
    ","<hr /></blockquote>",$message);
    /* [img] */
    $message = str_replace ("[IMG]","<img src=\"", $message);
    $message = str_replace ("[/IMG]","\" border=\"0\">",$message);
    /* [b] */
    $message = str_replace ("","<b>",$message);
    $message = str_replace ("
    ","</b>",$message);
    /* [i] */
    $message = str_replace ("","<i>",$message);
    $message = str_replace ("
    ","</i>",$message);
    /* [u] */
    $message = str_replace ("","<u>",$message);
    $message = str_replace ("
    ","</u>",$message);
    /* [ul] [li] */
    $message = str_replace ("[ul]","<ul>",$message);
    $message = str_replace ("[/ul]","</ul>",$message);
    $message = str_replace ("[li]","<li>",$message);
    $message = str_replace ("[/li]","</li>",$message);
    /* [center] */
    $message = str_replace ("
    ","<div align=\"center\">",$message);
    $message = str_replace ("
    ","</div>",$message);
    /* [code] */
    $message = str_replace ("
    Code:
    ","<blockquote><pre>\n",$message);
    	$message = str_replace ("
    ","</pre></blockquote>\n",$message);
    /* [\\] Zeilenumbruch in [code] */
    $message = str_replace ("[\\\\]",'\\\\'."\n",$message);
    /* automatische Linkgenerierung */
    $message = preg_replace ("{(^|[^\"=]{1})(http://|ftp://|mailto:|news([^\s<>]+)([\s\n<>]|$)}sm", "\\1<a href=\"\\2\\3\" target=\"_blank\">\\2\\3</a>\\4", $message);
    /* [url] */
    $message = preg_replace ("{(\[URL\])([A-Za-z0-9_~&=;\?:%@#./\-]+[A-Za-z0-9/])}si", "<a href=\"http://\\2\" target=\"_blank\">\\2", $message);
    $message = preg_replace ("{(\+[A-Za-z0-9/])(\])}si", "<a href=\"\\2\" target=\"_blank\">", $message);
    $message = str_replace ("
    ", "</a>", $message);
    /*
    PHP Code:
     */
    //    $message = preg_replace_callback("#\[php\](.*)\[\/php\]#Usi","php_syntax", $message);
        
        /* [LIST] */
        
    $message str_replace ("[LIST]""<ul>"$message);
        
    $message str_replace ("[/LIST]""</ul>"$message);
        
        
    /*  */
        
    $message str_replace ("[*]""<li>"$message);
        
    $message str_replace ("[/*]""</li>"$message);
        
        
    /* nl2br() */
        
    $message str_replace ("\n""<br />"$message);

        
        return 
    $message;
    }
    ?> 
    there are a feww characters missing since the browser is parsing them as html tags....just quote me and you'll have the right function.

  4. #4
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Michigan
    Posts
    260
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [QUOTE=astericks]found something for ya:

    [php]<?php
    function parsebbcode($message) {
    $message = stripslashes($message);
    $message = htmlspecialchars($message, ENT_QUOTES);
    /*
    */
    $message = str_replace ("","<hr /></blockquote>",$message);
    /* [img] */
    $message = str_replace ("[IMG]","<img src=\"", $message);
    $message = str_replace ("[/IMG]","\" border=\"0\">",$message);
    /* [b]*/
    $message = str_replace ("","<b>",$message);
    $message = str_replace ("
    ","</b>",$message);
    /* [i]*/
    $message = str_replace ("","<i>",$message);
    $message = str_replace ("
    ","</i>",$message);
    /* [u]*/
    $message = str_replace ("","<u>",$message);
    $message = str_replace ("
    ","</u>",$message);
    /* [ul] [li] */
    $message = str_replace ("[ul]","<ul>",$message);
    $message = str_replace ("[/ul]","</ul>",$message);
    $message = str_replace ("[li]","<li>",$message);
    $message = str_replace ("[/li]","</li>",$message);


    /* [center]*/

    $message = str_replace ("
    ","<div align=\"center\">",$message);
    $message = str_replace ("

    ","</div>",$message);

    /* [code] */
    $message = str_replace ("
    Code:
    ","<blockquote><pre>\n",$message);
    	$message = str_replace ("
    ","</pre></blockquote>\n",$message);
    /* [\\] Zeilenumbruch in [code] */
    $message = str_replace ("[\\\\]",'\\\\'."\n",$message);
    /* automatische Linkgenerierung */
    $message = preg_replace ("{(^|[^\"=]{1})(http://|ftp://|mailto:|news([^\s<>]+)([\s\n<>]|$)}sm", "\\1<a href=\"\\2\\3\" target=\"_blank\">\\2\\3</a>\\4", $message);
    /* [url="http://*/</p><p%20style="]*/
    $message = preg_replace ("{(\[URL\])([A-Za-z0-9_~&=;\?:%@#./\-]+[A-Za-z0-9/])}si", "<a href=\"http://\\2\" target=\"_blank\">\\2", $message);
    $message = preg_replace ("{(\+[A-Za-z0-9/])(\])}si", "<a href=\"\\2\" target=\"_blank\">", $message);
    $message = str_replace ("
    ", "</a>", $message);
    /*
    PHP Code:
     */
    //    $message = preg_replace_callback("#\[php\](.*)\[\/php\]#Usi","php_syntax", $message);
     
        /*
    [LIST] */     
    $message str_replace ("[list][*]""<ul>"$message);
        
    $message str_replace ("[/list]""</ul>"$message);
     
        
    /* */
        
    $message str_replace ("[*]""<li>"$message);
        
    $message str_replace ("[/*]""</li>"$message);
     
        
    /* nl2br() */
        
    $message str_replace ("\n""<br />"$message);
     
     
        return 
    $message;
    }
    ?> 
    there are a feww characters missing since the browser is parsing them as html tags....just quote me and you'll have the right function.
    *QUOTE*

  5. #5
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, these replies look great in Firebird..

  6. #6
    My precious!!! astericks's Avatar
    Join Date
    Mar 2002
    Location
    Vancouver, BC
    Posts
    1,971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not quote and reply, just quote so that you get all the text in my [*php] tags

  7. #7
    SitePoint Addict mserms's Avatar
    Join Date
    Jun 2001
    Location
    Scotland
    Posts
    230
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys. I actually ended up writing my own function to convert back to HTML. I really need to learn regular expressions...


Bookmarks

Posting Permissions

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