Link to home
Start Free TrialLog in
Avatar of chrisnetonline
chrisnetonlineFlag for United States of America

asked on

remove nested bbcode

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.
Avatar of chrisnetonline
chrisnetonline
Flag of United States of America image

ASKER

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;
}
just change this line
$nu_string .= '[quote]'.$s_array[$key];
to
$nu_string .= $s_array[$key].'[quote]';

the above lines will solve teh problem
>> before the quoting it eats it and throws it into a quote.
now if there isnt text before the quote it takes some of the text from the quote and spits it above the quote box
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
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;
      }
now its back to sucking whatever is above the quote tag into the quote
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;
}
?>
ASKER CERTIFIED SOLUTION
Avatar of armoghan
armoghan
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
remove this line

echo "Capture Last<BR>";
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)) ;

?>
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
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