Solved

remove nested bbcode

Posted on 2004-09-15
16
407 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
  • 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
 

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Php Array Diff 3 27
Phone Dialer 5 36
Creating Image Thumbnails Using PHP 3 15
confirm popup before record delete takes place 87 24
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 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

21 Experts available now in Live!

Get 1:1 Help Now