<?php
/**
* Squiloople Framework
*
* LICENSE: Feel free to use and redistribute this code.
*
* @author Michael Rushton <michael@squiloople.com>
* @link http://squiloople.com/
* @version 1.0
* @category Squiloople
* @package Models
* @subpackage Parsers
* @copyright © 2010 Michael Rushton
*/
// Define the namespace
namespace Models\Parsers;
/**
* BBCode Parser
*
* Parses BBCode in a string
*/
final class BBCodeParser
{
/**
* Array to contain regular expressions of BB tags
*
* @access private
* @var array $_bbTags
*/
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 $_htmlTags
*/
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 $bbTag
* @param string|bool $htmlTag
* @return array
*/
public function createTag($bbTag, $htmlTag = false)
{
// If an HTML tag is not specified, emulate the BB tag
$htmlTag = $htmlTag ?: $bbTag;
// Create a new BB tag regular expression of the form: [tag]Text[/tag]
$this->_bbTags[] = '/\[' . $bbTag. '\]([\x20-\x7E]+?)\[\/' . $bbTag . '\]/i';
// Create a new HTML tag replacement of the form Text
$this->_htmlTags[] = '<' . $htmlTag . '>$1</' . strtok($htmlTag, ' ') . '>';
// Return an array with the BBCode regular expression and the HTML replacement
return array(end($this->_bbTags), end($this->_htmlTags));
}
/**
* Create "[tag=option]Text[/tag] => <tag option="option">Text</tag>" style BB tags
*
* @access public
* @param string $bbTag
* @param string $htmlTag
* @return array
*/
public function createParameterTag($bbTag, $htmlTag)
{
// Create a new BB tag regular expression of the form [tag=option][/tag]
$this->_bbTags[] = '/\[' . $bbTag . '=([\x20-\x5A\x5C\x5E-\x7E]+)\]([\x20-\x7E]+?)\[\/' . $bbTag . '\]/i';
// Create a new HTML tag replacement of the form Text
$this->_htmlTags[] = '<' . $htmlTag . '>$2</' . strtok($htmlTag, ' ') . '>';
// Return an array with the BBCode regular expression and the HTML replacement
return array(end($this->_bbTags), end($this->_htmlTags));
}
/**
* Create "[smile] => :)" style BB tags
*
* @access public
* @param string $bbTag
* @param string $htmlTag
* @return array
*/
public function createSpecialTag($bbTag, $htmlTag)
{
// Create a new BB tag regular expression of the form [tag] (does not require usual brackets)
$this->_bbTags[] = '/' . preg_quote($bbTag) . '/i';
// Create a new replacement (does not need to be an HTML tag)
$this->_htmlTags[] = $htmlTag;
// Return an array with the BBCode regular expression and the HTML replacement
return array(end($this->_bbTags), end($this->_htmlTags));
}
/**
* Return the parsed string
*
* @access public
* @param string $string
* @return string
*/
public function parseString($string)
{
// Only replace tags if the string has not been fully parsed
for ($count = 1; $count != 0;)
{
$string = preg_replace($this->_bbTags, $this->_htmlTags, $string, -1, $count);
}
// Return the parsed string
return $string;
}
}
Read More
Tags: bb code, parsers