Scripts
UBB Parser uitgebreid
Ik heb voor mijn eigen framework een UBB parser gemaakt. Deze wil ik graag met jullie delen. Het is mogelijk om zelf codes toe te voegen.
index.php
<?php
require( 'classes/ubb.php' );
$text = '';
//Start the bbcode class
$ubb = new ubb;
//Add a smiley
$ubb->addSmiley( 'smile', 'http://www.one2xs.com/img/smileys/smile.gif');
//Add a smiley without colons
$ubb->addShortSmiley( ':-)', 'http://www.one2xs.com/img/smileys/smile.gif' );
//Add a UBB
$ubb->addUBB( 'i', '<i>\1</i>' );
/**
* Add a UBB with custom function
*
* - The param has the following attributes
* string (The unedited string)
* text (The text inside the tags)
* tag (The tag name)
* attributes (The attributes in the first tag)
*/
$ubb->addUBB( 'url', array( 'ubb', '_tagURL' ) );
//Parse a string
echo $ubb->parse( $text );
?>
ubb.php
<?php
/**
* Class: UBB parser
* @discription Replace the UBB in your text
* @author Jan Erik van Woerden
*
*/
class ubb {
/**
* UBB
*
* (default value: array())
*
* @var array
* @access private
*/
private $ubb = array();
/**
* smileys
*
* (default value: array())
*
* @var array
* @access private
*/
private $smileys = array();
/**
* shortsmileys
*
* (default value: array())
*
* @var array
* @access private
*/
private $shortsmileys = array();
/**
* __construct function.
*
* @access public
* @return void
*/
public function __construct() {
$this->addUBB( 'i', '<i>\1</i>' );
$this->addUBB( 'b', '<strong>\1</strong>' );
$this->addUBB( 'u', '<span style="text-decoration:underline">\1</span>' );
$this->addUBB( 's', '<span style="text-decoration:line-through">\1</span>' );
$this->addUBB( 'sub', '<sub>\1</sub>' );
$this->addUBB( 'sup', '<sup>\1</sup>' );
$this->addUBB( 'center', '<div style="text-align: center;">\1</div>' );
$this->addUBB( 'code', '<code>\1</code>' );
$this->addUBB( 'url', array( $this, '_tagURL' ) );
$this->addUBB( 'list', array( $this, '_tagList' ) );
$this->addUBB( 'img', array( $this, '_tagImg' ) );
}
/**
* addUBB function.
*
* @access public
* @param string $tag
* @param string $function
* @return bool
*/
public function addUBB( $tag, $function) {
if( !empty( $tag ) && is_string( $tag ) && !empty( $function ) && ( is_string( $function ) OR is_array( $function ) ) ) {
$this->ubb[ $tag ] = $function;
return true;
}
return false;
}
/**
* Add a smiley with :{tag}:
*
* @access public
* @param string $tag
* @param string $url The url to the image
* @return bool
*/
public function addSmiley( $tag, $url ) {
if( !empty( $tag ) && !empty( $url ) && is_string( $tag ) && is_string( $url ) ) {
$this->smileys[ $tag ] = $url;
return true;
}
return false;
}
/**
* Add a smiley without :{tag}: only {tag}
*
* @access public
* @param string $tag
* @param string $url The url to the image
* @return bool
*/
public function addShortSmiley( $tag, $url ) {
if( !empty( $tag ) && !empty( $url ) && is_string( $tag ) && is_string( $url ) ) {
$this->shortsmileys[ $tag ] = $url;
return true;
}
return false;
}
/**
* set2preg function.
*
* @access private
* @param string $string
* @return string
*/
private function set2preg( $string ) {
if( is_string( $string ) ) {
$string = str_replace( ')', '\)', $string );
$string = str_replace( '(', '\(', $string );
$string = str_replace( '/', '\/', $string );
$string = str_replace( '[', '\[', $string );
$string = str_replace( ']', '\]', $string );
}
return $string;
}
/**
* The parse function.
*
* @access public
* @param string $text
* @return string
*/
public function parse( $text ) {
$text = htmlspecialchars( $text );
$text = $this->ubbParse( $text );
$text = $this->smileyParse( $text );
$text = nl2br( $text );
return $text;
}
/**
* smileyParse function.
*
* @access private
* @param string $text
* @return string
*/
private function smileyParse( $text ) {
if( is_string( $text ) ) {
$search = array();
$replace = array();
foreach( $this->smileys as $key => $value ) {
$search[] = '/:' . $this->set2preg( $key ) . ':/';
$replace[] = '<img src="' . $value . '" class="smiley" title=":' . $key . ':" />';
}
foreach( $this->shortsmileys as $key => $value ) {
$search[] = '/' . $this->set2preg( $key ) . '/';
$replace[] = '<img src="' . $value . '" class="smiley" title="' . $key . '" />';
}
$text = preg_replace( $search, $replace, $text );
}
return $text;
}
/**
* Parse all the UBB functions
*
* @access private
* @param string $text
* @return string
*/
private function ubbParse( $text ) {
$text = preg_replace_callback( '~(?:\[(([^\s=]+))((?:(?: |=).+?)?)(?<!/)\]((?s).+?)\[/\2\])|(?:(?m)\[(([^\s=]+)(?:(?: |=).+?)?) /\](.+?)$)~iS', array( $this, '_tagUBB' ), $text );
return $text;
}
/**
* Every time the parser finds a UBB match he calls this function
*
* This functions calls the good function for every tag and replace the tags.
*
* @access private
* @param string $input
* @return string
*/
private function _tagUBB( $input ) {
$send[ 'string' ] = $input[ 0 ];
$send[ 'tag' ] = $input[ 2 ];
$send[ 'text' ] = $this->ubbParse( $input[ 4 ] );
if( $send[ 'tag' ] == 'no' ) {
return $input[ 4 ];
}
if( substr( $input[ 3 ], 0, 1 ) == '=' ) {
preg_match_all( '~([^\s=]+)(?:=(?(?=\')\'(.+?)\'|(.+?)))?(?: |$)~', $input[ 3 ], $return, PREG_SET_ORDER );
$send[ 'attributes' ] = $return[ 0 ][ 1 ];
} elseif( substr( $input[ 3 ], 0, 1 ) == ' ' ) {
preg_match_all( '~([^\s=]+)(?:=(?(?=\')\'(.+?)\'|(.+?)))?(?: |$)~', $input[ 3 ], $return, PREG_SET_ORDER );
$send['attributes'] = array();
foreach( $return as $attribute) {
if( !isset( $attribute[ 2 ] ) ) {
$attribute[ 2 ] = $attribute[ 1 ];
}
$send['attributes'][ $attribute[ 1 ] ] = $attribute[ 2 ];
}
}
if( isset( $this->ubb[ $send[ 'tag' ] ] ) ) {
if( is_callable( $this->ubb[ $send[ 'tag' ] ] ) ) {
return call_user_func( $this->ubb[ $send[ 'tag' ] ], $send );
} elseif( is_string( $this->ubb[ $send[ 'tag' ] ] ) ) {
return str_replace( '\1', $send[ 'text' ], $this->ubb[ $send[ 'tag' ] ] );
}
}
return $input[ 0 ];
}
/**
* The function for parsing a url.
*
* @access public
* @param mixed $send
* @return void
*/
public function _tagURL( $send ) {
$urlregex = "^(https?|ftp)\:\/\/([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*(\:[0-9]{2,5})?(\/([a-z0-9+\$_-]\.?)+)*\/?(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?(#[a-z_.-][a-z0-9+\$_.-]*)?\$";
if( isset( $send[ 'attributes' ] ) && is_string( $send[ 'attributes' ] ) && eregi( $urlregex, $send[ 'attributes' ] ) ) {
return '<a href="' . $send[ 'attributes' ] . '" target="_blank">' . $send[ 'text' ] . '</a>';
} elseif( eregi( $urlregex, $send[ 'text' ] ) ) {
return '<a href="' . $send[ 'text' ] . '" target="_blank">' . $send[ 'text' ] . '</a>';
}
return $send[ 'string' ];
}
/**
* The function for parsing a list.
*
* @access public
* @param mixed $send
* @return void
*/
public function _tagList( $send ) {
preg_match_all( "/\[\*\](.*)\n/", $send[ 'text' ], $checks, PREG_SET_ORDER );
$return = '<ul>';
foreach( $checks as $check ) {
$return .= '<li>' . $check[ 1 ] . '</li>';
}
$return .= '</ul>';
return $return;
}
/**
* The function for parsing a image.
*
* @access public
* @param mixed $send
* @return void
*/
public function _tagImg( $send ) {
$urlregex = "^(https?|ftp)\:\/\/([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*(\:[0-9]{2,5})?(\/([a-z0-9+\$_-]\.?)+)*\/?(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?(#[a-z_.-][a-z0-9+\$_.-]*)?\$";
if( eregi( $urlregex, $send[ 'text' ] ) ) {
return '<img class="img" src="' . $send[ 'text' ] . '" title="' . $send[ 'text' ] . '" />';
}
return $send[ 'string' ];
}
}
?>
Reacties
0