Solved

remove nested bbcode

Posted on 2004-09-15
16
415 Views
Last Modified: 2010-08-05
Okay, say I have the quote code [quote][/quote] bbcode.

I want to replace the following:

[quote]
[quote]
nested test
[/quote]
outside test
[/quote]

with:

[quote]
outside side
[/quote]

Also, please not there can be infinate amount of nested [quote] tags.
0
Comment
Question by:chrisnetonline
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
16 Comments
 

Author Comment

by:chrisnetonline
ID: 12066508
i have a current script that does this but if you have text before the quoting it eats it and throws it into a quote.

function kill_nesting($string)
{
      $s_array = explode('[quote]',$string);
      $nu_string = '';
      $nesting = 0;
      if(sizeof($s_array) > 1)
      {
            foreach($s_array as $key => $value)
            {
                  $ss_array = explode('[/quote]',$value);
                  if(sizeof($ss_array) > 1 && $nesting) // Capture Last
                  {
                        $nu_string .= $ss_array[(sizeof($ss_array)-2)].'[/quote]'.$ss_array[(sizeof($ss_array)-1)]; // Collect Last Element Which Holds 1st Quote
                        $nesting = 0;
                  }
                  else if($nesting == 1) // Dump Nested Material
                  {
                        ;
                  }
                  else // Capture First
                  {
                        if($value != '')
                        {
                              $nu_string .= '[quote]'.$s_array[$key];
                              $nesting = 1;
                        }
                  }
            }
      }
      else
      {
            $nu_string = $string;
      }
      unset($ss_array);
      unset($s_array);
      return $nu_string;
}
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12071869
just change this line
$nu_string .= '[quote]'.$s_array[$key];
to
$nu_string .= $s_array[$key].'[quote]';
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12071876

the above lines will solve teh problem
>> before the quoting it eats it and throws it into a quote.
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:chrisnetonline
ID: 12071912
now if there isnt text before the quote it takes some of the text from the quote and spits it above the quote box
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12072023
ohh got the problem,
its the explode('[quote]',$string);
which forgets that there was text before quote or not
You need to have a check in
// Capture First
block to see if the $string starts with quote or not .

I hope you get it .
If you want i can give you code for it
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12072073
check this piece of code in if condition

      if($value != '')
      {
      $value1 = strpos($string, '[quote]');
      if( $value1 == 0 )
      {
            $nu_string .= '[quote]'.$s_array[$key];
      }
      else
      {
            $nu_string .= '[quote]'.$s_array[$key].$s_array[$key+1];
      }
      $nesting = 1;
      }
0
 

Author Comment

by:chrisnetonline
ID: 12072117
now its back to sucking whatever is above the quote tag into the quote
0
 
LVL 3

Expert Comment

by:xakem
ID: 12090876
hello!
according to your question this function could help you:

<?php
function remNes($str)
{
      $ar = explode('[/quote]', $str);
      if (count($ar > 2))
      {
            return('[quote]' . $ar[count($ar) - 2] . '[/quote]');
      }
      return $str;
}
?>
0
 
LVL 18

Accepted Solution

by:
armoghan earned 500 total points
ID: 12091120
test the following code with differnet variations

      <?
      function kill_nesting($string)
      {
      $s_array = explode('[quote]',$string);
      $nu_string = '';
      $nesting = 0;
      if(sizeof($s_array) > 1)
      {
      foreach($s_array as $key => $value)
      {
      $ss_array = explode('[/quote]',$value);
      if(sizeof($ss_array) > 1 && $nesting) // Capture Last
      {
      $nu_string .= $ss_array[(sizeof($ss_array)-2)].'[/quote]'.$ss_array[(sizeof($ss_array)-1)]; // Collect Last Element Which Holds 1st Quote
      $nesting = 0;
      echo "Capture Last<BR>";
      }
      else if($nesting == 1) // Dump Nested Material
      {
      ;
      }
      else // Capture First
      {
      if($value != '')
      {
      $value1 = strpos($string, '[quote]');
      if( $value1 == 0 )
      {
            $nu_string .= '[quote]'.$s_array[$key];
      }
      else
      {
            $nu_string .= $s_array[$key].'[quote]'.$s_array[$key+1];
      }
      $nesting = 1;
      }
      }
      }
      }
      else
      {
      $nu_string = $string;
      }
      unset($ss_array);
      unset($s_array);
      return $nu_string;
      }

      $string = "[quote]Inside First
      [quote]
      [quote]
      nested test
      [quote]
      [quote]
      nested test
      [/quote]
      nested test
      [/quote]
      nested test
      [/quote]
      nested test
      [/quote]
      outside test
      [/quote]Last
      ";
      echo "before killing <BR>". $string;
      echo "<BR>after killing<BR>" .kill_nesting(ltrim($string)) ;
      ?>
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12091121
remove this line

echo "Capture Last<BR>";
0
 

Author Comment

by:chrisnetonline
ID: 12092382
however it messes up if there are more than one set of "main" quotes like below.

The current code outputs the below:
Test [quote]Inside First outside test [/quote]Last new before new inside [/quote] new after [quote]

How I need it to be outputed:
Test [quote]Inside First outside test [/quote]Last  new before [quote]new inside[/quote]  new after



<?php

     function kill_nesting($string)
     {
     $s_array = explode('[quote]',$string);
     $nu_string = '';
     $nesting = 0;
     if(sizeof($s_array) > 1)
     {
     foreach($s_array as $key => $value)
     {
     $ss_array = explode('[/quote]',$value);
     if(sizeof($ss_array) > 1 && $nesting) // Capture Last
     {
     $nu_string .= $ss_array[(sizeof($ss_array)-2)].'[/quote]'.$ss_array[(sizeof($ss_array)-1)]; // Collect Last Element Which Holds 1st Quote
     $nesting = 0;
     }
     else if($nesting == 1) // Dump Nested Material
     {
     ;
     }
     else // Capture First
     {
     if($value != '')
     {
     $value1 = strpos($string, '[quote]');
     if( $value1 == 0 )
     {
          $nu_string .= '[quote]'.$s_array[$key];
     }
     else
     {
          $nu_string .= $s_array[$key].'[quote]'.$s_array[$key+1];
     }
     $nesting = 1;
     }
     }
     }
     }
     else
     {
     $nu_string = $string;
     }
     unset($ss_array);
     unset($s_array);
     return $nu_string;
     }

     $string = "Test [quote]Inside First
     [quote]
     [quote]
     nested test
     [quote]
     [quote]
     nested test
     [/quote]
     nested test
     [/quote]
     nested test
     [/quote]
     nested test
     [/quote]
     outside test
     [/quote]Last
     
     
     new before
     [quote]
     new inside
     [/quote]
     new after    
     ";
     echo "before killing <BR>". $string;
     echo "<BR>after killing<BR>" .kill_nesting(ltrim($string)) ;

?>
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12109771
hmm ,
It would be very difficult to go with the same algorithm and solve the problem you stated in the last comment as explode('[quote]',$string); removes the quote and do not remember the any quotes in between.
To do such a task probably some other algorithm is required
0
 
LVL 18

Expert Comment

by:armoghan
ID: 12492506
In the initial requirement there was nothing about nested quotes.. Which was introduced afterwards.. So initial requirement was fulfilled..
The nesting requires soime another algorithum and cannot be done by the same code

So would claim points for this post
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

726 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