?
Solved

remove nested bbcode

Posted on 2004-09-15
16
Medium Priority
?
421 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
13 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

839 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