[SOLVED] Converting BBcode to HTML

This is a discussion on "[SOLVED] Converting BBcode to HTML" within the PHP Forum section. This forum, and the thread "[SOLVED] Converting BBcode to HTML are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Closed Thread
 
LinkBack Thread Tools
  #1  
Old Oct 5th, 2007, 21:19
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Converting BBcode to HTML

I am having problems with a php script that converts BBcode to HTML.

All the script works fine and converts the BBCode to HTML as expected apart from one......the [size] tag.

When I add the [size] tags to whatever and I preview the post, it doesn't do any styling at all. I can't see anything wrong with the regex so I don't know why it won't convert these tags.

PHP: Select all

<html><head>
<title>Editor</title>
<link rel="stylesheet" type="text/css" href="style2.css" />
</head><body>
<?php
   
//BBcode 2 HTML was written by WAY2WEB.net
   //It is free for you to use anywhere as long as you provide a link back to www.way2web.net
   
function BBCode($Text)
       {
         
// Replace any html brackets with HTML Entities to prevent executing HTML or script
            // Don't use strip_tags here because it breaks [ url ] search by replacing & with amp
            
$Text str_replace("<""&lt;"$Text);
            
$Text str_replace(">""&gt;"$Text);
            
// Convert new line chars to html <br /> tags
            
$Text nl2br($Text);
            
// Set up the parameters for a URL search string
            
$URLSearchString " a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'";
            
// Set up the parameters for a MAIL search string
            
$MAILSearchString $URLSearchString " a-zA-Z0-9\.@";
            
// Perform URL Search
            
$Text preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/"'<a href="$1" target="_blank">$1</a>'$Text);
            
$Text preg_replace("(\[url\=([$URLSearchString]*)\](.+?)\[/url\])"'<a href="$1" target="_blank">$2</a>'$Text);
            
//$Text = preg_replace("(\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[/url\])", '<a href="$1" target="_blank">$2</a>', $Text);
            // Perform MAIL Search
            
$Text preg_replace("(\[mail\]([$MAILSearchString]*)\[/mail\])"'<a href="mailto:$1">$1</a>'$Text);
            
$Text preg_replace("/\[mail\=([$MAILSearchString]*)\](.+?)\[\/mail\]/"'<a href="mailto:$1">$2</a>'$Text);
 
            
// Check for bold text
            
$Text preg_replace("(\[b\](.+?)\[\/b])is",'<span class="bold">$1</span>',$Text);
            
// Check for Italics text
            
$Text preg_replace("(\[i\](.+?)\[\/i\])is",'<span class="italics">$1</span>',$Text);
            
// Check for Underline text
            
$Text preg_replace("(\[u\](.+?)\[\/u\])is",'<span class="underline">$1</span>',$Text);
            
// Check for strike-through text
            
$Text preg_replace("(\[s\](.+?)\[\/s\])is",'<span class="strikethrough">$1</span>',$Text);
            
// Check for over-line text
            
$Text preg_replace("(\[o\](.+?)\[\/o\])is",'<span class="overline">$1</span>',$Text);
            
// Check for colored text
            
$Text preg_replace("(\[color=(.+?)\](.+?)\[\/color\])is","<span style=\"color: $1\">$2</span>",$Text);
            
// Check for sized text
            
$Text preg_replace("(\[size=(.+?)\](.+?)\[\/size\])is","<span style=\"font-size: $1%\">$2</span>",$Text);
            
// Check for list text
            
$Text preg_replace("/\[list\](.+?)\[\/list\]/is"'<ul class="listbullet">$1</ul>' ,$Text);
            
$Text preg_replace("/\[list=1\](.+?)\[\/list\]/is"'<ul class="listdecimal">$1</ul>' ,$Text);
            
$Text preg_replace("/\[list=i\](.+?)\[\/list\]/s"'<ul class="listlowerroman">$1</ul>' ,$Text);
            
$Text preg_replace("/\[list=I\](.+?)\[\/list\]/s"'<ul class="listupperroman">$1</ul>' ,$Text);
            
$Text preg_replace("/\[list=a\](.+?)\[\/list\]/s"'<ul class="listloweralpha">$1</ul>' ,$Text);
            
$Text preg_replace("/\[list=A\](.+?)\[\/list\]/s"'<ul class="listupperalpha">$1</ul>' ,$Text);
            
$Text str_replace("[*]""<li>"$Text);
            
// Check for font change text
            
$Text preg_replace("(\[font=(.+?)\](.+?)\[\/font\])","<span style=\"font-family: $1;\">$2</span>",$Text);
            
// Declare the format for [ code ]  layout
            
$CodeLayout '<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td class="quotecodeheader"> Code:</td>
                                </tr>
                                <tr>
                                    <td class="codebody">$1</td>
                                </tr>
                           </table>'
;
            
// Check for [ code ] text
            
$Text preg_replace("/\[code\](.+?)\[\/code\]/is","$CodeLayout"$Text);
            
// Declare the format for [ php ] layout
            
$phpLayout '<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td class="quotecodeheader"> Code:</td>
                                </tr>
                                <tr>
                                    <td class="codebody">$1</td>
                                </tr>
                           </table>'
;
            
// Check for [ php ] text
            
$Text preg_replace("/\[php\](.+?)\[\/php\]/is",$phpLayout$Text);
            
// Declare the format for [ quote ] layout
            
$QuoteLayout '<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td class="quotecodeheader"> Quote:</td>
                                </tr>
                                <tr>
                                    <td class="quotebody">$1</td>
                                </tr>
                           </table>'
;
 
            
// Check for [ quote ] text
            
$Text preg_replace("/\[quote\](.+?)\[\/quote\]/is","$QuoteLayout"$Text);
 
            
// Images
            // [img]pathtoimage[/img]
            
$Text preg_replace("/\[img\](.+?)\[\/img\]/"'<img src="$1">'$Text);
 
            
// [img=widthxheight]image source[/img]
            
$Text preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.+?)\[\/img\]/"'<img src="$3" height="$2" width="$1">'$Text);
 
           return 
$Text;
      }
 
$text BBCode($_POST['post']);
echo 
$text;
?>
</body>
</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!

  #2  
Old Oct 6th, 2007, 04:28
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Converting BBcode to HTML

Hi Adrock,

The replace ie. style="font-size: $1"

what is being passed with the [size=x] tags? If is's just an integer I don't think this is a valid font-size value.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #3  
Old Oct 6th, 2007, 10:16
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Converting BBcode to HTML

Hello Rakuli

You helped me out with this before in the JavaScript forum.

What is being passed is [size="150%"] for example. They go from 50%, 75%, 100%, 150% and 200%

I have tried changing the form option values to pt and px but it still doesn't work.

If you need to see the JavaScript or the form, i'll post the code
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #4  
Old Oct 6th, 2007, 10:21
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Converting BBcode to HTML

Okay, it looks like you're capturing the whole pattern -- size=(.+?)

This would then create a span that looks like <span style="font-size:"150%"">etc...

Can you try removing the quotes from around the size?
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #5  
Old Oct 6th, 2007, 10:34
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Converting BBcode to HTML

This is part of the form that does the size option

HTML: Select all
<select name="fontsize" onChange="font('[size=' + this.form.fontsize.options[this.form.fontsize.selectedIndex].value + ']', '[/size]'); this.selectedIndex=2;" onMouseOver="helpline('fontsize')" class="form_elements_dropdown">
                                <option value="50%" >Tiny</option>
                                <option value="75%" >Small</option>
                                <option value="100%" selected >Normal</option>
                                <option value="150%" >Large</option>
                                <option  value="200%" >Huge</option>
                        </select>
I can't really see where those extra quotes are too so if you can point them out to me i'll get rid of them..... Thanks Rakuli
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #6  
Old Oct 6th, 2007, 10:49
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Converting BBcode to HTML

Hi Adrock, I just tracked down the old javascript thread http://www.webforumz.com/javascript-...ext-editor.htm.

I went to the page and when it is written to the page in the preview, it is writing front-size: xx%px, a percentage and pixels.

Have you just changed that recently?
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #7  
Old Oct 6th, 2007, 10:52
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Converting BBcode to HTML

You could also try changing the form inputs to absolute values

xx-small, x-small, normal, x-large, xx-large

There is nothing wrong with the PHP becuase it is replacing as required.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #8  
Old Oct 6th, 2007, 10:52
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Converting BBcode to HTML

No...this is exactly how it came when i downloaded it.

I got the source code for the editor and the converter from one place but the converter wasn't good so i got another one from somewhere else
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #9  
Old Oct 7th, 2007, 12:47
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Converting BBcode to HTML

I tried changing the form select values to 8px, 9px, 10px, 12px and 14px and when I preview, all i get is a few dashes.

I have also tried using pts too and even tried using xx-small etc but none of it works
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #10  
Old Oct 9th, 2007, 08:40
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Converting BBcode to HTML

I have fixed it .....woohoo ....and it was so obvious but I couldn't see it for looking

I had to take the % sign out of this
PHP: Select all

$Text preg_replace("(\[size=(.+?)\](.+?)\[\/size\])is","<span style=\"font-size: $1%\">$2</span>",$Text); 

to make
PHP: Select all

$Text preg_replace("(\[size=(.+?)\](.+?)\[\/size\])is","<span style=\"font-size: $1\">$2</span>",$Text); 

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
  #11  
Old Oct 9th, 2007, 08:53
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Converting BBcode to HTML

LOL, I saw that % sign in the outputted code but didn't even think to see if it was in the preg replace (I was thinking it was a format part like in sprintf)

Soory bout that, glad you sorted it.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Closed Thread

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] converting flash to html Posie Starting Out 3 Dec 2nd, 2007 20:04
Function to convert BBcode to HTML AdRock PHP Forum 4 Sep 2nd, 2007 12:41
Converting from HTML to XHTML Aaron1988 Web Page Design 1 Dec 4th, 2006 15:09
help with converting HTML to XHTML sing2trees Web Page Design 4 Sep 20th, 2006 03:35
I'm converting my pages from HTML to CSS,,, Help me! ahm531 Web Page Design 9 Sep 6th, 2006 10:06


All times are GMT. The time now is 02:33.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2