BBcode to HTML

Discussion in 'PHP' started by shamess, Jun 26, 2006.

  1. #1
    I own forums which I put news into which I put onto the front page of my website. But at the moment I'm stuck with the horible BBcode tags, so I guess I'll need to use regex (ugh) to get ride of them and replace them with the correct HTML.

    Anyone have any guidence on where to start with learning to use regex, since PHP manuals don't give any of the more complex syntax. I'm pretty sure if I looked around I could find someone to just give me the coding I need, but I'd rather work it out myself and learn by doing.
     
    shamess, Jun 26, 2006 IP
  2. vishwaa

    vishwaa Well-Known Member

    Messages:
    271
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    138
    #2
    vishwaa, Jun 27, 2006 IP
    shamess likes this.
  3. shamess

    shamess Well-Known Member

    Messages:
    1,127
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    185
    #3
    That's great, but for some reason phpBB puts a random unique code within the bbCode (http://www.allroundnews.co.uk]) so that wouldn't work much.
     
    shamess, Jun 27, 2006 IP
  4. vishwaa

    vishwaa Well-Known Member

    Messages:
    271
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    138
    #4
    I think amit patel is using such script for his forum hiifii.com. He will be happy to help you.
     
    vishwaa, Jun 27, 2006 IP
  5. abuzant

    abuzant Well-Known Member

    Messages:
    956
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    140
    #5
    abuzant, Jun 27, 2006 IP
  6. dtang4

    dtang4 Active Member

    Messages:
    303
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #6
    dtang4, Jun 27, 2006 IP
  7. shamess

    shamess Well-Known Member

    Messages:
    1,127
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    185
    #7
    <?php
    
    $string = "[b:e3e53cad2b]Rawr, ^^;;";
    
    $string = preg_replace("/(\[b:[a-zA-Z0-9]{0-10}\])/", "<b>", $string);
    
    echo $string;
    
    ?>
    PHP:
    What's wrong with that..? I was hoping the [b:....] would be replaced with <b>.
     
    shamess, Jun 28, 2006 IP
  8. dtang4

    dtang4 Active Member

    Messages:
    303
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #8
    What's your output?

    I dont think you need the parentheses.
     
    dtang4, Jun 29, 2006 IP
  9. shamess

    shamess Well-Known Member

    Messages:
    1,127
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    185
    #9
    The output is still [b:e3e53cad2b]Rawr, ^^;; nothing changes, airgo the preg doesn't work >.<
     
    shamess, Jun 29, 2006 IP
  10. dtang4

    dtang4 Active Member

    Messages:
    303
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #10
    Hey, I think the syntax {0-10} might be wrong. Your code works if you used {0,10} instead.
     
    dtang4, Jun 29, 2006 IP
  11. shamess

    shamess Well-Known Member

    Messages:
    1,127
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    185
    #11
    Thanks dtang4 ^^;;

    So I have the [b:4d785e5de5] converted to <b>. But now it doesn't seem to like me doing this:

    <?php
    
    $string = "[b:e3e53cad2b]Rawr, ^^;;[/b:5d5e887ftg]";
    
    $string = preg_replace("/(\[b:[a-zA-Z0-9]{0,10}\])/", "<b>", $string);
    $string = preg_replace("/(\[/b:[a-zA-Z0-9]{0,10}\])/", "<b>", $string);
    
    echo $string;
    
    ?>
    PHP:
    Returns: Warning: preg_replace(): Unknown modifier 'b' in /home/httpd/vhosts/allroundnews.co.uk/subdomains/sheep/httpdocs/test.php on line 6

    I need to convert [/b:5d5e887ftg] into </b>, but the backslash confuses the PHP, so I'm not sure how to escape it.
     
    shamess, Jun 30, 2006 IP
  12. vishwaa

    vishwaa Well-Known Member

    Messages:
    271
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    138
    #12
    Hope this should help you.

    <?php
    $str = "[b:e3e53cad2b]Rawr, ^^;;[/b:5d5e887ftg]";
    $str = preg_replace("/\[([biu])(:[a-zA-Z0-9]{0,10})*\]/i","<\\1>",$str);
    $str = preg_replace("/\[\/([biu])(:[a-zA-Z0-9]{0,10})*\]/i","</\\1>",$str);
    echo $str;
    ?>
    PHP:
     
    vishwaa, Jun 30, 2006 IP
  13. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #13
    Just use parse_bbcode built in vB. No need to reinvent the wheel! (assuming you have vbulletin)

    If not, your forum sofware certainly has a similar function.

    
    	/**
    	* Parse an input string with BB code to a final output string of HTML
    	*
    	* @param	string	Input Text (BB code)
    	* @param	bool	Whether to parse smilies
    	* @param	bool	Whether to allow HTML (for smilies)
    	*
    	* @return	string	Ouput Text (HTML)
    	*/
    	function parse_bbcode($input_text, $do_smilies, $do_html = false)
    
    PHP:
     
    noppid, Jun 30, 2006 IP
  14. shamess

    shamess Well-Known Member

    Messages:
    1,127
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    185
    #14
    I'm not using forum soft ware, I'm just using bbCode as the submission code for my website since I want to strip HTML tags ^^;;
     
    shamess, Jun 30, 2006 IP
  15. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #15
    Ah, well, then I guess the other ideas will have to be developed.

    Rip the BBcode functions out of a GPL forum sofware package. ;)
     
    noppid, Jun 30, 2006 IP
    shamess likes this.
  16. shamess

    shamess Well-Known Member

    Messages:
    1,127
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    185
    #16
    *explodes* Thank you xD

    PS. I would give you more reputation, but i need to spread the love first.
     
    shamess, Jun 30, 2006 IP
    vishwaa likes this.