Solved

How to fix bug in bbcode .

Posted on 2012-03-29
9
229 Views
Last Modified: 2012-04-18
Hello

i make code my forums, i use bbcode and found bug <br />
how to not add <br / > in

Open in new window


        function bbcode_format ($str) {  
           // $str = htmlentities($str);  ¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿
              $str = htmlspecialchars($str);
          
            $simple_search = array(  
                        //added line break  
                        '/\[br\]/is',  
                        '/\[b\](.*?)\[\/b\]/is',  
                        '/\[i\](.*?)\[\/i\]/is',  
                        '/\[u\](.*?)\[\/u\]/is',  
                        '/\[url\=(.*?)\](.*?)\[\/url\]/is',  
                        '/\[url\](.*?)\[\/url\]/is',  
                        '/\[align\=(left|center|right)\](.*?)\[\/align\]/is',  
                        '/\[img\](.*?)\[\/img\]/is',  
                        '/\[mail\=(.*?)\](.*?)\[\/mail\]/is',  
                        '/\[mail\](.*?)\[\/mail\]/is',  
                        '/\[font\=(.*?)\](.*?)\[\/font\]/is',  
                        '/\[size\=(.*?)\](.*?)\[\/size\]/is',  
                        '/\[color\=(.*?)\](.*?)\[\/color\]/is',  
                          //added textarea for code presentation  
                       '/\[codearea\](.*?)\[\/codearea\]/is',  
                         //added pre class for code presentation  
                      '/\[code\](.*?)\[\/code\]/is',  
                        //added paragraph  
                      '/\[p\](.*?)\[\/p\]/is',  
                        );  
          
            $simple_replace = array(  
                        //added line break  
                       '<br />',  
                        '<strong>$1</strong>',  
                        '<em>$1</em>',  
                        '<u>$1</u>',  
                        // added nofollow to prevent spam  
                        '<a href="$1" rel="nofollow" title="$2 - $1"  target="_blank">$2</a>',  
                        '<a href="$1" rel="nofollow" title="$1"  target="_blank">$1</a>',  
                        '<div style="text-align: $1;">$2</div>',  
                        //added alt attribute for validation  
                        '<a href="$1" rel="nofollow" target="_blank"><img  class="thumb" src="$1" alt="http://www.ai4investment.com" /></a>',  
                        '<a href="mailto:$1"  target="_blank">$2</a>',  
                        '<a href="mailto:$1"  target="_blank">$1</a>',  
                        '<span style="font-family: $1;">$2</span>',  
                        '<span style="font-size: $1;">$2</span>',  
                        '<span style="color: $1;">$2</span>',  
                        //added textarea for code presentation  
                        '<textarea class="code_container" rows="30" cols="70">$1</textarea>',  
                        //added pre class for code presentation  
                        '<pre class="brush: cpp">$1</pre>',  
                        //added paragraph  
                        '<p>$1</p>',  
                        );  
          
            // Do simple BBCode's  
            $str = preg_replace ($simple_search, $simple_replace, $str);  
          
            // Do <blockquote> BBCode  
            $str = $this->bbcode_quote ($str);  
            $bbcode = nl2br($str);
          
            return $bbcode;  
        }     

Open in new window

0
Comment
Question by:XSoFTz
  • 5
  • 4
9 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 37783445
> ... how to not add <br / >
remove first array element in both arrays
0
 

Author Comment

by:XSoFTz
ID: 37785179
i mean

$bbcode = nl2br($str);

Open in new window


but i need add nl2br then check in bbcode [ code ][ /code ] and not add nl2br ?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 37785864
hmm, don't know what nl2br() does, assuming that it converts 0x0a to <br > then according your question:
> ... how to not add <br / >
simply do not call it

may be I don't understand what you want to archive, but your description is very vage ...
0
 

Author Comment

by:XSoFTz
ID: 37785888
about code it work but.

in tag code

[ code ]
if()
{

}
[ /code ]

PHP Code Line :

$bbcode = nl2br($str);

Output :

[ code ]
if()<br />
{<br />
<br />
}<br />
[ /code ]

it add <br /> in code. but i not need <br /> add in tag code

then not use nl2br() function, in tag \n not work display on html code.
0
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 51

Expert Comment

by:ahoffmann
ID: 37785928
you need to parse your code and exclude those parts which you define as code from being passed to nl2br()

You need a good parser for that as it cannot simply be done with regex, see this example

[ code ]
if(x=="[ /code ]"){
  bang="\
[ code ]  \
";
/*
[ /code ]
*/
hidden=true;
}
[ /code ]
0
 

Author Comment

by:XSoFTz
ID: 37786034
not so it .

see

DisplayHtml Code
i not get <br /> in tag [ code ]

in bbcode :

[ code ]
int start() {
   if (gs_108 != gs_100) return (0);
   int ind_counted_0 = IndicatorCounted();
   int li_8 = Bars - ind_counted_0;
   if (li_8 > 1) li_8 = Bars - 3;
   double ld_12 = p / 100.0;
   for (int li_4 = li_8; li_4 >= 0; li_4--) {
      if (Close[li_4] < gd_132) {
         gd_132 = Close[li_4];
         gd_148 = gd_132 * (2.0 * ld_12 + 1.0);
         gd_140 = gd_132 * (ld_12 + 1.0);
      }
      if (Close[li_4] > gd_148) {
         gd_148 = Close[li_4];
         gd_132 = gd_148 * (1 - 2.0 * ld_12);
         gd_140 = gd_148 * (1 - ld_12);
      }
      g_ibuf_120[li_4] = gd_148;
      g_ibuf_124[li_4] = gd_140;
      g_ibuf_128[li_4] = gd_132;
   }
   return (0);
}
[ /code ]
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 37786331
> i not get <br /> in tag [ code ]
I'm confused, what exactly is your problem then?
can you please give an example verbatime what you have, what you expect and what you get (which is buggy in your opinion)
0
 

Accepted Solution

by:
XSoFTz earned 0 total points
ID: 37786368
now i need fix bug..

1. put bbcode in my code
[ code ]
int start() {
   if (gs_108 != gs_100) return (0);
   int ind_counted_0 = IndicatorCounted();
   int li_8 = Bars - ind_counted_0;
   if (li_8 > 1) li_8 = Bars - 3;
   double ld_12 = p / 100.0;
   for (int li_4 = li_8; li_4 >= 0; li_4--) {
      if (Close[li_4] < gd_132) {
         gd_132 = Close[li_4];
         gd_148 = gd_132 * (2.0 * ld_12 + 1.0);
         gd_140 = gd_132 * (ld_12 + 1.0);
      }
      if (Close[li_4] > gd_148) {
         gd_148 = Close[li_4];
         gd_132 = gd_148 * (1 - 2.0 * ld_12);
         gd_140 = gd_148 * (1 - ld_12);
      }
      g_ibuf_120[li_4] = gd_148;
      g_ibuf_124[li_4] = gd_140;
      g_ibuf_128[li_4] = gd_132;
   }
   return (0);
}
[ /code ]

Open in new window



2. i use function

function bbcode_format ($str) {  
           // $str = htmlentities($str);  ¿¿¿¿¿¿¿¿¿¿¿¿¿¿¿
              $str = htmlspecialchars($str);
          
            $simple_search = array(  
                        //added line break  
                        '/\[br\]/is',  
                        '/\[b\](.*?)\[\/b\]/is',  
                        '/\[i\](.*?)\[\/i\]/is',  
                        '/\[u\](.*?)\[\/u\]/is',  
                        '/\[url\=(.*?)\](.*?)\[\/url\]/is',  
                        '/\[url\](.*?)\[\/url\]/is',  
                        '/\[align\=(left|center|right)\](.*?)\[\/align\]/is',  
                        '/\[img\](.*?)\[\/img\]/is',  
                        '/\[mail\=(.*?)\](.*?)\[\/mail\]/is',  
                        '/\[mail\](.*?)\[\/mail\]/is',  
                        '/\[font\=(.*?)\](.*?)\[\/font\]/is',  
                        '/\[size\=(.*?)\](.*?)\[\/size\]/is',  
                        '/\[color\=(.*?)\](.*?)\[\/color\]/is',  
                          //added textarea for code presentation  
                       '/\[codearea\](.*?)\[\/codearea\]/is',  
                         //added pre class for code presentation  
                      '/\[code\](.*?)\[\/code\]/is',  
                        //added paragraph  
                      '/\[p\](.*?)\[\/p\]/is',  
                        );  
          
            $simple_replace = array(  
                        //added line break  
                       '<br />',  
                        '<strong>$1</strong>',  
                        '<em>$1</em>',  
                        '<u>$1</u>',  
                        // added nofollow to prevent spam  
                        '<a href="$1" rel="nofollow" title="$2 - $1"  target="_blank">$2</a>',  
                        '<a href="$1" rel="nofollow" title="$1"  target="_blank">$1</a>',  
                        '<div style="text-align: $1;">$2</div>',  
                        //added alt attribute for validation  
                        '<a href="$1" rel="nofollow" target="_blank"><img  class="thumb" src="$1" alt="http://www.ai4investment.com" /></a>',  
                        '<a href="mailto:$1"  target="_blank">$2</a>',  
                        '<a href="mailto:$1"  target="_blank">$1</a>',  
                        '<span style="font-family: $1;">$2</span>',  
                        '<span style="font-size: $1;">$2</span>',  
                        '<span style="color: $1;">$2</span>',  
                        //added textarea for code presentation  
                        '<textarea class="code_container" rows="30" cols="70">$1</textarea>',  
                        //added pre class for code presentation  
                        '<pre class="brush: cpp">$1</pre>',  
                        //added paragraph  
                        '<p>$1</p>',  
                        );  
          
            // Do simple BBCode's  
            $str = preg_replace ($simple_search, $simple_replace, $str);  
          
            // Do <blockquote> BBCode  
            $str = $this->bbcode_quote ($str);  
         
            return $bbcode;  
        }     

Open in new window


3. in display text in forums it ok, but text not convert \n to <br />

4. i insert code in function.
            $bbcode = nl2br($str);

Open in new window


5. but display in forums in tag [ code ] and show <br />, i not need it
Not work
6. i need u help fix or find function for work bbcode...
0
 

Author Closing Comment

by:XSoFTz
ID: 37859812
....
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now