Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-02-2010, 09:45 AM   PM User | #1
MRushton
New Coder

 
Join Date: Aug 2010
Posts: 45
Thanks: 0
Thanked 14 Times in 14 Posts
MRushton is an unknown quantity at this point
BBCode Parser

PHP Code:
    /**
     * A class to allow easy parsing of BBCode
     *
     * @author Michael Rushton <michael@squiloople.com>
     * @version 1.0
     * @copyright © 2010 Michael Rushton - http://squiloople.com
     *
     * Feel free to use and redistribute this code
     */

    
namespace Parsers;

    final class 
BBCodeParser
    
{

        
/**
         * Array to contain regular expressions of BB tags
         *
         * @access private
         * @var array
         */

        
private $_bbTags = array(

            
'/\[b\]([\x20-\x7E]+?)\[\/b\]/i',
            
'/\[i\]([\x20-\x7E]+?)\[\/i\]/i',
            
'/\[u\]([\x20-\x7E]+?)\[\/u\]/i',
            
'/\[s\]([\x20-\x7E]+?)\[\/s\]/i',
            
'/\[sub\]([\x20-\x7E]+?)\[\/sub\]/i',
            
'/\[sup\]([\x20-\x7E]+?)\[\/sup\]/i',
            
'/\[img\]([\x20-\x7E]+?)\[\/img\]/i',
            
'/\[url\]([\x20-\x7E]+?)\[\/url\]/i',
            
'/\[email\]([\x20-\x7E]+?)\[\/email\]/i',
            
'/\[quote\]([\x20-\x7E]+?)\[\/quote\]/i',
            
'/\[color=([0-9a-f]{6})\]([\x20-\x7E]+?)\[\/color\]/i',
            
'/\[size=([1-9]?[0-9])\]([\x20-\x7E]+?)\[\/size\]/i',
            
'/\[font=([a-z\x20]+)\]([\x20-\x7E]+?)\[\/font\]/i',
            
'/\[img=([\x20-\x7E]+?)\]([\x20-\x7E]+?)\[\/img\]/i',
            
'/\[url=([\x20-\x5A\x5C\x5E-\x7E]+)\]([\x20-\x7E]+?)\[\/url\]/i',
            
'/\[email=([\x20-\x5A\x5C\x5E-\x7E]+)\]([\x20-\x7E]+?)\[\/email\]/i',
            
'/\[quote=([\x20-\x5A\x5C\x5E-\x7E]+)\]([\x20-\x7E]+?)\[\/quote\]/i',

        );

        
/**
         * Array to contain HTML tag replacements
         *
         * @access private
         * @var array
         */

        
private $_htmlTags = array(

            
'<strong>$1</strong>',
            
'<em>$1</em>',
            
'<span style="text-decoration:underline">$1</span>',
            
'<del>$1</del>',
            
'<sub>$1</sub>',
            
'<sup>$1</sup>',
            
'<img src="$1" alt="" />',
            
'<a href="$1">$1</a>',
            
'<a href="mailto:$1">$1</a>',
            
'<fieldset>$1</fieldset>',
            
'<span style="color:#$1;background-color:transparent">$2</span>',
            
'<span style="font-size:$1px">$2</span>',
            
'<span style="font-family:\'$1\', sans-serif">$2</span>',
            
'<img src="$2" alt="$1" />',
            
'<a href="$1">$2</a>',
            
'<a href="mailto:$1">$2</a>',
            
'<fieldset><legend>$1</legend>$2</fieldset>',

        );

        
/**
         * Create "[tag]Text[/tag] => <tag>Text</tag>" style BB tags
         *
         * @access public
         * @param string
         * @param string
         */

        
public function createTag($bb$html false)
        {

            
// If an HTML tag is not specified, emulate the BB tag

            
$html $html ?: $bb;

            
// Create a new BB tag regular expression of the form: [tag]Text[/tag]

            
$this->_bbTags[] = '/\[' $bb '\]([\x20-\x7E]+?)\[\/' $bb '\]/i';

            
// Create a new HTML tag replacement of the form <tag>Text</tag>

            
$this->_htmlTags[] = '<' $html '>$1</' $html '>';

        }

        
/**
         * Create "[tag=option]Text[/tag] => <tag option="option">Text</tag>" style BB tags
         *
         * @access public
         * @param string
         * @param string
         */

        
public function createParameterTag($bb$html)
        {

            
// Create a new BB tag regular expression of the form [tag=option][/tag]

            
$this->_bbTags[] = '/\[' $bb '=([\x20-\x5A\x5C\x5E-\x7E]+)\]([\x20-\x7E]+?)\[\/' $bb '\]/i';

            
// Create a new HTML tag replacement of the form <tag option="option">Text</tag>

            
$this->_htmlTags[] = '<' $html '>$2</' strtok($html' ') . '>';

        }

        
/**
         * Create "[smile] => :)" style BB tags
         *
         * @access public
         * @param string
         * @param string
         */

        
public function createSpecialTag($bb$html)
        {

            
// Create a new BB tag regular expression of the form [tag] (does not require usual brackets)

            
$this->_bbTags[] = '/' preg_quote($bb) . '/i';

            
// Create a new replacement (does not need to be an HTML tag)

            
$this->_htmlTags[] = $html;

        }

        
/**
         * Return the parsed string
         *
         * @access public
         * @param string
         * @return string
         */

        
public function parseString($string)
        {

            
// Ensure that the loop runs at least once

            
$count 1;

                
// Only replace tags if the string has not been fully parsed

                
while ($count !== 0) {

                    
// Replace BB tags with HTML tags; will repeat if successful

                    
$string preg_replace($this->_bbTags$this->_htmlTags$string, -1$count);

                }

            
// Return the parsed string

            
return $string;

        }

    } 
Usage example:

PHP Code:
  $stringParser = new BBCodeParser;

  echo 
$stringParser->parseString('[url=http://example.com][b]Bold Example[/b][/url]');

  
// <a href="http://example.com"><strong>Bold</strong></a>

  
$stringParser->createTag('p');
  
$stringParser->createTag('d''div');

  echo 
$stringParser->parseString('[d][p]Paragraph 1[/p][p]Paragraph 2[/p][/div]');

  
// <div><p>Paragraph 1</p><p>Paragraph 2</p></div>

  
$stringParser->createParameterTag('span''span id="$1"');

  echo 
$stringParser->parseString('[span=parameter]This has a parameter[/span]');

  
// <span id="special">This is special</span>

  
$stringParser->createSpecialTag(':)''<img src="smile.png" alt="smile" />');

  echo 
$stringParser->parseString('Look at my smile: :)');

  
// Look at my smile: <img src="smile.png" alt="smile" /> 
The default tags are:

PHP Code:
[b]Bold[/b]
[
i]Italics[/i]
[
u]Underline[/i]
[
s]Strikethrough[/s]
[
sub]Subscript[/sub]
[
sup]Superscript[/sup]
[
img]image.png[/img]
[
url]http://example.com[/url]
[email]michael@example.com[/email]
[
quote]Quote[/quote]
[
color=red]Red[/color]
[
size=15]15px[/size]
[
font=Arial]Arial[/font]
[
img=alt]smile.png[/img]
[
url=http://example.com]Example[/url]
[email=michael@example.com]Michael[/email]
[
quote=Michael]Quote[/quote

Last edited by MRushton; 11-02-2010 at 01:19 PM..
MRushton is offline   Reply With Quote
Old 11-02-2010, 03:32 PM   PM User | #2
SmoothieVemund
New Coder

 
Join Date: Apr 2010
Posts: 28
Thanks: 3
Thanked 0 Times in 0 Posts
SmoothieVemund is an unknown quantity at this point
Real cool. I want to use this in a textarea with a form. How can I do that?
PHP Code:
    <form action="post_massage.php" method="post">
<
textarea rows="5" cols="55" name="post" wrap="physical">Enter your favorite quote!</textarea><br />
<
input type="submit" />
</
form
SmoothieVemund is offline   Reply With Quote
Old 11-02-2010, 06:16 PM   PM User | #3
MRushton
New Coder

 
Join Date: Aug 2010
Posts: 45
Thanks: 0
Thanked 14 Times in 14 Posts
MRushton is an unknown quantity at this point
If you're looking to insert/update a row into the table on the form submission, try this. Note, however, that where I have "new Parsers/BBCodeParser", it should say "new Parsers\BBCodeParser". Unfortunately, this forum seems to strip backslashes.

PHP Code:

  
if (isset($_POST['post'])) {

    require_once 
'BBCodeParser.php';

    
$stringParser = new Parsers/BBCodeParser;

    
$parsedString $mysqli->real_escape_string($stringParser->parseString($_POST['post']));

    try {

      if (!
$mysqli->query("INSERT INTO `table` (`post`) VALUES ('" $parsedString "')") {
        throw new 
Exception('Unable to insert the post into the database.');
      }

    }

    catch (
Exception $e) {
      echo 
$e->getMessage();
    }

  } 

Last edited by MRushton; 11-02-2010 at 06:25 PM..
MRushton is offline   Reply With Quote
Old 11-02-2010, 06:23 PM   PM User | #4
MRushton
New Coder

 
Join Date: Aug 2010
Posts: 45
Thanks: 0
Thanked 14 Times in 14 Posts
MRushton is an unknown quantity at this point
To do list:

1. Include tags which do not parse contents within
2. Enforce XHTML conformity
3. Make createOptionTag() more flexible (at the moment, to emulate the replacement for [quote=parameter]Text[/quote] one would need to do createOptionTag('quote', 'fieldset><legend>$1</legend'), which isn't very nice).
4. createTag(), createParameterTag(), and createSpecialTag() should return an array containing the BBCode regular expression and its HTML replacement.

Last edited by MRushton; 11-02-2010 at 06:33 PM..
MRushton is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:38 PM.


Advertisement
Log in to turn off these ads.