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
Solved

remove nested bbcode

Posted on 2004-09-15
16
411 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
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 …

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