GIDNetwork > BBCode - the [quote] tag
Register
« GIDPanel - Member's Area is now open How Does a Credit Card Transaction Get Processed? »

BBCode - the [quote] tag

by: JdS - May 05, 2005

I spent the last 2 days figuring out how to write PHP codes to process [QUOTE] bb code tags. It seemed like an easy thing to do until I actually sat down to write the whole thing.

Sometimes "knowing too much" can be a bad thing - in this particular instance, it really was!

I started writing this little function using the PHP preg_match_all function. It worked really well but it failed miserably when there were nested [quote] bb code tags to be found anywhere in the text.

Anyway, to make a long story short, I finally decided to "think differently" and re-wrote the function from the ground up... and so far, so good!

PHP function - QUOTE BB Code

Let's assume you have an example text that contains one or more [quote] bbcode tags and it looks something like this:

Generic Code Example:

Some text dummy text quoted from [url=http://www.lipsum.com/]http://www.lipsum.com/[/url]
[quote][b]What is Lorem Ipsum?[/b]

Lorem Ipsum is simply dummy text of the printing and typesetting industry.[/quote]

Still some more:
[quote][b]Where does it come from?[/b]

Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.[/quote]

Now that we have a sample to work with, let me show you how I wrote the function to process the [quote] bbcode tags on this site.

PHP Code Example:


<?php

/**
  * Process [quote] bbcode tags in a string.
  * 
  * Processes [quote] bbcode tags in a string, even nested ones.
  * 
  * @access public
  * @author J de Silva
  * @email giddomains@gmail.com
  * @url http://www.gidnetwork.com/b-6.html
  * @param string $str Passed by reference
  * @return void Working with the actual string, not a copy.
  */
function processBBCode_quote( &$str )
{
  // no point proceeding if there are no bb code tags for QUOTE  
  if( false!==strpos($str'[quote]') or false!==strpos($str'[QUOTE]') )
  {
    $len      strlen$str );
    // some flags
    $pos      0;
    $new_str  null;
    $tag      null;
    $in_quote false;
    $nested   = array();
    while( $pos<$len )
    {
      $c $str{$pos}; // get the current character
      if( $tag )
      {
        $tag .= $c;
        if( $c==']' )
        {
          if( $tag=='[quote]' or $tag=='[QUOTE]' )
          {
            if( $in_quote )
            {
              $nested[] = true;
              $in_quote .= '<blockquote>';
            }
            else
            $in_quote '<blockquote title="Quoted Text">';
          }
          elseif( $tag=='[/quote]' or $tag=='[/QUOTE]' )
          {
            if( $nested )
            {
              array_pop$nested );
              $in_quote .= '</blockquote>';
            }
            else
            {
              $new_str $in_quote '</blockquote>';
              $in_quote null;
            }
          }
          else
          {
            // this is some other tag, let it go
            if( $in_quote )
              $in_quote .= $tag;
            else
              $new_str .= $tag;    
          }
          $tag null;    
        }
      }
      elseif( $in_quote )
      {
        if( $c=='[' )
        {
          $tag $c;    
        }
        else
          $in_quote .= $c;
      }
      elseif( $c=='[' )
        $tag .= $c;
      else
        $new_str .= $c;
      ++$pos;
    }
    $str = &$new_str;
  }
}

\?>

Example output - QUOTE BB Code

To see the resulting output, lets paste that example text in [quote] tags right here inside this article, just to see how it handles nested [quote] tags.

Some text dummy text quoted from http://www.lipsum.com/

What is Lorem Ipsum?

Lorem Ipsum is simply dummy text of the printing and typesetting industry.

Still some more:

Where does it come from?

Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.

For (web) display purposes, you can now simply customise the CSS selector for <blockquote> tag in your HTML!

Would you like to comment? This story has been viewed 19,044 times.
« GIDPanel - Member's Area is now open How Does a Credit Card Transaction Get Processed? »

__top__

Copyright © GIDNetwork™ 2001 - 2024

Another website by J de Silva

Page generated in : 0.00739 sec.